在spring boot @activeprofile注释中配置maven配置文件

wor*_*erd 5 java h2 flyway spring-boot

我一直在阅读很多,但找不到一个解决方案,告诉我如何在@activeprofiles注释中包含maven配置文件.有可能吗?

我试图在我的测试执行之前尝试解决H2和flyway启动的问题,这是没有发生的.配置在pom.xml中的maven配置文件中进行了解释.当测试在teamcity中运行时,它会选择maven配置文件并执行但是作为独立配置,他们无法找到H2和flyway的配置并且在启动时失败.

iku*_*men 9

...无法找到一个解决方案,告诉我如何在@activeprofiles注释中包含maven配置文件.

您是指基于maven配置文件激活Spring配置文件,如果是这样,您可以在pom.xml中配置它:

<profiles>
  <profile>
    <id>mvnprofile</id>
    <properties>
      <spring.profiles.active>springprofile</spring.profiles.active>
    </properties>
    <dependencies>
      <dependency>
      </dependency> 
    </dependencies>
  </profile>
  ...
</profiles>
Run Code Online (Sandbox Code Playgroud)

在您的测试类中,配置它应运行的配置文件.

@Runwith(..)
@SpringApplicationConfiguration(...)
@ActiveProfiles("springprofile")
public class YourTest { ... }
Run Code Online (Sandbox Code Playgroud)

对于特定于配置文件的属性,application-springprofile.properties除了创建一个属性外application.properties,Spring Boot将首先加载application.properties,然后加载application-springprofile.properties - 覆盖先前从application.properties配置的任何属性.

最后,使用-P标志设置maven配置文件

$mvn test -P mvnprofile 
Run Code Online (Sandbox Code Playgroud)