Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass command line parameters into run task

Tags:

gradle

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.
like image 675
Travis Avatar asked Feb 11 '15 22:02

Travis


People also ask

How do I pass a command line argument to exe?

Right click Build Specifications in the Project Explorer of the completed project. Select new>>Application (EXE) Navigate to the Advanced category. Check the Pass all command line arguments to application box (Figure 3)

Is it possible to pass command line arguments to a test?

It is possible to pass custom command line arguments to the test module.


1 Answers

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()
}
like image 197
Opal Avatar answered Sep 21 '22 16:09

Opal