2

I've managed to inject the Gradle proj.ver into application.yml and after that injected it into service application. My application.yml looks like this:

project: 
  version: ${version}

But it works only if I started the app from cli with:

gradle bootRun

If I'm trying to start the app from IntelliJ, it didn't work and it failed with:

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'version' in value "${version}"

I read all the answers from Stackoverflow and they suggested two solutions:

  1. Use spring profiles

  2. Modify run configuration and run before launch the gradle task: processResources

I'd prefer something like a default value for proj.ver when I'm running from IntelliJ. Is that possible? Or are any better solutions for this situation ?
Thanks

brebDev
  • 774
  • 10
  • 27
  • `${version:default}` should give you a default if not found. – M. Deinum May 01 '18 at 12:10
  • Thx for the answer, but as I wrote in the answer it seems that only like this work from both sides: `${version?:unknown}` – brebDev May 01 '18 at 14:26
  • That shouldn't be needed as it is a value expression and not a SpEL expression. Unless this is somehow parsed by gradle (which it probably is) which does the replacement. – M. Deinum May 02 '18 at 05:48
  • can you say how you inject gradle properties into application.yaml? – ykembayev Apr 13 '20 at 12:47
  • Yes of course, I've updated the response. You can find out the steps there. Hope it helps. – brebDev Apr 13 '20 at 13:17

2 Answers2

4

As M. Deinum said above in the comment, I managed to run the app from IntelliJ, but now the gradle bootRun started to fail with:

Caused by: groovy.lang.MissingPropertyException: No such property: unknown for class: SimpleTemplateScript2

After some more research it seems that ${version?:unknown}(with the question mark) it works either from the IDE or from cli.


I've updated the response, in order for others to know how to inject Gradle build info into Spring-boot:

1) Tell Gradle to pass the build data towards a Spring yml file like this:

processResources {
filesMatching('appInfo.yml') {
    expand(project.properties)
}}

2) The appInfo.yml will look like:

project:
  version: ${version?:unknown}

3) Inject the version of the build into Spring services like:

@Value("${project.version}")
private String applicationVersion;
brebDev
  • 774
  • 10
  • 27
0

Just to complete for Kotlin user, what works for me was :

  1. build.gradle.kts

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

     project.version= ${version}
    
  3. Service.kt

     @Value("\${project.version}") lateinit var version: String
    
Nicolas
  • 663
  • 6
  • 17