如何在运行gradle任务时在命令行中传递多个参数? [英] How to pass multiple parameters in command line when running gradle task?
问题描述
我有一个由gradle任务运行的java和groovy类。我设法使它工作,但我不喜欢我必须在命令行中传递参数的方式。以下是我现在通过命令行执行的操作: gradle runTask -Pmode['doStuff','username','password']
我的build.gradle代码接受这些参数如下所示:
if(project.hasProperty(mode)){
args Eval.me(mode)}
然后我在我的参数/参数java代码如下:
String action = args [0]; //doStuff
String name = args [1]; .. //用户名
我想知道有没有办法将参数传递给更好例如:
gradle runTask -Pmode = doStuff -Puser = username -Ppass = password
$ c
以及如何在我的java类中使用它们。
解决方案 JavaExec 可能是一条可行的路。只需声明一个任务并将项目参数传递给Java应用程序:
任务myExecTask(类型:JavaExec){
classpath = sourceSets.main.runtimeClasspath
main ='com.project.MyApplicationMainClass'
args project.getProperty('userName')+''+ project.getProperty('password');
}
要运行它,只需编写 gradle myExecTask -PuserName = john -Ppassword = secret
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.
解决方案 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
这篇关于如何在运行gradle任务时在命令行中传递多个参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!