【问题标题】:How to pass the selected Maven profile to Spring profile?如何将选定的 Maven 配置文件传递给 Spring 配置文件?
【发布时间】:2018-11-08 20:48:30
【问题描述】:

我有 3 个 maven 项目 A、B、C。A 是 B 的父项目,B 是 C 的父项目。所有 配置文件都在项目 A 的 pom.xml 中定义

在项目 C 中,我正在尝试根据所选配置文件在 spring-test-context(在 src/test/resources 下)中选择属性文件。对于 回归测试,我们有 2 个属性文件:

  • application-test-local.properties
  • application-test.properties

在我们的 Windows 开发系统上,选定的配置文件将是“本地的”,并相应地在服务器上。选择“本地”配置文件时,应使用@ 987654322和application-test.properties否则在测试弹簧上下文中。在project C,在spring-test-context.xml,我试过了:

<beans profile="docker">
    <util:properties id="metaDbProps"  location="application-test-local.properties"/>
 </beans>
 <beans profile="default">
    <util:properties id="metaDbProps"  location="application-test.properties"/>
 </beans>

但是,当我尝试 "mvn clean test -Pdocker" 时,应用程序似乎无法将选定的 maven 配置文件传递给 spring 配置文件,并且它总是从“默认”配置文件。

知道要解决什么问题以将 maven 配置文件传递给 spring 配置文件,以便它可以获取正确的属性文件吗?

为了便于理解,以下是项目 A 中配置文件的定义方式:

<profiles>
     <!-- Windows development  -->
    <profile>
        <id>docker</id>
        <activation/>
        <properties>
            <INSTALL_MACHINE_LIST>localhost</INSTALL_MACHINE_LIST>
            <COPY_MODE>local</COPY_MODE>
        </properties>
    </profile>
    <!-- Development -->
    <profile>
        <id>dev</id>
        <activation/>
        <properties>
            <INSTALL_MACHINE_LIST>dev01</INSTALL_MACHINE_LIST>
        </properties>
    </profile>
    <!-- QA -->
    <profile>
        <id>qa</id>
        <activation/>
        <properties>
            <INSTALL_MACHINE_LIST>dqa01</INSTALL_MACHINE_LIST>
        </properties>
    </profile>
    <!-- Production -->
    <profile>
        <id>prod</id>
        <activation/>
        <properties>
            <INSTALL_MACHINE_LIST>prod01</INSTALL_MACHINE_LIST>
        </properties>
    </profile>
</profiles>

【问题讨论】:

  • 回归测试、单元测试或集成测试是什么意思?它是如何通过surefire插件调用的?最后,为什么你有两套测试用例,你不是只想在测试阶段测试所有东西吗?

标签: java spring maven


【解决方案1】:

默认情况下,Maven 测试使用Maven Surefire Plugin 运行。您可以将 Spring 配置文件声明为 Maven 配置文件中的属性:

<profile>
  <id>docker</id>
  <properties>
    <spring.profiles.active>docker</spring.profiles.active>
  </properties>
</profile>

然后使用 &lt;argLine&gt; 配置将其传递给 Surefire:

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

请注意@{...} 语法:

由于 2.17 版使用 argLine 的替代语法,@{...} 允许在执行插件时延迟替换属性,因此已被其他插件修改的属性将被正确拾取

【讨论】:

    猜你喜欢
    • 2016-10-08
    • 2013-07-11
    • 2016-07-11
    • 1970-01-01
    • 2017-12-25
    • 2019-09-09
    • 2017-12-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多