69.1 Automatically expand properties at build time
Rather than hardcoding some properties that are also specified in your project’s build configuration, you can automatically expand them using the existing build configuration instead. This is possible in both Maven and Gradle.
69.1.1 Automatic property expansion using Maven
You can automatically expand properties from the Maven project using resource filtering. If you use the spring-boot-starter-parent
you can then refer to your Maven ‘project properties’ via @..@
placeholders, e.g.
[email protected]@ [email protected]@
Tip | |
---|---|
The spring-boot:run can add src/main/resources directly to the classpath (for hot reloading purposes) if you enable the addResources flag. This circumvents the resource filtering and this feature. You can use the exec:java goal instead or customize the plugin’s configuration, see the plugin usage page for more details. |
If you don’t use the starter parent, in your pom.xml
you need (inside the <build/>
element):
<resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources>
and (inside <plugins/>
):
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.7</version> <configuration> <delimiters> <delimiter>@</delimiter> </delimiters> <useDefaultDelimiters>false</useDefaultDelimiters> </configuration> </plugin>
Note | |
---|---|
The useDefaultDelimiters property is important if you are using standard Spring placeholders in your configuration (e.g. ${foo} ). These may be expanded by the build if that property is not set to false . |
69.1.2 Automatic property expansion using Gradle
You can automatically expand properties from the Gradle project by configuring the Java plugin’s processResources
task to do so:
processResources { expand(project.properties) }
You can then refer to your Gradle project’s properties via placeholders, e.g.
app.name=${name} app.description=${description}
Note | |
---|---|
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 \${..} . |