maven 默认情况下,为`mvn test`使用特定的Spring配置文件

hi3rlvi2  于 8个月前  发布在  Maven
关注(0)|答案(2)|浏览(91)

如何将Maven test阶段配置为使用名为test的Spring profile 默认情况下 )?

换句话说,我希望mvn test的普通调用(没有其他参数)默认使用名为test的Spring配置文件。目前,mvn test使用default配置文件(这并不奇怪)。
作为第二个要求,我希望开发人员能够在命令行中使用以下命令来覆盖它:

mvn test -Dspring.profiles.active=otherprofile

并且优先于默认的test配置文件。
我希望这在pom.xml中是可配置的,但是在Maven文档中找不到我想要的东西。
注意:这里我的测试框架是JUnit 5,测试类用SpringBootTest注解,尽管我认为这在这里不应该相关。

zd287kbt

zd287kbt1#

这取决于您使用的Maven版本。但是假设你可以使用最新版本的maven,你可以在你的${maven.projectBasedir}/.mvn/jvm.config中添加:

-Dspring.profiles.active=test

然后您指定的命令行将覆盖它。
如果你还没有这样做,你可以读到maven config

p4tfgftt

p4tfgftt2#

由于Maven test阶段的plugin:goalsurefire:test,我们可以配置Maven Surefire插件:

<build>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
          <argLine>-Dspring.profiles.active=test</argLine>
        </configuration>
      </plugin>
...

然后,mvn test将默认使用Spring test配置文件运行,但也可以被覆盖:

mvn test -Dspring.profiles.active=other

相关问题