使用 maven 给出的 spring 配置文件运行 SpringBootTest

Unn*_*SoS 2 maven-3 maven spring-boot spring-boot-maven-plugin

我在我的 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>
Run Code Online (Sandbox Code Playgroud)

mvn clean install -Plocal 这对于启动应用程序来说很好,但是如果我想按照以下方式构建应用程序, @SpringBootTest则会失败,原因是:

No active profile set, falling back to default profiles: default
Run Code Online (Sandbox Code Playgroud)

还尝试过:

        <profile>
            <id>local</id>
                <build>
                    <plugins>
                        <plugin>
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-failsafe-plugin</artifactId>
                            <configuration>
                                <argLine>-Dspring.profiles.active=local</argLine>
                            </configuration>
                        </plugin>
                    </plugins>
                </build>
        </profile>
Run Code Online (Sandbox Code Playgroud)

我缺少什么?

psmvn spring-boot:run -Plocal在那里工作没问题...也不感兴趣, mvn clean install -Dspring.profiles.active=local我知道这有效,但只是不感兴趣,因为配置文件不仅仅包含我们的配置文件!

xer*_*593 5

(您的)Spring-boot-maven-plugin(配置)不受“安装”目标的影响(它仅涉及 spring-boot:repackage,它不知道“活动配置文件”/此配置),这就是您的配置文件的原因(尽管已传播)在您的测试中未激活。

如果你希望它适用于mvn install,你必须通过:

-Dspring.profiles.active=local
Run Code Online (Sandbox Code Playgroud)

到:

  • Surefire 插件

    在pom(>配置文件>构建>插件)中:

    <plugin>
      <artifactId>maven-surefire-plugin</artifactId>
      <configuration>
        <argLine>-Dspring.profiles.active=local</argLine>
      </configuration>
    </plugin>
    
    Run Code Online (Sandbox Code Playgroud)

    或通过cmd。也可以看看。 插件文档

  • 故障安全插件...类似!;) Plugin-Doc

  • 也许更多...(启动 spring-boot 的任何其他插件。)


  • 但当然,请不要“忘记” @ActiveProfiles,它会激活(更准确地说是添加!)配置文件到您的测试(类)中。(但建立独立;)

  • 并且(当然;)您还可以spring.profiles.active在(几乎)任何“maven build”中“打包”应用程序中的某个位置(属性/任何位置)。

    例如:

    • 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>
      
      Run Code Online (Sandbox Code Playgroud)
    • src/test/resources/application.properties:

       spring.profiles.active=${myProfiles}
      
      Run Code Online (Sandbox Code Playgroud)
    • 将写入 target/test-classes/application.properties:

       spring.profiles.active=local,foo,bar
      
      Run Code Online (Sandbox Code Playgroud)

      ...,这将(希望)被“下一个测试”拾取并激活。