【问题标题】:configure maven profiles in spring boot @activeprofile annotation在spring boot @activeprofile注解中配置maven配置文件
【发布时间】:2015-09-06 01:16:26
【问题描述】:

我读了很多书,但找不到告诉我如何在 @activeprofiles 注释中包含 maven 配置文件的解决方案。有没有可能?

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

【问题讨论】:

    标签: java spring-boot h2 flyway


    【解决方案1】:

    ...找不到告诉我如何引入 maven 的解决方案 @activeprofiles 注解中的配置文件。

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

    <profiles>
      <profile>
        <id>mvnprofile</id>
        <properties>
          <spring.profiles.active>springprofile</spring.profiles.active>
        </properties>
        <dependencies>
          <dependency>
          </dependency> 
        </dependencies>
      </profile>
      ...
    </profiles>
    

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

    @Runwith(..)
    @SpringApplicationConfiguration(...)
    @ActiveProfiles("springprofile")
    public class YourTest { ... }
    

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

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

    $mvn test -P mvnprofile 
    

    【讨论】:

    • 谢谢,我一定会试试这个。它很有帮助。
    猜你喜欢
    • 2017-12-21
    • 1970-01-01
    • 2020-05-07
    • 1970-01-01
    • 2023-04-01
    • 2019-08-30
    • 1970-01-01
    • 2017-06-17
    • 2021-02-16
    相关资源
    最近更新 更多