2015-02-11 33 views
7

https://stackoverflow.com/a/23689696/1757491传递命令行参数到运行任务

我开始使用一些信息从上面的回答所提出的解决方案: 应用程序插件方法

(的build.gradle)

apply plugin: 'application' 

mainClassName = "com.mycompany.MyMain" 
run { 
    /* Need to split the space-delimited value in the exec.args */ 
    args System.getProperty("exec.args").split()  
} 

命令Line:

gradle run -Dexec.args="arg1 arg2 arg3" 

it wor ks对于它的预期目的很好,但似乎有副作用。这是有道理的命令行参数运行通过,但我必须要通过他们在例如每一项任务:

gradle tasks -Dexec.args="arg1 arg2 arg3" 

如果我离开了

-Dexec.args="arg1 arg2 arg3" 

我得到

"build failed with an exception" 
Where:path\build.gradle line:18 which if where my run{ } is. 

回答

5

您可以通过两种不同的方式解决:

第一个

exec.args属性可以在主类中直接读取 - 因此无需在run关闭中配置args

只是如果它:

execArgs = System.getProperty('exec.args') 
if(execArgs)  
    args = execArgs.split() 

的问题提问者编辑: 使用,如果不工作,但我不得不稍微改变语法。

if(System.getProperty("exec.args") != null) { 
    args System.getProperty("exec.args").split() 
}