Execute command line options in the startup task

stack overflow

I started using some information from the proposed solution from the answer above: Approach to the application plugin

(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() } 

Command line:

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

it works just as intended, but seems to have a side effect. It makes sense to pass command line arguments to run, but I have to pass them for each task, for example:

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

If i leave

 -Dexec.args="arg1 arg2 arg3" 

I get

 "build failed with an exception" Where:path\build.gradle line:18 which if where my run{ } is. 
+7
gradle
source share
1 answer

You can solve this in two ways:

First

exec.args property can be read directly in the main class, so there is no need to configure args to run close at all.

Second

Just if it is:

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

Edited Asker Question: using if, but I had to change the syntax a bit.

 if(System.getProperty("exec.args") != null) { args System.getProperty("exec.args").split() } 
+5
source share

All Articles