[英]How to pass the selected Maven profile to Spring profile?
我有3個Maven項目A,B,C。A是B的父項目,B是C的父項目。所有概要文件都在項目A的pom.xml
中定義 。
在項目C中,我試圖根據所選配置文件在spring-test-context(在src / test / resources下 )中選擇屬性文件。對於回歸測試 ,我們有2個屬性文件:
在我們的Windows開發系統上,所選的配置文件將是“本地”的,並且在服務器上也將是相應的。 當選擇“本地”的個人資料, application-test-local.properties
應使用application-test.properties
測試Spring上下文另有說明。 在項目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插件運行的。 您可以在您的Maven配置文件中將Spring配置文件聲明為屬性:
<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>
請注意@{...}
語法:
由於版本2.17對argLine使用了備用語法,因此@ {...}允許在執行插件時延遲替換屬性,因此可以正確選擇由其他插件修改的屬性
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.