如何将参数从命令行传递给gradle [英] How to pass arguments from command line to gradle

查看:2064
本文介绍了如何将参数从命令行传递给gradle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从命令行向java类传递一个参数。我跟着这个帖子: HTTP://gradle.1045684。 n5.nabble.com/Gradle-application-plugin-question-td5539555.html ,但代码不适用于我(可能它不适用于JavaExec?)。下面是我试过的:

pre $ task listTests(type:JavaExec){
main =util.TestGroupScanner
classpath = sourceSets.util.runtimeClasspath
//这个可以工作...
args'demo'
/ *
//这不起作用!
if(project.hasProperty(group)){
args group
}
* /
}

上述硬编码参数值的输出为:

  C:\ws\svn\sqe\sandbox\selenium2forbg\testgradle>克listTests 
:compileUtilJava UP-TO-DATE
:processUtilResources UP-TO-DATE
:utilClasses UP-TO-DATE
:listTests
收到的参数:demo

建立成功

总时间:13.422秒

但是,一旦我将代码更改为使用hasProperty部分并将demo作为参数传递给命令行,得到一个NullPointerException:

  C:\ws\svn\sqe\sandbox\selenium2forbg\testgradle>克listTests -Pgroup = demo -s 

失败:构建失败,出现异常。

*其中:
构建文件'C:\ws\svn\sqe\sandbox\selenium2forbg\testgradle\build.gradle'行:25

*出错:
评估根项目'testgradle'时发生问题。
> java.lang.NullPointerException(无错误消息)

*尝试:
使用--info或--debug选项运行以获取更多日志输出。

*例外情况是:
org.gradle.api.GradleScriptException:评估根项目
'testgradle'时发生问题。
在org.gradle.groovy.scripts.internal.DefaultScriptRunnerFactory $ ScriptRunnerImpl.run(DefaultScriptRunnerFactory.java:54)
在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 = hasProperty('group')? group:'nosuchgroup'

关于如何将命令行参数传递给gradle的指针表示赞赏。 >

解决方案

project.group 是一个预定义的属性。使用 -P ,您只能设置预定义 的项目属性。或者,您可以设置Java系统属性( -D )。


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
    }
    */
}

The output from the above hard coded args value is:

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

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:\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) 

There is a simple test project available at http://gradle.1045684.n5.nabble.com/file/n5709919/testgradle.zip that illustrates the problem.

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' 

Any pointers on how to pass command line arguments to gradle appreciated.

解决方案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆