157

I have a Java application that runs with a custom gradle task and the application requires some arguments upon being invoked. These are:

programName ( string | -f filename | -d key | -h)
Options:
    string         Message to be used.
    -d key         Use default messages, key must be s[hort], m[edium] or l[ong].
    -f filename    Use specified file as input.
    -h             Help dialog.

Gradle task looks like:

task run (type: JavaExec){
    description = "Secure algorythm testing"
    main = 'main.Test'
    classpath = sourceSets.main.runtimeClasspath
}

I've tried running gradle run -h and it does not work.

3
  • Did the answers fit your needs? If so, you should mark one as solution. Dec 23, 2014 at 14:58
  • 1
    Not really... a friend and I discovered a way to do it but we don't have it clear yet so as to publish a solution, both proposed solutions were tried, we understood what it'd to be done, but didn't really seem to work... Dec 25, 2014 at 1:33
  • @6uitar6reat6od How did you resolve it in the end? Also what version of gradle?
    – xlm
    Apr 1, 2015 at 4:04

8 Answers 8

128

Gradle 4.9+

gradle run --args='arg1 arg2'

This assumes your build.gradle is configured with the Application plugin. Your build.gradle should look similar to this:

plugins {
  // Implicitly applies Java plugin
  id: 'application'
}

application {
  // URI of your main class/application's entry point (required)
  mainClassName = 'org.gradle.sample.Main'
}

Pre-Gradle 4.9

Include the following in your build.gradle:

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

Then to run: gradle run -PappArgs="['arg1', 'args2']"

0
109

Since Gradle 4.9, the command line arguments can be passed with --args. For example, if you want to launch the application with command line arguments foo --bar, you can use

gradle run --args='foo --bar'

See Also Gradle Application Plugin

How to upgrade Gradle wrapper

7
  • 1
    Is the ' expected or a typo? Should all arguments be passed as a string delimited by single quotes? Jul 20, 2018 at 8:32
  • @RecuencoJones Fixed per docs.gradle.org/current/userguide/… Aug 2, 2018 at 13:23
  • 1
    gradle run --args='foo --bar'
    – Jim Flood
    Aug 5, 2018 at 16:38
  • 4
    'foo --bar' is confusing, why not just use 'foo bar'.
    – Eric
    Sep 7, 2018 at 7:37
  • 4
    @EricWang These are arbitrary command line arguments a program may need. It's nice to show that gradle supports any kind of arguments, since the raw string is passed to the built application.
    – Joffrey
    Nov 28, 2018 at 10:39
39

If you want to use the same set of arguments all the time, the following is all you need.

run {
    args = ["--myarg1", "--myarg2"]
}
3
  • 3
    Ok, for absolute beginners like me : in order to be able to define run task your build.gradle should contain following two lines: apply plugin:'application' mainClassName="<full classname including the package path>" Otherwise, you cannot define the run method in the buuild.gradle
    – Sandeep
    Aug 9, 2018 at 7:07
  • 1
    I'm using the id 'application' plugin and this was the answer I needed (it works).
    – Big Rich
    Oct 30, 2019 at 16:42
  • 1
    I am getting "unresolved reference: args". Gradle 7.3.
    – Henning
    Nov 28, 2021 at 16:12
29

Sorry for answering so late.

I figured an answer alike to @xlm 's:

task run (type: JavaExec, dependsOn: classes){
    if(project.hasProperty('myargs')){
        args(myargs.split(','))
    }
    description = "Secure algorythm testing"
    main = "main.Test"
    classpath = sourceSets.main.runtimeClasspath
}

And invoke like:

gradle run -Pmyargs=-d,s
6

You can find the solution in Problems passing system properties and parameters when running Java class via Gradle . Both involve the use of the args property

Also you should read the difference between passing with -D or with -P that is explained in the Gradle documentation

1
  • Saw this too. Still looking. All of these methods seem to want to edit/massage the current properties and pass them along. Command line and Java properties for running an application or service are akin to "Context" or "Configuration" setting. It would be better to have a plug-in that does things like "run parameters" as a side-by-side profiles or something.
    – will
    Dec 8, 2016 at 4:41
6

Of course the answers above all do the job, but still i would like to use something like

gradle run path1 path2

well this can't be done, but what if we can:

gralde run --- path1 path2

If you think it is more elegant, then you can do it, the trick is to process the command line and modify it before gradle does, this can be done by using init scripts

The init script below:

  1. Process the command line and remove --- and all other arguments following '---'
  2. Add property 'appArgs' to gradle.ext

So in your run task (or JavaExec, Exec) you can:

if (project.gradle.hasProperty("appArgs")) {
                List<String> appArgs = project.gradle.appArgs;

                args appArgs

 }

The init script is:

import org.gradle.api.invocation.Gradle

Gradle aGradle = gradle

StartParameter startParameter = aGradle.startParameter

List tasks = startParameter.getTaskRequests();

List<String> appArgs = new ArrayList<>()

tasks.forEach {
   List<String> args = it.getArgs();


   Iterator<String> argsI = args.iterator();

   while (argsI.hasNext()) {

      String arg = argsI.next();

      // remove '---' and all that follow
      if (arg == "---") {
         argsI.remove();

         while (argsI.hasNext()) {

            arg = argsI.next();

            // and add it to appArgs
            appArgs.add(arg);

            argsI.remove();

        }
    }
}

}


   aGradle.ext.appArgs = appArgs

Limitations:

  1. I was forced to use '---' and not '--'
  2. You have to add some global init script

If you don't like global init script, you can specify it in command line

gradle -I init.gradle run --- f:/temp/x.xml

Or better add an alias to your shell:

gradleapp run --- f:/temp/x.xml
1
  • 2
    This works great ... if none of my arguments start with a dash. This makes it useless for common command line parsers :(. As soon as that happens, gradle seems to treat that arg as an argument to gradle (I don't think the argsI.remove() is having the desired effect). Suggestions?
    – Krease
    Jan 22, 2018 at 18:42
4

You need to pass them as args to the task using project properties, something like:

args = [project.property('h')]

added to your task definition (see the dsl docs)

Then you can run it as:

gradle -Ph run
0

Using Kotlin DSL:

plugins {
    java
    application
}

application {
    mainClass = "io.fouad.AppLauncher"
    applicationDefaultJvmArgs = listOf("-Dsome.system.properties=123")
}

tasks.withType(JavaExec::class) {
    args = listOf("abc", "def")
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.