Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass multiple parameters in command line when running gradle task?

I've got a java and groovy classes that are being run by gradle task. I have managed to make it work but I do not like the way I have to pass the parameters in command line. Here is how I do it currently via command line: gradle runTask -Pmode"['doStuff','username','password']"
my build.gradle code which takes these parameters looks like this:

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

and then I use my arguments/parameters in my java code as follows:

String action = args[0]; //"doStuff"
String name = args[1]; .. //"username"

I was wondering is there a way to pass the parameters in a better way such as:

gradle runTask -Pmode=doStuff -Puser=username -Ppass=password 

and how to use them in my java classes.

like image 866
Tomas Avatar asked Jan 19 '16 11:01

Tomas


People also ask

How do you pass arguments in gradle task?

Here, we don't use properties to pass arguments. Instead, we pass the –args flag and the corresponding inputs there. This is a nice wrapper provided by the application plugin. However, this is only available from Gradle 4.9 onward.

How do I pass environment variables in gradle task?

If you are using an IDE, go to run, edit configurations, gradle, select gradle task and update the environment variables. See the picture below. Alternatively, if you are executing gradle commands using terminal, just type 'export KEY=VALUE', and your job is done. Save this answer.

How do you pass properties in gradle?

Using the -D command-line option, you can pass a system property to the JVM which runs Gradle. The -D option of the gradle command has the same effect as the -D option of the java command. You can also set system properties in gradle. properties files with the prefix systemProp.

How do I run a gradle task in CMD?

To run a Gradle command, open a command window on the project folder and enter the Gradle command. Gradle commands look like this: On Windows: gradlew <task1> <task2> … ​ e.g. gradlew clean allTests.


1 Answers

JavaExec may be the way to go. Just declare a task and pass project parameters to java app:

task myExecTask(type: JavaExec) {
   classpath = sourceSets.main.runtimeClasspath
   main = 'com.project.MyApplicationMainClass' 
   args project.getProperty('userName') + ' ' + project.getProperty('password');
}

To run it, simply write gradle myExecTask -PuserName=john -Ppassword=secret

like image 83
AdamSkywalker Avatar answered Sep 22 '22 13:09

AdamSkywalker