Travis
Travis

Reputation: 2145

Pass command line parameters into run task

https://stackoverflow.com/a/23689696/1757491

I started using some info from the proposed solution from the above answer: Application Plugin Approach

(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 great for its intended purpose but seems to have a side effect. It makes sense to pass in the command line arguments for run but I have to pass them in for every task for example:

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

If I leave out the

-Dexec.args="arg1 arg2 arg3"

I get

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

Upvotes: 7

Views: 13088

Answers (1)

Opal
Opal

Reputation: 84854

You can solve it a two different ways:

First:

exec.args property can be read directly in the main class - so there's no need to configure args in run closure at all.

Second:

Just if it:

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

Edited by Question Asker: using if does work but I had to change the syntax slightly.

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

Upvotes: 5

Related Questions