activate spring boot profile from maven profile

1.2k Views Asked by At

I want to separate my junit test and integration test separate. So I created a separate profile in pom.xml for the integration test as follows:

<profiles>
<profile>
    <id>integration-test</id>
    <properties>
        <test>IntegrationTestTrigger</test>
        <spring.profiles.active>integration-test</spring.profiles.active>
    </properties>
</profile>                                                                                  
<profiles>

The when I run the maven command mvn test -Pintegration-test, it is picking the test class as defined in the <properties> tag shown above as IntegrationTestTrigger. But it is not setting the spring.profiles.active property. So the test is starting with default profile. It is working fine with the maven command mvn test -Dtest=IntegrationTestTrigger -Dspring.profiles.active=integration-test

But as per my organisations jenkins setting, I need to run mvn test -Pintegration-test for the integration test, so I cannot add any extra environment variables to mvn command

2

There are 2 best solutions below

4
On

The properties at the is meant for property substitution at the .properties/.yml file inside resources folder.

Example:

application.yml:

spring:
   profiles:
       active: '@spring.profiles.active@'

pom.xml:

        <profile>
            <id>dev</id>
            <properties>
                <spring.profiles.active>dev</spring.profiles.active>
            </properties>
        </profile>

Here, the @spring.profiles.active@ will be replaced with dev during compile(by maven-resources-plugin plugin). Spring Boot uses @ as the resource delimiter at the spring-boot-starter-parent pom. You can change it to any character by changing the following property

//pom.xml
<project  .....>
  <properties>
    <resource.delimiter>@</resource.delimiter>
    ...
  </properties>

See https://github.com/gtiwari333/spring-boot-blog-app/blob/master/pom.xml#L436 for an complete example

See also: https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto-automatic-expansion-maven

0
On

Indeed as @gtivari333 said, profile/properties section is only to be used for substitution in POM files (and other files processed by maven, if so desired).
To set JVM properties aka "system properties" in POM directly, for use during test execution, you need to set them using surefire plugin configuration like this:

 <profiles>
    <profile>
        <id>integration-test</id>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <groupId>org.apache.maven.plugins</groupId>
                    <configuration>
                        <systemProperties>
                            <foo>bar</foo>
                            <spring.profiles.active>integration-test</spring.profiles.active>
                        </systemProperties>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>