16
votes

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.

3
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
@Adam Yes you're right, my task run them using javaexec and it runs main() method in one of the java classes - Tomas
ok, I understood the problem, wait a few minutes for an answer) - AdamSkywalker
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: stackoverflow.com/questions/11696521 - blgt
@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

3 Answers

22
votes

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

0
votes

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']
0
votes

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")