Home > OS >  running SpringBootTest using a spring profile given by maven
running SpringBootTest using a spring profile given by maven

Time:11-17

I got the following profile in my maven pom:

    <profiles>
        <profile>
            <id>local</id>
                <build>
                    <plugins>
                        <plugin>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-maven-plugin</artifactId>
                            <configuration>
                                <profiles>
                                    <profile>local</profile>
                                </profiles>
                            </configuration>
                        </plugin>
                    </plugins>
                </build>
        </profile>
    <profile>

which is fine for starting application, but if i want to build the application as follow mvn clean install -Plocal my @SpringBootTest fails due to:

No active profile set, falling back to default profiles: default

what am I missing?

ps mvn spring-boot:run -Plocal works no problem there ... also no intrested in mvn clean install -Dspring.profiles.active=local i know this works but just not intrested as profiles contain more than just profiles for us!

CodePudding user response:

(Your) Spring-boot-maven-plugin (configuration) is not affected by "install" target (it involves only spring-boot:repackage, which is not aware of "active profiles"/this config), that is why your profile (though propagated) not activated in your tests.

If you want it to work for mvn install, you will have to pass:

-Dspring.profiles.active=local

to:

  • surefire-plugin

    in pom(>profile>build>plugins):

    <plugin>
      <artifactId>maven-surefire-plugin</artifactId>
      <configuration>
        <argLine>-Dspring.profiles.active=local</argLine>
      </configuration>
    </plugin>
    

    or via cmd. See also. Plugin-Doc.

  • failsafe-plugin ... analogous!;) Plugin-Doc.

  • maybe more... (any other plugin, which starts spring-boot.)


  • But of course, please don't "forget" about @ActiveProfiles, which activates(more precisely adds!) profiles to your test( classe)s. (but build independent;)

  • And (of course;) you can also "package" spring.profiles.active somewhere in your application (properties/any location), in (almost) any "maven build".

    e.g. :

    • pom.xml:

       <profile>
        <id>local</id>
        <properties>
          <myProfiles>local,foo,bar</myProfiles>
        </properties>
       </profile>
       <!-- ... and (outside/in default profile!) -->
       <build>
        <testResources>
          <testResource>
            <directory>src/test/resources</directory>
            <filtering>true</filtering>
          </testResource>
        </testResources>
        <!-- ... -->
      </build>
      
    • src/test/resources/application.properties:

       spring.profiles.active=${myProfiles}
      
    • would write into target/test-classes/application.properties:

       spring.profiles.active=local,foo,bar
      

      ..., which would be (hopefully) picked up & activated by the "next test".

  • Related