springmavenspring-bootspring-boot-maven-pluginmaven-profiles

How to integrate a Spring Boot profile with Maven profile?


I am trying to integrate Spring Boot profile with Maven profile but for some reason always the default profile is getting picked up.

mvn clean test -Dspring.profiles.active=prod (WORKING)

Logs:

2019-07-01 17:02:15.013  INFO 21872 --- [           main] com.example.BrowserTest                  : The following profiles are active: prod
2019-07-01 17:02:15.405  INFO 21872 --- [           main] com.example.BrowserTest                  : Started BrowserTest in 0.743 seconds (JVM running for 1.757)
************************************************************************
************************************************************************
Site: https://www.yahoo.com
************************************************************************
************************************************************************
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.509 s - in com.example.BrowserTest

mvn clean test -Prod (DOES NOT WORK, ALWAYS PICKS THE default PROFILE)

Logs:

2019-07-01 17:03:20.136  INFO 17532 --- [           main] com.example.BrowserTest                  : The following profiles are active: @activatedProperties@
2019-07-01 17:03:20.535  INFO 17532 --- [           main] com.example.BrowserTest                  : Started BrowserTest in 0.727 seconds (JVM running for 1.706)
************************************************************************
************************************************************************
Site: http://www.default.com
************************************************************************
************************************************************************
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.456 s - in com.example.BrowserTest

pom.xml

<?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>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>profiles-junit-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>profiles-junit-demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <profiles>
                        <profile>dev</profile>
                        <profile>prod</profile>
                    </profiles>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <activatedProperties>dev</activatedProperties>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <activatedProperties>prod</activatedProperties>
            </properties>
        </profile>
    </profiles>

</project>

src/test/resources/application.properties

spring.profiles.active=@activatedProperties@
site=http://www.default.com

src/test/resources/application-dev.properties

site=https://www.google.com

src/test/resources/application-prod.properties

site=https://www.yahoo.com

BrowserTest.java

public class BrowserTest extends ProfilesJunitDemoApplicationTests {

    @Value("${site}")
    private String site;

    //mvn clean test
    //mvn clean test -Dspring.profiles.active=dev
    //mvn clean test -Dspring.profiles.active=prod
    //mvn clean test -Pprod - NOT WORKING :(
    @Test
    public void homePage() {
        System.out.println("************************************************************************");
        System.out.println("************************************************************************");
        System.out.println("Site: " + site);
        System.out.println("************************************************************************");
        System.out.println("************************************************************************");
        assertNotNull(site);
    }

}

Solution

  • There are two ways:

    1. Move src/test/resources/application.properties to src/main/resources/application.properties.

    2. Add testResources:

    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>src/test/resources</directory>
        <filtering>true</filtering>
      </testResource>
    </testResources>