Java arguments to run with gradle run can be specified with --args (since Gradle 4.9)

Overview

--Command line arguments for Java applications run with gradle run can be specified with --args (available since Gradle 4.9 migration) --For example, you can set the parameter of the array of strings in the main method of Java main class by specifying gradle run --args = "aaa bbb ccc". --Before Gradle 4.9, it was dealt with by describing the pre-processing for executing the run task in build.gradle.

Description example

An example of specifying three arguments for aaa bbb ccc.

$ gradle run --args="aaa bbb ccc"

Official information

The Application Plugin

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 JavaExec.setArgsString(java.lang.String).

Operation check

Confirmed to work with OpenJDK 11.0.2 and Gradle 5.6.2.

Prepare build.gradle and App.java for execution.

$ tree
.
├── build.gradle
└── src
    └── main
        └── java
            └── com
                └── example
                    └── App.java

build.gradle file.

build.gradle


plugins {
  id 'java'
  id 'application'
}

repositories {
  mavenCentral()
  jcenter()
}

application {
  mainClassName = 'com.example.App'
}

App.java file.

package com.example;

public class App {
  public static void main(String[] args) {
    System.out.println("args.length=" + args.length);
    for(String arg: args) {
      System.out.println(arg);
    }
  }
}

Run gradle run with no arguments.

$ gradle run

> Task :run
args.length=0

BUILD SUCCESSFUL in 1s
2 actionable tasks: 2 executed

Run gradle run with arguments.

$ gradle run --args="aaa bbb ccc"

> Task :run
args.length=3
aaa
bbb
ccc

BUILD SUCCESSFUL in 1s
2 actionable tasks: 1 executed, 1 up-to-date

Workaround before Gradle 4.9

Add the process when the run task is executed to build.gradle.

build.gradle


run {
  //Pass the args property to args of the run task if it exists
  if (project.hasProperty('args')) {
    //Divide by half-width white space and pass
    args(project.args.split('\\s+'))
  }
}

If you specify the args property when running gradle run, it will be passed as an argument of the Java application.

$ gradle run -Pargs="aaa bbb ccc"

> Task :run
args.length=3
aaa
bbb
ccc

BUILD SUCCESSFUL in 1s
2 actionable tasks: 1 executed, 1 up-to-date

Reference material

Recommended Posts

Java arguments to run with gradle run can be specified with --args (since Gradle 4.9)
Introduction to Java that can be understood even with Krillin (Part 1)
Using Gradle with VS Code, build Java → run
[Java] char type can be cast to int type
Connect to Access database with Java [UCan Access] * Set until it can be executed with VS Code
Only the top level Stream can be parallelized with Java Stream.
Be sure to compare the result of Java compareTo with 0
Whether options can be used due to different Java versions
Run batch with docker-compose with Java batch
Java to play with Function
Connect to DB with Java
Connect to MySQL 8 with Java
Run Java VM with WebAssembly
Build a Java project with Gradle
Java to learn with ramen [Part 1]
How to run Ant in Gradle
Dare to challenge Kaggle with Java (1)
I tried to interact with Java
Java to be involved from today
Java, arrays to start with beginners
Run an application made with Java8 with Java6
[Java] Java was said to be okay to concatenate strings with +, so I checked