运行gradle任务时如何在命令行中传递多个参数?

时间:2022-07-13 15:16:04

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:

我有一个由gradle任务运行的java和groovy类。我已设法使其工作,但我不喜欢我必须在命令行中传递参数的方式。以下是我目前通过命令行执行的操作:gradle runTask -Pmode“['doStuff','username','password']”我的build.gradle代码采用这些参数如下所示:

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

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

然后我在我的java代码中使用我的参数/参数,如下所示:

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.

以及如何在我的java类中使用它们。

1 个解决方案

#1


11  

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

JavaExec可能是最佳选择。只需声明一个任务并将项目参数传递给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

要运行它,只需写入gradle myExecTask -PuserName = john -Ppassword = secret

#1


11  

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

JavaExec可能是最佳选择。只需声明一个任务并将项目参数传递给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

要运行它,只需写入gradle myExecTask -PuserName = john -Ppassword = secret