Spring Boot with Maven Resource Filter (Profile)

If you want to know how to use Maven resource filter with Spring Boot, this page is for you.

This example is a part of  Spring Boot with all examples

Full Code with Spring Boot 1.5.8.RELEASE:

11_spring-boot-maven-resource-filter.zip

What you need for run this:

  • Maven 3
  • Java 8

What the code will do:

  • Call a url and it will return a value which is defined in a profile in Maven profile

Explain:

This is the file structure:

Let say you have two maven profiles in pom.xml, see the yellow lines:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8.RELEASE</version>
    </parent>

    <groupId>com.surasint.example</groupId>
    <artifactId>spring-boot-11</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    <profiles>
        <profile>
            <id>local</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <database.url>jdbc:mysql://localhost/testdb</database.url>
            </properties>
        </profile>
        <profile>
            <id>production</id>
            <properties>
                <database.url>jdbc:mysql://production/realdb</database.url>
            </properties>
        </profile>
    </profiles>

</project>

The “local” profile is for the local computer.

The “production” profile is for  the production environment.

Note: normally with Maven, you have to  have these lines in the <build> tag.

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

But you do not need this here because Spring Boot will take care it.

In application.properties looks like this:

database.url=@database.url@

Note: normally with Maven, you have to use ${database.url}, but here it is @database.url@

In TestRestController.java, it will get the property from application.properties and just return it if “/api/show” is called:

package com.surasint.example.web.api;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestRestController {

    @Value("${database.url}")
    private String sqlurl;

    @GetMapping("/api/show")
    public String show() {
        return sqlurl;
    }
}

Key point:

  • Line 10: @Value(“${database.url}”) : it is the Spring Boot annotation to get value from application.properties.

Try:

run this in console:

mvn clean package

Then run this to start (this will use the default profile which is “local”):

mvn clean spring-boot:run

You should see something like this:

 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.8.RELEASE)

.....
.....
2017-12-12 22:03:04.243  INFO 260 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.Annotation
ConfigEmbeddedWebApplicationContext@7595f8d7: startup date [Tue Dec 12 22:03:01 CET 2017]; root of context hierarchy

Open a browser and try to go to this url:

http://localhost:8080/api/show

You will get this:

Then stop the console by Ctrl + C (on Windows).

And start with this:

mvn clean spring-boot:run -Pproduction

Open a browswer and try to go to this url again:

http://localhost:8080/api/show

You will get this: