Close

Spring Boot - Property expansion using Maven Resource Filtering

[Updated: Jul 19, 2017, Created: Jul 19, 2017]

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 place holder ${}.

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.properties

project-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);
        }
    }
}

Output

Project name: Automatic property expansion using Maven Example
Spring version: 4.3.9.RELEASE
App title: Example Project

Example Project

Dependencies 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

Maven Resource Filter in Boot Application Select All Download
  • maven-property-expansion
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • resources

Importing spring-boot-dependencies

pom.xml

<project .....>
<modelVersion>4.0.0</modelVersion>

<groupId>com.logicbig.example</groupId>
<artifactId>maven-property-expansion-no-parent-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Automatic property expansion using Maven Example</name>

<properties>
<java.version>1.8</java.version>
<app.title>Example Project</app.title>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.4.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<delimiters>
<delimiter>@</delimiter>
</delimiters>
<useDefaultDelimiters>false</useDefaultDelimiters>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<addResources>false</addResources>
</configuration>
</plugin>
</plugins>
</build>
</project>

Our property file and the main class are same as last example.

Output

Project name: Automatic property expansion using Maven Example
Spring version: @spring.version@
App title: Example Project

Note that @spring.version@ was not substituted this time, that's because this property was inherited from spring-boot-starter-parent in our last example. In this example we are not extending spring-boot-starter-parent, so this property is not available.

Example Project

Dependencies and Technologies Used:

  • Spring Boot 1.4.2.RELEASE
  • spring-boot-starter : Core starter, including auto-configuration support, logging and YAML.
  • JDK 1.8
  • Maven 3.3.9

Maven Resource Filtering with Boot Dependency Import Select All Download
  • maven-property-expansion-no-parent-starter
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • resources

See Also