gpt4 book ai didi

java - 如何将选定的 Maven 配置文件传递到 Spring 配置文件?

转载 作者:行者123 更新时间:2023-11-30 01:59:59 30 4
gpt4 key购买 nike

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

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

  • 应用程序测试本地.properties
  • 应用程序测试.properties

在我们的 Windows 开发系统上,所选配置文件将是“本地”并相应地位于服务器上。当选择“本地”配置文件时,应在测试 Spring 上下文中使用 application-test-local.properties ,否则应使用 application-test.properties 。在项目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>

但是,应用程序似乎无法将选定的 Maven 配置文件传递到 spring 配置文件,因为我正在尝试 “mvn clean test -Pdocker” 并且它总是从“<强>默认”配置文件。

知道如何修复以将 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>

最佳答案

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

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

然后使用 <argLine> 将其传递给 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>

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

Since the Version 2.17 using an alternate syntax for argLine, @{...} allows late replacement of properties when the plugin is executed, so properties that have been modified by other plugins will be picked up correctly

关于java - 如何将选定的 Maven 配置文件传递到 Spring 配置文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53215901/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com