3

I am using spring boot and gradle for my application. The application can have different versions and we need to show this version info on the application in the footer.

e.g. 1.0.0 or 1.0.0.RELEASE etc

what are the possible ways to do this?

Patrick
  • 12,336
  • 15
  • 73
  • 115
ajm
  • 12,863
  • 58
  • 163
  • 234

1 Answers1

1

There is a good example in the documentation. You can automatically expand properties from the Gradle project and refer those properties as placeholders in your application.properties file.

 processResources {
     expand(project.properties) 
 }

You can then refer to your Gradle project’s properties via placeholders, e.g.

app.name=${name} 
app.description=${description}

You are then able to use for example the @Value annotation to get the properties value in your application.

@Value("${app.version}")
public String appVersion;

@Override
public void run(String... arg0) throws Exception {
    System.out.println(appVersion);
}

Maybe you have to escape the placeholder mechanism:

Gradle’s expand method uses Groovy’s SimpleTemplateEngine which transforms ${..} tokens. The ${..} style conflicts with Spring’s own property placeholder mechanism. To use Spring property placeholders together with automatic expansion the Spring property placeholders need to be escaped like \${..}.

P.S.:With maven you can do the same. more...

Patrick
  • 12,336
  • 15
  • 73
  • 115
  • I tried what u suggested. But then I am getting error like Execution failed for task ':processResources'. Could not copy file .. logback.xml – ajm Jan 31 '17 at 09:35
  • @ashishjmeshram maybe you have to escape the placeholders. I updated my answer. If the error still exists, please provide some more infos. – Patrick Jan 31 '17 at 09:44
  • The problem is with processResources { expand(project.properties)} line only. So even if I just add this one line only. I start getting errors. – ajm Jan 31 '17 at 11:55
  • @ashishjmeshram can you show your error messages you get. – Patrick Feb 01 '17 at 07:04
  • The error I am getting is as below. :processResources FAILED FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':processResources'. > Could not copy file '/opt/workspaces/src/main/resources/logback-spring.xml' to '/opt//workspaces/build/resources/main/logback-spring.xml'. – ajm Feb 01 '17 at 07:34
  • Also, I am using yml file and this is how I am trying to reference the gradle properties in my yml file . version: ${version}. See if this is correct or not? – ajm Feb 01 '17 at 07:35