Gradle scripts are Groovy scripts. So we can use all the functionality of Groovy in our Gradle scripts. And because we can use Java classes as well, we can simply get user input values from the console using java.io.Console
. With the Console
class we can for example ask for a value to be used in our script. We can even ask for a password without the password being echoed on the console.
In the following sample build script we have a ask task that uses Console
to let the user enter a username and password value. In the print task we use the values that are entered to print them to the console.
def username def password task ask << { def console = System.console() if (console) { username = console.readLine('> Please enter your username: ') password = console.readPassword('> Please enter your password: ') } else { logger.error "Cannot get console." } } task print << { println "Hello $username! Your password is ${password.size()} characters." }
We can run the script (suppose we use help as password):
$ gradle -q ask print > Please enter your username: mrhaki > Please enter your password: Hello mrhaki! Your password is 4 characters.
Written with Gradle 0.9.