Using SSH with Gradle

Today one point of the agenda was to upload an OSGi Library to a remote server. The Gradle SSH Plugin to the rescue. First we create a Gradle configuration - this is usually a gradle.properties file:

# sftp server url
FTP_HOST=datenkollektiv.de

# sftp username
FTP_USER=fluffi

# sftp username
FTP_IDENTITY=id_rsa

# absolute path to the root of the update-sites mirror
FTP_MIRROR_PATH=/var/lib/virgo-tomcat-3.6.4.RELEASE/pickup

In the build file responsible for uploading the OSGi bundles upload.gradle we use the properties defined above to configure the SSH Plugin. Most note worthy in here: Referencing the user's home directory in a Gradle script.

plugins { id 'org.hidetake.ssh' version '1.1.4' }

remotes {
  datenkollektivDe {
    host = FTP_HOST
    user =  FTP_USER
    identity = file(System.properties['user.home'] + "/.ssh/${FTP_IDENTITY}")
    knownHosts = file(System.properties['user.home'] + "/.ssh/known_hosts")
  }
}

The remaining part of the script is pretty straight forward:

task uploadOSGiBundle << {
  ssh.run {
    session(remotes.datenkollektivDe) {
      execute "mkdir -p ${FTP_MIRROR_PATH}"
      put from: "${buildDir}/planets-homepage.jar", into: "${FTP_MIRROR_PATH}"
    }
  }
}