Maven Resource plugin provides filtering which is nothing but variable substitutions. In resource files, we can use ${...} placeholder as variables which are replaced by system or maven properties during build time.
By default maven resource filtering is not enabled. If we extend our Spring Boot project from spring-boot-starter-parent the resource filtering is enabled by default. In that case @..@ delimiter is used instead of ${}, that is to avoid conflict with the spring-style placeholder ${}.
If we don't extend spring-boot-starter-parent but instead import spring-boot-dependencies then we have to enable maven resource filtering ourselves.
Let's see examples of both cases.
Extending from spring-boot-starter-parent
src/main/resources/application.propertiesproject-name=@project.name@
app-title=@app.title@
spring-version=@spring.version@
Where project.name is maven project specific property, app.title is our defined properties and spring.version is the property inherited from spring-boot-starter-parent.
pom.xml<project .....> <modelVersion>4.0.0</modelVersion>
<groupId>com.logicbig.example</groupId> <artifactId>maven-property-expansion</artifactId> <version>1.0-SNAPSHOT</version> <name>Automatic property expansion using Maven Example</name>
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> </parent>
<properties> <java.version>1.8</java.version> <app.title>Example Project</app.title> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <addResources>false</addResources> </configuration> </plugin> </plugins> </build> </project>
@SpringBootConfiguration
public class ExampleMain {
@Bean
MyBean myBean() {
return new MyBean();
}
public static void main(String[] args) throws InterruptedException {
SpringApplication bootApp = new SpringApplication(ExampleMain.class);
bootApp.setBannerMode(Banner.Mode.OFF);
bootApp.setLogStartupInfo(false);
ConfigurableApplicationContext context = bootApp.run(args);
MyBean myBean = context.getBean(MyBean.class);
myBean.doSomething();
}
private static class MyBean {
@Value("${project-name}")
private String projectName;
@Value("${spring-version}")
private String springVersion;
@Value("${app-title}")
private String appTitle;
public void doSomething() {
System.out.printf("Project name: %s%n"
+ "Spring version: %s%n"
+ "App title: %s%n",
projectName, springVersion, appTitle);
}
}
} OutputProject name: Automatic property expansion using Maven Example Spring version: 4.3.9.RELEASE App title: Example Project
Example ProjectDependencies and Technologies Used: - Spring Boot 1.5.4.RELEASE
Corresponding Spring Version 4.3.9.RELEASE - spring-boot-starter : Core starter, including auto-configuration support, logging and YAML.
- JDK 1.8
- Maven 3.3.9
|