October 8, 2010

Gradle Goodness: Pass Command-line Arguments to Build Script

The Gradle command-line parser will add all non-option arguments as tasks to be executed to the build. So if we want to pass an extra argument to our build script via the command-line we must use the option -P or --project-prop. With this option we can set a project property and then use it in the build script.

// File: build.gradle
task showArgs << {
    println "$word1 $word2"

We can run the script:

$ gradle showArgs -Pword1=hello --project-prop word2=world
hello world