Search code examples
spring-bootgradleintellij-ideaspring-boot-gradle-plugin

Inject Gradle properties into Spring Boot application.yml, not working in IntelliJ IDEA


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


Solution

  • 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;