Gradle task - pass arguments to Java application

121,268

Solution 1

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

Solution 2

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']"

Solution 3

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

run {
    args = ["--myarg1", "--myarg2"]
}

Solution 4

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

Solution 5

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

Share:
121,268
RecuencoJones
Author by

RecuencoJones

Updated on June 26, 2020

Comments

  • RecuencoJones
    RecuencoJones about 4 years

    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.

  • will
    will over 7 years
    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.
  • Krease
    Krease over 6 years
    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?
  • RecuencoJones
    RecuencoJones almost 6 years
    Is the ' expected or a typo? Should all arguments be passed as a string delimited by single quotes?
  • Drew Stephens
    Drew Stephens almost 6 years
    @RecuencoJones Fixed per docs.gradle.org/current/userguide/…
  • Jim Flood
    Jim Flood almost 6 years
    gradle run --args='foo --bar'
  • Sandeep
    Sandeep almost 6 years
    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
  • Eric
    Eric almost 6 years
    'foo --bar' is confusing, why not just use 'foo bar'.
  • Joffrey
    Joffrey over 5 years
    @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.
  • Admin
    Admin almost 5 years
    If you have args defined in your run block, per Claudio's answer below, then they will all be overridden by your --args flag. In other words, gradle doesn't try to merge the two.
  • Big Rich
    Big Rich over 4 years
    I'm using the id 'application' plugin and this was the answer I needed (it works).
  • keita063
    keita063 over 3 years
    If you face issues with gradle run --args='foo --bar', use " " quotation marks instead of ' '. For instance : gradle run --args="arg1 arg2".
  • Henning
    Henning over 2 years
    I am getting "unresolved reference: args". Gradle 7.3.