23

I was wondering if it's possible to set a system property, for a Java application, using Gradle?

I tried using gradle.properties file and defining a property as

systemProp.name = my name

but then when I try to get that property from a Java application using

System.getProperty("name")

that property is not found.

build.gradle and gradle.properties are in root folder of the project.

This is what my build.gradle looks like:

apply plugin: 'war'
apply plugin: 'appengine'

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'com.google.appengine:gradle-appengine-plugin:1.8.6'
    }
}

appengine {
    httpPort = 8081
}

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'com.google.inject', name: 'guice', version: '3.0'
    compile group: 'com.google.inject.extensions', name: 'guice-servlet', version: '3.0'
    compile group: 'javax.servlet', name: 'servlet-api', version: '2.5'
    compile group: 'org.hibernate', name: 'hibernate-entitymanager', version: '4.2.0.Final'
    compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.27'
    compile 'com.google.protobuf:protobuf-java:2.5.0'
    compile 'com.google.appengine:appengine-api-1.0-sdk:1.8.6'
    compile 'com.google.template:soy:2012-12-21'
    compile 'org.json:json:20090211'
}

And this is what my build.properties look like:

systemProp.firstName=Marko
systemProp.lastName=Vuksanovic

This is part of an AppEngine application and I run it using the following command:

gradle appengineRun
markovuksanovic
  • 15,676
  • 13
  • 46
  • 57

4 Answers4

14

This is how I do it, setting props for Selenide test framework:

test {
    systemProperty "browser", "chrome"
    systemProperty "webdriver.chrome.driver", "$projectDir/drivers/chromedriver"
}
djangofan
  • 28,471
  • 61
  • 196
  • 289
13

I just stumbled on this for use with the gradle application plugin. Add to the run task. Works great:

run {   
    systemProperties['MYPROP'] = '/my/prop/value'
}
Fred Toth
  • 213
  • 2
  • 11
  • 7
    This doesn't work for me, getting `> Could not find method run() for arguments [build_3q2qbgis9k2n97muogsnwks4i$_run_closure5@3172da27]` – birgersp Jun 02 '17 at 13:38
  • @birgersp The answer mentions `gradle application plugin`. Just add this plugin to your `build.gradle`. – VadimBelov Dec 12 '20 at 12:00
9

This is what worked for me (using Kotlin DSL):

tasks.withType<JavaExec> {
        systemProperty("key", "value")
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
shduke
  • 91
  • 1
  • 6
6

The system property set in gradle.properties will be available only in JVM where Gradle is running.

From gradle-appengine-plugin documentation:

appengineRun: Starts a local development server running your project code.

jvmFlags: The JVM flags to pass on to the local development server.

If you need your system properties to be available in app engine, which is separate JVM, you should use jvmFlags property.

Explicitly:

appengine {
    jvmFlags = ['-DfirstName=Marko', '-DlastName=Vuksanovic']
}

With gradle.properties:

appengine {
    jvmFlags = ['-DfirstName=$firstName', '-DlastName=$lastName']
}
kukido
  • 10,431
  • 1
  • 45
  • 52