gmacster
gmacster

Reputation: 403

Gradle.properties not passed through to my Java application

Issue

I have a project-level gradle.properties file that defines various systemProp.* values (especially proxy stuff). I got this idea from the documentation.

I'm expecting those properties to be passed through to the JVM that runs my application, so that my application can use those values at runtime.

However, it looks like none of those systemProp values are passed through.

Am I misunderstanding how this is supposed to work?

Repeatable Example

I created a small GitHub repo that shows this in action: https://github.com/gmacster/gradle-system-properties-passthrough

I have a custom systemProp defined in gradle.properties called some-sample-prop. My main() method reads the value of this property and logs it.

I'm just running gradlew run with no extra args.

Version Info

------------------------------------------------------------
Gradle 5.4.1
------------------------------------------------------------

Build time:   2019-04-26 08:14:42 UTC
Revision:     261d171646b36a6a28d5a19a69676cd098a4c19d

Kotlin:       1.3.21
Groovy:       2.5.4
Ant:          Apache Ant(TM) version 1.9.13 compiled on July 10 2018
JVM:          11.0.2 (Oracle Corporation 11.0.2+9-LTS)
OS:           Windows 10 10.0 amd64

Upvotes: 1

Views: 1998

Answers (2)

britter
britter

Reputation: 1392

The system properties defined in gradle.properties are only passed to the JVM that is running Gradle itself, not the JVMs that are forked by what ever plugin you're using to run your application. So in order to pass the system properties from Gradle to the application that is started by Gradle, you need to manually copy them to the systemProperties property of the JavaExec task. Assuming you're using the run task provided by the application plugin, and you have a systemProp.proxy.host=example.proxy.host.com in your gradle.properties file, you would need to do this (see also this very old answer in the Gradle forums):

task.named('run') {
    systemProperties = [
        'proxy.host': System.getProperty('proxy.host')
    ]
}

But let me ask you why you need this indirection. I assume you only need those properties for running the application. If this is the case it would be easier to define them directly in the build script:

task.named('run') {
    systemProperties = [
        'proxy.host': 'example.proxy.host.com'
    ]
}

Upvotes: 4

Brother
Brother

Reputation: 2210

you can access even though it is not a good idea as this file it is supposed to store properties to be used for gradle tasks only.

I suggest you to have a application.properties under src/main/resources and read the file from there.

But, if you want to store in the gradle.properties, here is how you can retrieve it:

public static void main(String[] args) throws IOException {

    FileInputStream input = new FileInputStream("gradle.properties");

    // load a properties file
    Properties prop = new Properties();
    prop.load(input);

    System.out.println(prop.getProperty("systemProp.some-sample-prop"));
}

It will print:

hello world

Upvotes: -1

Related Questions