Commit a7dd3cf8 authored by Christian Meyer's avatar Christian Meyer
Browse files

projects can set properties that allow deploying / publishing on public server

build.gradle
    changed dependency of gradle-ssh-plugin to 2
added DeployDochost.groovy
    task that contains deploy workings
        formerly in ZmtJavadocPlugin, ZmtDeployPlugin
    contains determineCredentials formerly in ZmtDochostPlugin
ZmtDochostPlugin.groovy
    added properties deployPublic, publishPublic
        to allow deploying / publishing to public dochost
    added property sshDryRun
    moved determineCredentials workings to DeployDochost
    no longer allows any hosts in ssh
    creates remote for public dochost
ZmtDeployPlugin, ZmtJavadocPlugin.groovy
    uses DeployDochost task
    deploys to public dochost if deployPublic property is set
ZmtPublishPlugin.groovy
    deploys to public dochost if publishPublic property is set
ZmtLibraryPlugin, ZmtSimulationPlugin.groovy
    moved applying ZmtJavadocPlugin after ZmtDochostPlugin
parent 9d9be7bd
......@@ -20,7 +20,7 @@ dependencies {
compile localGroovy()
testCompile group: 'junit', name: 'junit', version: '4.+'
testCompile group: 'org.hamcrest', name: 'hamcrest-library', version: '1.3'
compile 'org.hidetake:gradle-ssh-plugin:1.+'
compile 'org.hidetake:gradle-ssh-plugin:2.+'
compile 'nu.studer:gradle-credentials-plugin:1.+'
compile 'be.jlr-home.gradle:plantumlPlugin:0.1.+'
/* specify here again to override v8001 in
......
package de.zmt.gradle;
import org.gradle.api.*
import de.zmt.gradle.task.DeployDochost
/*
* Deploy plugin for simulations based on zmt.core
......@@ -10,27 +11,22 @@ import org.gradle.api.*
*/
public class ZmtDeployPlugin implements Plugin<Project> {
private static final def CONFIGURE = {
apply plugin: ZmtDochostPlugin.class
apply plugin: "org.hidetake.ssh"
apply plugin: "distribution"
task('deployZip') {
group = 'Upload'
task('deployZip', type: DeployDochost.class) {
description = 'Uploads zip distribution to dochost.'
dependsOn distZip, setupDochostRemote
dependsOn distZip
remote = remotes.deployRemote
}
deployZip.sessionClosure = {
def remoteTargetDir = remotes.paths[deployZip.remote] + 'dist'
// create target directory on remote
execute "mkdir -p ${remoteTargetDir}"
doLast {
println "\nConnecting to ${remotes.dochost.host}..."
ssh.run {
session(remotes.dochost) {
def remoteTargetDir = setupDochostRemote.deployPath + 'dist'
// create target directory on remote
execute "mkdir -p ${remoteTargetDir}"
put from: distZip.archivePath, into: remoteTargetDir
}
}
}
put from: distZip.archivePath, into: remoteTargetDir
}
}
......
package de.zmt.gradle;
import org.gradle.api.*
import groovy.swing.*
import org.hidetake.groovy.ssh.core.Remote
/*
* Plugin for getting user / password and setting properties for
* internal and public servers' name and path.
* <p>
* With this plugin a task 'determineCredentials' is added to the project
* that asks the user to enter login credentials.
* Plugin for setting up remotes to connect to public / internal dochost.
*/
class ZmtDochostPlugin implements Plugin<Project> {
private static final PROPERTY_PREFIX = ZmtDochostPlugin.class.getPackage().getName() + '.dochost.'
public static final def PUBLIC_HOST_PROPERTY_NAME = PROPERTY_PREFIX + 'publicHost'
public static final def PUBLIC_PATH_PROPERTY_NAME = PROPERTY_PREFIX + 'publicPath'
public static final def INTERNAL_HOST_PROPERTY_NAME = PROPERTY_PREFIX + 'internalHost'
public static final def INTERNAL_PATH_PROPERTY_NAME = PROPERTY_PREFIX + 'internalPath'
private static final def DEFAULT_PUBLIC_HOST = 'ecomod01'
private static final def DEFAULT_PUBLIC_PATH = '/projects/dochost/'
private static final def DEFAULT_INTERNAL_HOST = 'dochost'
private static final def DEFAULT_INTERNAL_PATH = '/var/www/dochost/'
public static final def PUBLIC_HOST_PROPERTY = PROPERTY_PREFIX + 'publicHost'
public static final def PUBLIC_PATH_PROPERTY = PROPERTY_PREFIX + 'publicPath'
public static final def INTERNAL_HOST_PROPERTY = PROPERTY_PREFIX + 'internalHost'
public static final def INTERNAL_PATH_PROPERTY = PROPERTY_PREFIX + 'internalPath'
/** Property name for flag that enables public dochost deploying (javadoc, distributions). */
public static final def DEPLOY_PUBLIC_PROPERTY = PROPERTY_PREFIX + 'deployPublic'
/** Property name for flag that enables public dochost maven publishing (libraries). */
public static final def PUBLISH_PUBLIC_PROPERTY = PROPERTY_PREFIX + 'publishPublic'
/** Property name for dry running ssh deploying (useful for debugging) */
public static final def SSH_DRY_RUN_PROPERTY = PROPERTY_PREFIX + 'sshDryRun'
private static final def CONFIGURE = {
apply plugin: 'nu.studer.credentials'
apply plugin: 'org.hidetake.ssh'
// setup properties
putIfAbsent(project, PUBLIC_HOST_PROPERTY_NAME, DEFAULT_PUBLIC_HOST)
putIfAbsent(project, PUBLIC_PATH_PROPERTY_NAME, DEFAULT_PUBLIC_PATH)
putIfAbsent(project, INTERNAL_HOST_PROPERTY_NAME, DEFAULT_INTERNAL_HOST)
putIfAbsent(project, INTERNAL_PATH_PROPERTY_NAME, DEFAULT_INTERNAL_PATH)
putIfAbsent(project, PUBLIC_HOST_PROPERTY, 'ecomod01')
putIfAbsent(project, PUBLIC_PATH_PROPERTY, '/projects/dochost/')
putIfAbsent(project, INTERNAL_HOST_PROPERTY, 'dochost')
putIfAbsent(project, INTERNAL_PATH_PROPERTY, '/var/www/dochost/')
putIfAbsent(project, DEPLOY_PUBLIC_PROPERTY, false)
// only publish to public dochost by default
putIfAbsent(project, PUBLISH_PUBLIC_PROPERTY, true)
putIfAbsent(project, SSH_DRY_RUN_PROPERTY, false)
tasks.create('determineCredentials') {
description = 'Ask user to enter credentials.'
onlyIf { !credentials.dochostUser || !credentials.dochostPw }
}
determineCredentials.doLast {
def console = System.console()
// we can access console: ask for username / password
if (console) {
console.printf("\nEnter username: ")
credentials.dochostUser = console.readLine()
console.printf("\nEnter password: ")
// readPassword() returns char array, wrap it into string
credentials.dochostPw = new String(console.readPassword())
}
// no console: create dialog and get it from there
else {
// https://www.timroes.de/2014/01/19/using-password-prompts-with-gradle-build-files/
new SwingBuilder().edt {
dialog(
modal: true, // Otherwise the build will continue running before you closed the dialog
title: 'Connecting...', // Dialog title
alwaysOnTop: true, // pretty much what the name says
resizable: false, // Don't allow the user to resize the dialog
locationRelativeTo: null, // Place dialog in center of the screen
pack: true, // We need to pack the dialog (so it will take the size of it's children)
show: true // Let's show it
) {
vbox {
label(text: "username: ")
userInput = textField(text: System.getProperty("user.name"))
userInput.selectAll()
label(text: "password: ")
pwInput = passwordField()
button(defaultButton: true, text: 'OK', actionPerformed: {
credentials.dochostUser = userInput.text
credentials.dochostPw = pwInput.text
dispose() // Close dialog
})
}
}
}
}
println "Use task 'addCredentials' with keys 'dochostUser' and 'dochostPw' if you want to store credentials permanently."
}
tasks.create('setupDochostRemote') {
dependsOn('determineCredentials')
ext.deployPath = getProperty(INTERNAL_PATH_PROPERTY_NAME)
}
setupDochostRemote.doLast {
remotes {
dochost {
host = getProperty(INTERNAL_HOST_PROPERTY_NAME)
knownHosts = allowAnyHosts
user = credentials.dochostUser
password = credentials.dochostPw
}
}
ssh.settings {
dryRun = getProperty(SSH_DRY_RUN_PROPERTY).toBoolean()
}
createRemotes(project)
}
private static void putIfAbsent(Project project, String propertyName, Object propertyValue) {
if (!project.ext.has(propertyName)) {
if (!project.hasProperty(propertyName)) {
project.ext.set(propertyName, propertyValue)
}
}
/**
* Creates remotes publicDochost and internalDochost.
* @param the project to create the remotes for
*/
private static void createRemotes(def project) {
// create an empty map for paths
def remotes = project.remotes
remotes.ext.paths = [:]
['deploy', 'publish'].each { target ->
def isPublic = project.getProperty(PROPERTY_PREFIX + target + 'Public').toBoolean()
def targetMode = isPublic ? 'public' : 'internal'
def targetHost = project.getProperty(PROPERTY_PREFIX + targetMode + 'Host')
// def targetRemote = new Remote(targetMode + 'Dochost')
def targetRemote = new Remote(targetMode + 'Dochost')
def targetPath = project.getProperty(PROPERTY_PREFIX + targetMode + 'Path')
targetRemote.host = targetHost
remotes.ext.set(target + 'Remote', targetRemote)
remotes.ext.paths[targetRemote] = targetPath
remotes.add(targetRemote)
}
}
@Override
void apply(Project project) {
project.configure(project, CONFIGURE)
......
......@@ -3,6 +3,7 @@ package de.zmt.gradle;
import org.gradle.api.*
import org.gradle.external.javadoc.JavadocOfflineLink
import org.gradle.plugins.ide.eclipse.model.ProjectDependency
import de.zmt.gradle.task.DeployDochost
/*
* Plugin for adding links to dependency projects to javadoc task
......@@ -11,35 +12,30 @@ import org.gradle.plugins.ide.eclipse.model.ProjectDependency
*/
class ZmtJavadocPlugin implements Plugin<Project> {
private static final def CONFIGURE = {
apply plugin: ZmtDochostPlugin.class
apply plugin: be.jlrhome.gradle.plantuml.PlantumlPlugin
// use svg instead of the default (png)
plantuml.fileFormat = net.sourceforge.plantuml.FileFormat.SVG
javadoc {
ext.targetPath = 'javadoc/' + project.name
options.links << 'http://docs.oracle.com/javase/8/docs/api/'
}
task('deployJavadoc') {
dependsOn 'javadoc', 'setupDochostRemote'
group = 'Upload'
task('deployJavadoc', type: DeployDochost.class) {
dependsOn 'javadoc'
description = 'Uploads javadoc files to dochost.'
remote = remotes.deployRemote
}
deployJavadoc.doLast {
println "\nConnecting to ${remotes.dochost.host}..."
ssh.run {
session(remotes.dochost) {
def remoteTargetDir = setupDochostRemote.deployPath + javadoc.targetPath
// create and clean target directory on remote
execute "mkdir -p ${remoteTargetDir}"
execute "rm -rf ${remoteTargetDir}/*"
javadoc.destinationDir.eachFile {
put from: it, into: remoteTargetDir
}
}
deployJavadoc.sessionClosure = {
def remoteTargetDir = remotes.paths[deployJavadoc.remote] + 'javadoc/' + project.name
// create and clean target directory on remote
execute "mkdir -p ${remoteTargetDir}"
execute "rm -rf ${remoteTargetDir}/*"
javadoc.destinationDir.eachFile { file ->
put from: file, into: remoteTargetDir
}
}
}
......
......@@ -9,8 +9,8 @@ public class ZmtLibraryPlugin implements Plugin<Project> {
private static final def CONFIGURE = {
apply(plugin: ZmtBasePlugin)
apply(plugin: ZmtJavaPlugin)
apply(plugin: ZmtJavadocPlugin)
apply(plugin: ZmtDochostPlugin)
apply(plugin: ZmtJavadocPlugin)
apply(plugin: ZmtPublishPlugin)
apply(plugin: ZmtEclipsePlugin)
}
......
......@@ -2,6 +2,9 @@ package de.zmt.gradle;
import org.gradle.api.*
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.api.artifacts.repositories.PasswordCredentials
import java.lang.UnsupportedOperationException
import de.zmt.gradle.task.DeployDochost
/**
* Custom plugin for publishing ZMT projects
......@@ -12,11 +15,9 @@ class ZmtPublishPlugin implements Plugin<Project> {
private static final def CONFIGURE = {
apply plugin: 'maven-publish'
publish.dependsOn determineCredentials
publishing {
publications {
mavenJava(MavenPublication) {
java(MavenPublication.class) {
from components.java
// publish sources as well
......@@ -44,12 +45,16 @@ class ZmtPublishPlugin implements Plugin<Project> {
}
}
}
repositories {
maven {
def host = getProperty(ZmtDochostPlugin.PUBLIC_HOST_PROPERTY_NAME)
def path = getProperty(ZmtDochostPlugin.PUBLIC_PATH_PROPERTY_NAME)
url "sftp://${host}:22${path}maven2"
def remote = remotes.publishRemote
def path = remotes.paths[remote]
name = remote.name
url "sftp://${remote.host}:22${path}maven2"
credentials {
DeployDochost.determineCredentials(project.credentials)
username project.credentials.dochostUser
password project.credentials.dochostPw
}
......
......@@ -10,8 +10,8 @@ public class ZmtSimulationPlugin implements Plugin<Project> {
private static final def CONFIGURE = {
apply(plugin: ZmtBasePlugin)
apply(plugin: ZmtJavaPlugin)
apply(plugin: ZmtJavadocPlugin)
apply(plugin: ZmtDochostPlugin)
apply(plugin: ZmtJavadocPlugin)
apply(plugin: ZmtLaunchPlugin)
apply(plugin: ZmtDeployPlugin)
apply(plugin: ZmtEclipsePlugin)
......
package de.zmt.gradle.task;
import org.gradle.api.*;
import org.gradle.api.tasks.*;
import groovy.swing.*
public class DeployDochost extends DefaultTask {
def remote
/** Closure which is called inside ssh session. */
Closure sessionClosure
public DeployDochost() {
group = 'Upload'
}
@TaskAction
public void deploy() {
println "\nConnecting to ${remote}..."
project.ssh.run {
settings {
determineCredentials(project.credentials)
user = project.credentials.dochostUser
password = project.credentials.dochostPw
fileTransfer = 'scp'
}
session(remote) {
execute('uname -a')
sessionClosure.delegate = delegate
sessionClosure.call()
}
}
}
/**
* Determines credentials via console or dialog if they have not been
* set before.
* @param credentials the credentials object
*/
public static void determineCredentials(def credentials) {
// do nothing if credentials have been already determined
if (credentials.dochostUser && credentials.dochostPw) {
return
}
def console = System.console()
// we can access console: ask for username / password
if (console) {
console.printf("\nEnter username: ")
credentials.dochostUser = console.readLine()
console.printf("\nEnter password: ")
// readPassword() returns char array, wrap it into string
credentials.dochostPw = new String(console.readPassword())
}
// no console: create dialog and get it from there
else {
// https://www.timroes.de/2014/01/19/using-password-prompts-with-gradle-build-files/
new SwingBuilder().edt {
dialog(
modal: true, // Otherwise the build will continue running before you closed the dialog
title: 'Connecting...', // Dialog title
alwaysOnTop: true, // pretty much what the name says
resizable: false, // Don't allow the user to resize the dialog
locationRelativeTo: null, // Place dialog in center of the screen
pack: true, // We need to pack the dialog (so it will take the size of it's children)
show: true // Let's show it
) {
vbox {
label(text: "username: ")
userInput = textField(text: System.getProperty("user.name"))
userInput.selectAll()
label(text: "password: ")
pwInput = passwordField()
button(defaultButton: true, text: 'OK', actionPerformed: {
credentials.dochostUser = userInput.text
credentials.dochostPw = pwInput.text
dispose() // Close dialog
})
}
}
}
}
println "Use task 'addCredentials' with keys 'dochostUser' and 'dochostPw' if you want to store credentials permanently."
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment