Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing gradle properties in application.yml

If this question has been asked and answered, or that there is doc or an example, please forgive me. I have spent several hours looking for a solution here on stackoverflow and even more time in the gradle doc and haven't been able to make this work.

I have a spring boot project with a pretty standard maven layout. I am using gradle 2.4. here's the layout of the relevent files:

/gradle.properties
/build.gradle
/settings.gradle
/src/main/resources/application.yml

In gradle.properties, I have defined the following properties:

name=Sample microservice
description=brief description of the service goes here
version=1.0.0-SNAPSHOT

In my application.yml file, I'd like to set corresponding spring properties to those same values. (I'd like to define them together in one place and use them in several places. Since version is typically defined in gradle.properties, I want to cluster the rest there as well.)

I've tried the following line in application.yml, but things aren't working as hoped:

info.app.name: ${name}
info.app.description: ${description}
info.app.version: ${version}

(I also tried ${project.name}, etc. That didn't work either.)

I ran gradlew properties... the properties are listed with values as expected. However, when I run a build, the yaml file is copied into \build\resources\main as expected, but the ${value} tokens are not resolved.

I also included the following lines in build.gradle file, but things remain unresolved.

processResources {
   filesMatching('gradle.properties') { expand(project.properties) }
}

(My goal is to use the actuator /info endpoint to provide the values of those properties to a service caller.)

Any suggestions or pointers to documentation that help would be greatly appreciated!

like image 459
drhender Avatar asked Oct 09 '15 17:10

drhender


People also ask

Where do I put Gradle properties?

The global properties file should be located in your home directory: On Windows: C:\Users\<you>\. gradle\gradle. properties.

How do I pass system properties in Gradle?

System propertiesUsing the -D command-line option, you can pass a system property to the JVM which runs Gradle. The -D option of the gradle command has the same effect as the -D option of the java command.

Where is Gradle wrapper properties file?

The Wrapper shell script and batch file reside in the root directory of a single or multi-project Gradle build. You will need to reference the correct path to those files in case you want to execute the build from a subproject directory e.g. ../../gradlew tasks .


1 Answers

I recently faced same situation - here is what worked for me.

TL;DR: In Spring Boot 1.2.5, info.* properties in application.yml are ignored by /info; need to use application.properties for info.* properties.

First, create src/main/resources/application.properties with this content:

info.build.description=${description}
info.build.name=${name}
info.build.version=${version}

Second, add this snippet to your build.gradle file:

processResources {
    filesMatching("**/application.properties") {
        expand( project.properties )
    }
}

Next, run your build as normal. This will process the application.properties file and replace the variables with their build-time values as it copies the file to build/resources/main/application.properties .

For me, when running the deployed .jar, my /info endpoint is populated with the expanded values, which is the goal.

Note that for bonus points you can add this to your build.gradle to get Git info in the /info endpoint, too:

apply plugin: "com.gorylenko.gradle-git-properties"

You will also need to add this to the dependencies section of build.gradle, for that to work:

classpath 'gradle.plugin.com.gorylenko.gradle-git-properties:gradle-git-properties:1.+'

Hope this helps!

like image 51
m0j0hn Avatar answered Nov 05 '22 05:11

m0j0hn