How to pass arguments from command line to gradle

I am trying to pass an argument from the command line to a java class. I followed this post: http://gradle.1045684.n5.nabble.com/Gradle-application-plugin-question-td5539555.html , but the code does not work for me (maybe it is not intended for JavaExec?). Here is what I tried:

task listTests(type:JavaExec){ main = "util.TestGroupScanner" classpath = sourceSets.util.runtimeClasspath // this works... args 'demo' /* // this does not work! if (project.hasProperty("group")){ args group } */ } 

The output from the above value of the argument is:

 C:\ws\svn\sqe\sandbox\selenium2forbg\testgradle>g listTests :compileUtilJava UP-TO-DATE :processUtilResources UP-TO-DATE :utilClasses UP-TO-DATE :listTests Received argument: demo BUILD SUCCESSFUL Total time: 13.422 secs 

However, as soon as I changed the code to use the hasProperty section and passed "demo" as an argument on the command line, I get a NullPointerException:

 C:\ws\svn\sqe\sandbox\selenium2forbg\testgradle>g listTests -Pgroup=demo -s FAILURE: Build failed with an exception. * Where: Build file 'C:\ws\svn\sqe\sandbox\selenium2forbg\testgradle\build.gradle' line:25 * What went wrong: A problem occurred evaluating root project 'testgradle'. > java.lang.NullPointerException (no error message) * Try: Run with --info or --debug option to get more log output. * Exception is: org.gradle.api.GradleScriptException: A problem occurred evaluating root project 'testgradle'. at org.gradle.groovy.scripts.internal.DefaultScriptRunnerFactory$ScriptRunnerImpl.run(DefaultScriptRunnerFactory.java:54) at org.gradle.configuration.DefaultScriptPluginFactory$ScriptPluginImpl.apply(DefaultScriptPluginFactory.java:127) at org.gradle.configuration.BuildScriptProcessor.evaluate(BuildScriptProcessor.java:38) 

There is a simple test project at http://gradle.1045684.n5.nabble.com/file/n5709919/testgradle.zip that illustrates the problem.

Used Gradle 1.0-rc-3. NullPointer from this line of code:

 args group 

I added the following task before the task definition, but it did not change the result:

 group = hasProperty('group') ? group : 'nosuchgroup' 

Any pointers on how to pass command line arguments to Gradle.

+86
gradle
Jul 27 '12 at 22:34
source share
7 answers

project.group is a predefined property. With -P you can only set project properties that are not predefined. In addition, you can set the properties of the Java ( -D ) system.

+58
Jul 27 '12 at 22:50
source share

Based on Peter N.'s answer, this is an example of how to add (optional) user-specified arguments to go to Java main for the JavaExec task (since you cannot manually set the "args" property for the reason it cites.)

Add this to the task:

 task(runProgram, type: JavaExec) { [...] if (project.hasProperty('myargs')) { args(myargs.split(',')) } 

... and run on the command line like this

 % ./gradlew runProgram '-Pmyargs=-x,7,--no-kidding,/Users/rogers/tests/file.txt' 
+51
Oct 08 '13 at
source share

My program with two arguments, args [0] and args [1]:

 public static void main(String[] args) throws Exception { System.out.println(args); String host = args[0]; System.out.println(host); int port = Integer.parseInt(args[1]); 

my build.gradle

 run { if ( project.hasProperty("appArgsWhatEverIWant") ) { args Eval.me(appArgsWhatEverIWant) } } 

my hint for terminal:

 gradle run -PappArgsWhatEverIWant="['localhost','8080']" 
+26
Mar 28 '15 at 17:22
source share

Starting with Gradle 4.9, the application plugin understands --args , so passing arguments is as simple as:

build.gradle

 plugins { id 'application' } mainClassName = "my.App" 

Src / main / java / mine /app.java

 public class App { public static void main(String[] args) { System.out.println(args); } } 

hit

 ./gradlew run --args='This string will be passed into my.Class arguments' 
+9
Nov 24 '18 at 23:32
source share

You can use custom command-line options in Gradle to end up with something like:

 /.gradlew printPet --pet="puppies!" 

However, custom command-line options in Gradle are an incubation function .

Java solution

To end with something like this, follow the instructions here :

 import org.gradle.api.tasks.options.Option; public class PrintPet extends DefaultTask { private String pet; @Option(option = "pet", description = "Name of the cute pet you would like to print out!") public void setPet(String pet) { this.pet = pet; } @Input public String getPet() { return pet; } @TaskAction public void print() { getLogger().quiet("'{}' are awesome!", pet); } } 

Then register this:

 task printPet(type: PrintPet) 

Now you can do:

 ./gradlew printPet --pet="puppies" 

exit:

Puppies! awesome!

Kotlin solution

 open class PrintPet : DefaultTask() { @Suppress("UnstableApiUsage") @set:Option(option = "pet", description = "The cute pet you would like to print out") @get:Input var pet: String = "" @TaskAction fun print() { println("$pet are awesome!") } } 

then register the task with:

 tasks.register<PrintPet>("printPet") 
+4
May 15 '19 at 9:53
source share

I wrote a piece of code that puts the command line arguments in the format expected by gradle.

 // this method creates a command line arguments def setCommandLineArguments(commandLineArgs) { // remove spaces def arguments = commandLineArgs.tokenize() // create a string that can be used by Eval def cla = "[" // go through the list to get each argument arguments.each { cla += "'" + "${it}" + "'," } // remove last "," add "]" and set the args return cla.substring(0, cla.lastIndexOf(',')) + "]" } 

my task looks like this:

 task runProgram(type: JavaExec) { if ( project.hasProperty("commandLineArgs") ) { args Eval.me( setCommandLineArguments(commandLineArgs) ) } } 

To pass arguments from the command line, do the following:

 gradle runProgram -PcommandLineArgs="arg1 arg2 arg3 arg4" 
+3
Sep 20 '17 at 18:51
source share

pass the url from the command line, saving your url in the app gradle file as follows resValue "string", "url", CommonUrl

and specify the parameter in the gradle.properties files as follows CommonUrl = "enter your url here or it may be empty"

and pass the command from the command line as follows gradle assembleRelease -Pcommanurl = post your URL here

-four
Apr 01 '16 at 7:16
source share



All Articles