我试图将参数从命令行传递给Java类。我关注了这篇文章:http : //gradle.1045684.n5.nabble.com/Gradle-application-plugin-question-td5539555.html,但是该代码对我不起作用(也许不是JavaExec的意思?)。这是我尝试过的:
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值的输出为:
C:\ws\svn\sqe\sandbox\selenium2forbg\testgradle>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:
C:\ws\svn\sqe\sandbox\selenium2forbg\testgradle>g listTests -Pgroup=demo -s
FAILURE: Build failed with an exception.
* Where:
Build file 'C:\ws\svn\sqe\sandbox\selenium2forbg\testgradle\build.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来自以下代码行:
args group
我在任务定义之前添加了以下分配,但是并没有改变结果:
group = hasProperty('group') ? group : 'nosuchgroup'
任何有关如何将命令行参数传递给gradle的指针表示赞赏。