From 813a9e1fbff9a4be474d9e0b75ea10467b559e54 Mon Sep 17 00:00:00 2001 From: Norman Walsh Date: Mon, 8 Jun 2015 07:17:09 +0100 Subject: [PATCH] Update build.gradle to use AWT to ask for passphrase --- build.gradle | 70 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 54 insertions(+), 16 deletions(-) diff --git a/build.gradle b/build.gradle index ced2b1f..6555b38 100644 --- a/build.gradle +++ b/build.gradle @@ -1,3 +1,6 @@ +import groovy.swing.SwingBuilder +import java.awt.Point + buildscript { repositories { mavenCentral() @@ -96,22 +99,6 @@ jar { } } -// I refuse to put my PGP password in a file, so there's no way to -// make this work from a CI environment. -gradle.taskGraph.whenReady { taskGraph -> - if (taskGraph.hasTask(":uploadArchives")) { - Console console = System.console() - console.printf "\n\nWe have to sign some things in this build." + - "\n\nPlease enter your signing details.\n\n" - - def password = console.readPassword("PGP Private Key Password: ") - - allprojects { ext."signing.password" = password } - - console.printf "\nThanks.\n\n" - } -} - task copyNotices(type: Copy) { from 'resources/notices' into 'build/dist/docs/notices' @@ -181,6 +168,57 @@ signing { sign configurations.archives } +// I refuse to put my PGP password in a file. +// +// Adapted from +// https://www.timroes.de/2014/01/19/using-password-prompts-with-gradle-build-files/ +// +// N.B. This can't work in a CI environment so there's no way to +// automatically publish the artifacts from, for example, Travis CI. +// +gradle.taskGraph.whenReady { taskGraph -> + if(taskGraph.hasTask(':uploadArchives')) { + + def pass = '' + if(System.console() == null) { + new SwingBuilder().edt { + dialog(modal: true, + title: 'Enter password', + alwaysOnTop: true, + resizable: false, + locationRelativeTo: null, + locationByPlatform: true, + // On my Linux multi-monitor setup, the dialog "appears" at + // the top of the virtual desktop below my desktop. So move + // the damned thing somewhere visible. Meh. + location: new Point(100,100), + pack: true, + show: true + ) { + vbox { // + label(text: "Please enter key passphrase:") + input = passwordField() + button(defaultButton: true, text: 'OK', actionPerformed: { + pass = input.password; + dispose(); + }) + } + } + } + } else { + pass = System.console().readPassword("\nPlease enter key passphrase: ") + pass = new String(pass) + } + + if (pass.size() <= 0) { + throw new InvalidUserDataException("You must enter a password to proceed.") + } + + allprojects { ext."signing.password" = pass } + + } +} + uploadArchives { repositories { mavenDeployer {