Copy Static Content Using SSH and Gradle - kercheval/GradleCMPlugin GitHub Wiki
Often, there is a subset of content in a server that is statically generated and stored. This is often articles, pages, lists, blog entries, etc.
This content is often required during development to ensure that the visual presentation is accurate and that no poor interactions are present.
This example set has two pieces: one example to acquire the static content and one example to actually use it.
Acquiring Content from the Server
This example file uses the SCP ant task to acquire the static content after prompting for username and password.
//
// This gradle file is responsible for download and maintenance of
// the static content (CMS/Jekyll/etc)
//
def referenceHost = 'static.mydomain.com'
def referenceHostFiledir = '/opt/static-export'
//
// This section defines the dependencies and configuration needed to use
// the jsch (ssh) ant task
//
configurations {
sshAntTask
}
dependencies {
sshAntTask 'org.apache.ant:ant-jsch:1.7.1', 'jsch:jsch:0.1.29'
}
//
// Define the location of the destination and the needed static directories.
// These are defined as ext so that subprojects can reference these locations
// using a reference like ${rootProject.cmsDestDir}
//
// Note: Normally the destination directory here would be in .gitignore and
// and not part of the checked in sources.
//
ext {
staticDestDir = new File(rootProject.projectDir, 'dev/static')
staticStaticDirs = [ 'images' ]
staticJspDirs = [ 'intros', 'content' ]
}
//
// This task queries for username and password, makes the SSH connection
// to the machine and downloads content from the machine using scp.
//
task staticUpdate {
description = "Obtain static reference files from ${referenceHost}"
setGroup('Static Content')
doLast {
//
// Need login credentials for the remote server
//
def reader = new BufferedReader(new InputStreamReader(System.in))
println "Please enter username for ${referenceHost}:"
def username = reader.readLine()
println "Please enter password for ${referenceHost}:"
def console = System.console()
def password
if (console == null) {
password = reader.readLine()
} else {
password = new String(console.readPassword())
}
//
// Redefine scp Ant task, with the classpath property set to
// sshAntTask configuration classpath.
//
ant.taskdef(name: 'scp',
classname: 'org.apache.tools.ant.taskdefs.optional.ssh.Scp',
classpath: configurations.sshAntTask.asPath)
//
// Copy the static directories over from the static server
//
[
staticStaticDirs,
staticJspDirs
].flatten().each {
println("Copying ${it}...");
def toDir = new File(staticDestDir, "/${it}")
toDir.mkdirs()
ant.scp(file: "${username}@${referenceHost}:${referenceHostFiledir}/${it}/*",
todir: toDir, password: password, trust: 'true', verbose: 'true')
}
}
}
//
// This task removes the cached static content from the reference location
//
task staticClean {
description = "Remove static reference files from project"
setGroup('Static Content')
doLast {
//
// Remove the reference files
//
[
staticStaticDirs,
staticJspDirs
].flatten().each {
println("Removing ${it}...");
def deleteDir = new File(staticDestDir, "/${it}")
delete deleteDir
}
}
}
Placing Cached Content into the Build
To use the static content obtained from your static content server in your build, you need to pull the content over.
This task uses the variables defined in the static gradle example above to accomplish this
//
// This task uses the cached static content and copies them into local build
// locations. The root project 'staticUpdate' task must be run to actually
// get any content from this tasks!
//
task staticCopy(type: Copy) {
description = "Copy from local static cache to build locations"
setGroup('Static Content')
into webAppDirName
rootProject.staticStaticDirs.each { staticDir ->
from("${rootProject.staticDestDir}/${staticDir}") {
into staticDir
}
}
rootProject.staticJspDirs.each { jspDir ->
from("${rootProject.staticDestDir}/${jspDir}") {
into "WEB-INF/jsp/static/${jspDir}"
}
}
}