【问题标题】:running SpringBootTest using a spring profile given by maven使用 Maven 提供的弹簧配置文件运行 SpringBootTest
【发布时间】:2021-12-27 18:01:56
【问题描述】:

我在我的 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>

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

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

也试过了:

        <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>

我错过了什么?

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

【问题讨论】:

  • 为什么使用与 maven 配置文件相关的 spring boot 配置文件。没有意义...如果您想使用配置文件启动 Spring Boot 应用程序,请通过--spring.profiles.active=XXX 使用命令行...

标签: spring-boot maven maven-3 spring-boot-maven-plugin


【解决方案1】:

(您的)Spring-boot-maven-plugin(配置)不受“安装”目标的影响(它只涉及 spring-boot:repackage,它不知道“活动配置文件”/此配置),即为什么您的个人资料(尽管已传播)未在您的测试中激活。

如果你想让它为 mvn install 工作,你必须通过:

-Dspring.profiles.active=local

到:

  • surefire 插件

    在 pom(>profile>build>plugins) 中:

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

    或通过 cmd。 See also.Plugin-Doc.

  • failsafe-plugin ...类似!;) Plugin-Doc.

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


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

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

    例如:

    • 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}
      
    • 将写入 target/test-classes/application.properties:

       spring.profiles.active=local,foo,bar
      

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

【讨论】:

  • 曾尝试过 maven-failsafe-plugin 但没有奏效。但是 maven-failsafe-plugin 确实有效
  • 用于故障保护(如果您拥有/使用它):local 可能不适合。
  • 添加了一个我尝试过的故障安全示例。 local 仅用于测试(如 foo 或 bar),除非它在故障安全中被跳过?
  • failsafe-plugin 也不受mvn clean install 的影响(肯定),因为你必须mvn integration-test。 ;)
猜你喜欢
  • 2016-12-13
  • 2020-06-18
  • 2017-07-07
  • 2014-10-14
  • 2019-02-06
  • 1970-01-01
  • 2021-06-27
  • 2017-11-01
  • 1970-01-01
相关资源
最近更新 更多