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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34875637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 23:29:25  来源:igfitidea点击:

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

javagradlebuild.gradle

提问by Tomas

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 类中使用它们。

回答by AdamSkywalker

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

JavaExec可能是要走的路。只需声明一个任务并将项目参数传递给 java 应用程序:

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

回答by Shaun

This is working for me:

这对我有用:

task myExecTask(type: JavaExec) {
  classpath = sourceSets.main.runtimeClasspath
  main = 'com.project.MyApplicationMainClass' 
  args(user, pass); // no need to access user and pass via project.getProperty()
}
  • args needs to be built as a List of Strings for java main to use.
  • args now should be in the form of : ['myusername', 'mypassword']
  • args 需要构建为字符串列表供 java main 使用。
  • args 现在应该采用以下形式:['myusername', 'mypassword']

回答by frknio

this is the task I had to create for passing arguments through gradle task

这是我必须为通过 gradle 任务传递参数而创建的任务

task cucumber() {
    dependsOn assemble, compileTestJava
    doLast {
        javaexec {
            systemProperties = [
                    usr: project.getProperty('usr'),
                    pwd: project.getProperty('pwd')
            ]
            main = "io.cucumber.core.cli.Main"
            classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
            args = ['--plugin', 'pretty', '--glue', 'location to step def', 'location to feature files']
        }
    }
}

in order to execute test $ gradle cucumber -Pusr=<username> -Ppwd=<password>

为了执行测试 $ gradle cucumber -Pusr=<username> -Ppwd=<password>

to access args in your code System.getProperty("usr"), System.getProperty("pwd")

访问代码中的参数 System.getProperty("usr"), System.getProperty("pwd")