Problems passing system properties and parameters when running Java class via Gradle

gradle pass jvm arguments command line
gradle pass system property to test
gradle set system property
gradle set system property in task
system properties getproperty gradle
gradle pass arguments to task
gradle test pass parameters
gradle system.getproperty returns null

I am trying to run a command-line Java app via Gradle as part of a quick integration test. I am porting my build scripts from Maven, where this was easily done via exec-maven-plugin. My two big requirements are:

  • Being able to pass system properties to the executable Java code
  • Being able to pass command-line args to the executable Java code

Please note that I am not trying to read these properties in the build script, I'm trying to read them in the Java program that the script builds and executes.

I have found two other SO posts that address Java program execution via Gradle: one with an answer that advocates using apply plugin: "application" in the build file and gradle run at the command line, and another with answers advocating that approach as well as using task execute(type:JavaExec) in the build file and gradle execute at the command line. I have tried both approaches and have not succeeded.

I have two problems:

(1) I cannot get the Java executable to read the system properties

Whether I do this:

build.gradle:

apply plugin: 'application'
mainClassName = "com.mycompany.MyMain"

Command line:

gradle run -Dmyproperty=myvalue

Or this:

build.gradle:

task execute (type:JavaExec) {
    main = "com.mycompany.MyMain"
    classpath = sourceSets.main.runtimeClasspath 
}

Command line:

gradle execute -Dmyproperty=myvalue

In either case, myproperty does not make it through. The code that begins running from MyMain.main (...) reads the myproperty system property as null/missing.

(2) I cannot pass command line arguments

This is probably related to the first problem. In exec-maven-plugin, for example, command line args were themselves passed in via a system property. Is that the case with Gradle, or is there another way to pass command line arguments?

How do I get these variables through? Also, is it better to use apply plugin: 'application' or task execute (type:JavaExec)?

Figured it out. The main issue is that when Gradle forks a new Java process, it does not automatically pass the environment variable values along to the new environment. One has to explicitly pass these variables via the systemProperties property of the task or plugin.

The other issue was understanding how to pass command-line args; these are via the args property on the task or plugin. As with the Maven exec-maven-plugin, they should be passed in on the command line via yet another system property, as a space-delimited list that then needs to be split() before setting args, which accepts List objects. I've named the property exec.args, which is the old Maven name.

It seems both the javaExec and application plugin approach are valid. One might favor the application plugin approach if one wants to use some of its other features (automatically putting together a distribution, etc.)

Here are the solutions:

JavaExec Approach

Command Line:

gradle execute -Dmyvariable=myvalue -Dexec.args="arg1 arg2 arg3"

build.gradle:

task execute (type:JavaExec) {

    main = "com.myCompany.MyMain"
    classpath = sourceSets.main.runtimeClasspath 

    /* Can pass all the properties: */
    systemProperties System.getProperties()

    /* Or just each by name: */
    systemProperty "myvariable", System.getProperty("myvariable")

    /* Need to split the space-delimited value in the exec.args */
    args System.getProperty("exec.args", "").split()    
}
Application Plugin Approach

Command Line:

gradle run -Dmyvariable=myvalue -Dexec.args="arg1 arg2 arg3"

build.gradle:

apply plugin: 'application'
mainClassName = "com.mycompany.MyMain"
run {    
    /* Can pass all the properties: */
    systemProperties System.getProperties()

    /* Or just each by name: */
    systemProperty "myvariable", System.getProperty("myvariable")

    /* Need to split the space-delimited value in the exec.args */
    args System.getProperty("exec.args", "").split()    
}

How should I be handling passing system properties from Gradle to , I have a Selenium test framework that I am using with Gradle. In my BaseTest class, which all my other tests inherit from, I define a number of system properties so I can run the tests on different web servers, browsers, selenium servers, etc. about every system parameter that I might need to override at some point? I can't � Passing System Properties and Arguments With Gradle When using the Gradle application plugin, Gradle spawns a new JVM on the fly, and does not pass the System Properties or Command-line arguments to the new Java process. This post explains how to pass the System properties and command-line arguments when using Gradle application plugin.

For those who might not want to pollute your application's system properties by passing unrelated Gradle props, I recommend namespacing your arguments.

tasks.withType(JavaExec) {
    System.properties.each { k,v->
        if (k.startsWith("prefix.")) {
            systemProperty k - "prefix.", v
        }
    }
}

java ... -Dprefix.my.prop=true will pass my.prop

Passing gradle project properties via command line to , I have a problem executing the tests because I need to pass in some user credentials (the Passing gradle project properties via command line to GradleRunner? I am passing parameters with -D instead of -P you have used. { jcenter() } } apply plugin: "java" test { //systemProperty username = System. Here's a variant that passes numerous project properties to the test JVM as system properties. I prefer project properties over system properties to increase flexibility. task intTest(type: Test) { systemProperties project.properties.subMap(["foo", "bar"]) } Which may be passed on the command-line: $ gradle intTest -Pfoo=1 -Pbar=2

I'm new to gradle so I needed this and what is working for me with gradle 4.6 seems a little easier for the command line. Instead of parsing 1 arg string you can pass an array of args, and I found a way to pass in all property with one line as well. Combined below:

apply plugin: 'java'
apply plugin: 'org.springframework.boot'    <- for my project

task runApp(type: JavaExec) {
  classpath = sourceSets.main.runtimeClasspath

  main = 'testit.TomcatApp'

  // arguments to pass to the application
  //  args 'myarg1 -rest'    <- came in as 1 string

  args = ["--myarg1 with spaces even", "--myarg2"]

  // and to pass in all -D system property args:
  systemProperties = System.properties
}

gradle run -Dwhatever=xxx -Dmyarg2=hey

// Java reading them:
public static void main(String[] args) {
    for ( int i = 0; i < args.length; i++ )
        {
        logger.info( "** args [" + i + "] =" + args[i] + "=" );
        }
    logger.info( "** -Dwhatever =" + System.getProperty("whatever") + "=" );
    logger.info( "** -Dmyarg2 =" + System.getProperty("myarg2") + "=" );

[main] INFO testit.TomcatApp - ** args [0] =--myarg1 with spaces even=
[main] INFO testit.TomcatApp - ** args [1] =--myarg2=
[main] INFO testit.TomcatApp - ** -Dwhatever =xxx=
[main] INFO testit.TomcatApp - ** -Dmyarg2 =hey=

On Windows, system properties with quotes and spaces are not , This is a problem I noticed in Spring Boot Gradle plugin when calling The task fails to run with an exception that it did not find the main class "Corporation", which system properties and set them with - D parameters, but not in quotes, task Exception when passing outer system properties into JavaExec� gradle run -Pmyargs=-d,s 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

Maybe I am late for the party, but has anyone tried with "set the prop before executing gradle"? I have tested and this works too, apparently.

myVar=myVal gradle test

For example, you can set the active profile like:

SPRING_PROFILES_ACTIVE=dev  gradle test

These also work, apparently:(tested)

set myVar=myVal && gradle test      # for windows
export myVar=myVal && gradle test   # for linux and mac

Be wary that myVar cannot be period-separated; or else only the part before the first period will be taken as key.

Gradle Goodness: Pass Java System Properties To Java Tasks , If we have tasks in our projects that need to execute a Java application we can use the JavaExec task. When we need to pass Java system� Problems passing system properties and parameters when running Java class via Gradle. 28. What is the gradle equivalent of maven's exec plugin for running Java apps? 1.

Sbt Run Pass Jvm Arguments, Using named parameters, you can set a value to prefix separately from the vararg . To pass Java arguments to Jenkins, you need to change the Jenkins service configuration file. To pass argument to gradle run task main class. Because you're not forking this mutates the state of the system property in the JVM running � Although these system properties provided to the Gradle build via the -D option are always available to the Gradle build via the normal Java mechanism for obtaining system properties, Gradle makes

Gradle for DevOps, Part 2 - Project parameters, The java.lang package is imported by default in the Gradle build file, so you can use line option you can pass a system property to the JVM which runs Gradle. Using Gradle project properties is probably is the most convenient way to line: 2 * What went wrong: A problem occurred evaluating root project 'gradle'. Defining properties via system properties. We can also use Java system properties to define properties for our Gradle build. We use the -D command-line option just like in a normal Java application. The name of the system property must start with org.gradle.project, followed by the name of the property we want to set, and then by the value.

Spring Boot Gradle Plugin Reference Guide, It allows you to package executable jar or war archives, run Spring For example, when the java plugin is applied a task for building an The main class can also be configured explicitly using the task's Since bootRun is a standard JavaExec task, system properties can be passed to the application's� Answers: 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

Comments
  • I think that should be System.getProperties() (capital S).
  • If you are using gradle 2.xx, you can also use the following: - systemProperty "myvariable", "${myvariable}"
  • Thank you! I put was trying to run Cucumber tests and wanted to pass environment variables to the JVM. I just put "systemProperties System.getProperties()" in test { } instead of in run { }
  • Please note that default split doesn't allow arguments with spaces, e.g. -Dexec.args="--greeting 'hello there'" – won't work, need to set another delimiter.
  • Use: args System.getProperty("exec.args", "").split() to avoid null pointer exception if no args are supplied. For example, even 'gradle tasks' will throw an exception with the suggested build.gradle