yyunikov
yyunikov

Reputation: 5907

Add Gradle properties to application.properties resource

I have mutli-module Gradle application and I would to add properties which I've defined in gradle.properties to be available in application.properties of all my subprojects in /src/main/resources folder.

What I've alread tried is adding processResources plugin to subprojects section.

subprojects {

    processResources {
        expand(project.properties)
    }
// ...

As an example, I've defined the following property in gradle.properties file:

appVersion='0.0.1-SNAPSHOT'

Now, I want it to be present in application.properties, so I've added a placeholder as stated here. So, my application.properties looks the following way:

app.version=${appVersion}

Later on, I would like to use it using Spring, e.g.:

@Value("${app.version}")

However, after the project is built, properties are not replaced, so I have no version value in application.properties and still ${appVersion} placeholder. Any suggestions how to do that?

Upvotes: 2

Views: 18620

Answers (2)

yyunikov
yyunikov

Reputation: 5907

The problem was not in the Gradle configuration, but how my Spring Boot application started. I was running main() method directly from Intellij IDEA, which didn't work for me well, probably some of the tasks were not executed properly.

So, the solution is to run ./gradlew bootRun command. This way properties are getting correctly replaced.

Upvotes: 2

lance-java
lance-java

Reputation: 28071

I feel strongly that you should have separate folders for separate purposes. Therefore I suggest moving application.properties to src/main/filteredResources. Then:

import org.apache.tools.ant.filters.ReplaceTokens

processResources {
    with copySpec {
        from 'src/main/filteredResources'
        filter(ReplaceTokens, tokens: project.properties)
    }
}

Upvotes: 3

Related Questions