Pepster
Pepster

Reputation: 2166

How to pass a compile-time Maven defined property to a spring-boot application?

When I was using the war plugin I could do:

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.0.2</version>
        <configuration>
          <archive>
            <manifest>
              <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
            </manifest>
            <manifestEntries>
              <Implementation-Build>${buildNumber}</Implementation-Build>
            </manifestEntries>
          </archive>
        </configuration>
      </plugin>

And in my src/main/resources I had a file (build.properties) to map this to a Spring Environment property:

build.revision=${buildNumber}

Is there a way to achieve this with the spring-boot-maven-plugin?

Upvotes: 2

Views: 2246

Answers (1)

Thilo
Thilo

Reputation: 262494

Using spring-boot-starter-parent gives you (according to documentation)

Sensible resource filtering for application.properties and application.yml including profile-specific files (e.g. application-foo.properties and application-foo.yml)". Since the default config files accept Spring style placeholders (${…​}) the Maven filtering is changed to use @..@ placeholders (you can override that with a Maven property resource.delimiter).

So, a @buildNumber@ in application.properties might work for you.

I am not sure how "sensible filtering" works for other (non-application-config) resource files (such as your build.properties). Maybe try it there, too.


Pure Maven option:

You can opt in to the Maven resource filtering:

  <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
  </resource>

This will rewrite your ${variable} expressions.

If you have files in there that should not receive this treatment (such as those that contain runtime placeholders, or binary files), you can also set up a separate resources-filtered directory.

Upvotes: 2

Related Questions