Search code examples
gradlegradle-2

Pass command line arguments to gradle


To start Java program, I can pass arguments like:

java Main arg1 arg2 arg3

What are the good ways to do that in gradle command line:

gradle startProgram arg1 arg2 arg3

And this is in build.gradle:

task startProgram(dependsOn: 'classes', type: JavaExec) {
    main = 'Main'
    classpath = sourceSets.main.runtimeClasspath
    systemProperties = System.properties
}

Solution

  • Best way is to use java system properties (-D switch) but these are more 'global'. Instead You can use simple properties (-P switch) and get the passed values using instance of Project class.