如何将参数从命令行传递到 gradle [英] How to pass arguments from command line to gradle
问题描述
我正在尝试将参数从命令行传递到 Java 类.我关注了这篇文章:http://gradle.1045684.n5.nabble.com/Gradle-application-plugin-question-td5539555.html 但代码对我不起作用(也许它不适用于 JavaExec?).这是我尝试过的:
I'm trying to pass an argument from command line to a java class. I followed this post: http://gradle.1045684.n5.nabble.com/Gradle-application-plugin-question-td5539555.html but the code does not work for me (perhaps it is not meant for JavaExec?). Here is what I tried:
task listTests(type:JavaExec){
main = "util.TestGroupScanner"
classpath = sourceSets.util.runtimeClasspath
// this works...
args 'demo'
/*
// this does not work!
if (project.hasProperty("group")){
args group
}
*/
}
上述硬编码 args 值的输出为:
The output from the above hard coded args value is:
C:wssvnsqesandboxselenium2forbg estgradle>g listTests
:compileUtilJava UP-TO-DATE
:processUtilResources UP-TO-DATE
:utilClasses UP-TO-DATE
:listTests
Received argument: demo
BUILD SUCCESSFUL
Total time: 13.422 secs
但是,一旦我更改代码以使用 hasProperty 部分并在命令行上将demo"作为参数传递,我就会收到 NullPointerException:
However, once I change the code to use the hasProperty section and pass "demo" as an argument on the command line, I get a NullPointerException:
C:wssvnsqesandboxselenium2forbg estgradle>g listTests -Pgroup=demo -s
FAILURE: Build failed with an exception.
* Where:
Build file 'C:wssvnsqesandboxselenium2forbg estgradleuild.gradle' line:25
* What went wrong:
A problem occurred evaluating root project 'testgradle'.
> java.lang.NullPointerException (no error message)
* Try:
Run with --info or --debug option to get more log output.
* Exception is:
org.gradle.api.GradleScriptException: A problem occurred evaluating root project
'testgradle'.
at org.gradle.groovy.scripts.internal.DefaultScriptRunnerFactory$ScriptRunnerImpl.run(DefaultScriptRunnerFactory.java:54)
at org.gradle.configuration.DefaultScriptPluginFactory$ScriptPluginImpl.apply(DefaultScriptPluginFactory.java:127)
at org.gradle.configuration.BuildScriptProcessor.evaluate(BuildScriptProcessor.java:38)
http://gradle 上有一个简单的测试项目.1045684.n5.nabble.com/file/n5709919/testgradle.zip 说明了问题.
这是使用 Gradle 1.0-rc-3.NullPointer 来自这行代码:
This is using Gradle 1.0-rc-3. The NullPointer is from this line of code:
args group
我在任务定义之前添加了以下分配,但它没有改变结果:
I added the following assignment before the task definition, but it didn't change the outcome:
group = hasProperty('group') ? group : 'nosuchgroup'
有关如何将命令行参数传递给 gradle 的任何指示表示赞赏.
Any pointers on how to pass command line arguments to gradle appreciated.
推荐答案
project.group
是一个预定义的属性.使用-P
,您只能设置未预定义的项目属性.或者,您可以设置 Java 系统属性 (-D
).
project.group
is a predefined property. With -P
, you can only set project properties that are not predefined. Alternatively, you can set Java system properties (-D
).
这篇关于如何将参数从命令行传递到 gradle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!