How to set system property using gradle?

Viewed 42292

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
4 Answers

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

tasks.withType<JavaExec> {
        systemProperty("key", "value")
}
Related