How to pass multiple parameters in command line wh

2019-03-18 16:46发布

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.

1条回答
来,给爷笑一个
2楼-- · 2019-03-18 17:17

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

查看更多
登录 后发表回答