16

I've got a java and groovy classes that are being run by gradle task. I have managed to make it work but I do not like the way I have to pass the parameters in command line. Here is how I do it currently via command line: gradle runTask -Pmode"['doStuff','username','password']"
my build.gradle code which takes these parameters looks like this:

if (project.hasProperty("mode")) {
args Eval.me(mode)}

and then I use my arguments/parameters in my java code as follows:

String action = args[0]; //"doStuff"
String name = args[1]; .. //"username"

I was wondering is there a way to pass the parameters in a better way such as:

gradle runTask -Pmode=doStuff -Puser=username -Ppass=password 

and how to use them in my java classes.

Tomas
  • 1,131
  • 2
  • 12
  • 25
  • I assume these java classes are part of a project code, which is run by gradle, am I correct? How does your task run them? – AdamSkywalker Jan 19 '16 at 11:42
  • @Adam Yes you're right, my task run them using javaexec and it runs main() method in one of the java classes – Tomas Jan 19 '16 at 11:43
  • ok, I understood the problem, wait a few minutes for an answer) – AdamSkywalker Jan 19 '16 at 11:44
  • 1
    Command line arguments passed to `main` are not a map. What you already have is the common way of working with them. It looks like you're trying to use them like system properties, which are intentionally different. Related question: http://stackoverflow.com/questions/11696521 – blgt Jan 19 '16 at 11:48
  • @blgt so this means that my way(using an array of parameters) is the only way to pass parameters via command line in this situation? I mean I can use it my but I was looking for a better way than this as it is quite open for human error with all these quote-marks and brackets – Tomas Jan 19 '16 at 11:53
  • @Tomas WEll gradle is a build tool, so you don't generally expect to be passing a ton of command line args to/via it. `JavaExec` might be there to help you but in this case it looks more like it's forcing you into a box – blgt Jan 19 '16 at 12:00
  • @blgt Thanks for your time, JavaExec indeed helped as posted in answer by AdamSkyWalker – Tomas Jan 19 '16 at 12:03

3 Answers3

22

JavaExec may be the way to go. Just declare a task and pass project parameters to java app:

task myExecTask(type: JavaExec) {
   classpath = sourceSets.main.runtimeClasspath
   main = 'com.project.MyApplicationMainClass' 
   args project.getProperty('userName') + ' ' + project.getProperty('password');
}

To run it, simply write gradle myExecTask -PuserName=john -Ppassword=secret

AdamSkywalker
  • 11,408
  • 3
  • 38
  • 76
  • Brilliant, worked like a charm and just what I needed, thank you Sir! – Tomas Jan 19 '16 at 12:02
  • any ideas about this? What if I want to run that same task but without getting some of the properties, e.g I want to run the task just with `gradle myExecTask -Pmode=doStuff ` and not `gradle myExecTask -Pmode=doStuff -PuserName=john -Ppassword=secret` , it throws me and error saying that properties 'usernName' and 'password' were not found.. – Tomas Jan 19 '16 at 13:56
  • 1
    @Tomas yes, gradle throws annoying exceptions when property is not present. The only way to do avoid it is to first call project.hasProperty('name') to check if property was set. – AdamSkywalker Jan 19 '16 at 14:06
  • Yeah I went down that path and managed to sort that out now, cheers – Tomas Jan 19 '16 at 14:07
  • 1
    Shouldnt the parameters be separated by commas instead of spaces? i.e. `args project.getProperty('userName'), project.getProperty('password');` instead of `args project.getProperty('userName') + ' ' + project.getProperty('password');` – Ritesh Tendulkar Jul 15 '16 at 12:35
  • 1
    @RiteshTendulkar you are right, args parameter in JavaExec task has Iterable type, so using comma is more correct. Spaces work too because at the end iterable params become a string. – AdamSkywalker Jul 15 '16 at 13:05
0

This is working for me:

task myExecTask(type: JavaExec) {
  classpath = sourceSets.main.runtimeClasspath
  main = 'com.project.MyApplicationMainClass' 
  args(user, pass); // no need to access user and pass via project.getProperty()
}
  • args needs to be built as a List of Strings for java main to use.
  • args now should be in the form of : ['myusername', 'mypassword']
Shaun
  • 308
  • 2
  • 10
0

this is the task I had to create for passing arguments through gradle task

task cucumber() {
    dependsOn assemble, compileTestJava
    doLast {
        javaexec {
            systemProperties = [
                    usr: project.getProperty('usr'),
                    pwd: project.getProperty('pwd')
            ]
            main = "io.cucumber.core.cli.Main"
            classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
            args = ['--plugin', 'pretty', '--glue', 'location to step def', 'location to feature files']
        }
    }
}

in order to execute test $ gradle cucumber -Pusr=<username> -Ppwd=<password>

to access args in your code System.getProperty("usr"), System.getProperty("pwd")

RamPrakash
  • 1,687
  • 3
  • 20
  • 25
frknio
  • 1