[英]Spring boot test how to use different application.properties for integration test
[英]How to use Maven and various application.properties for artifact generation of different testing regions in a Spring configured app
我想使用Maven來處理不同本地和測試區域的工件生成。 我相信我可以使用不同的配置文件,但我不確定。
在Maven中,我可以選擇不同的目錄來選擇打包時使用的文件(例如application.properties)嗎? 我該如何設置?
我想要的是在我的項目中為資源提供以下文件夾
每個文件夾應包含不同版本的application.resources,它是Spring中的一個文件,可用於處理用於變量的硬編碼字符串。 對於本地構建,我們的開發人員也可以使用不同的操作系統 我是否應該要求我也想在不同的操作系統上實現它。
主要成果是:
理想情況下,我會根據最佳實踐(Duvall,Matyas,Glover)設置我的項目。
我們目前提供不同的屬性,但不是通過不同的文件夾。 我們通過混合來做到這一點
PropertyPlaceholderConfigurer
我對我們所做的事情的理解是有限的,但希望這可以作為其他人的一個有用的例子,也許我自己可以考慮。
我們提供參數,如下所示,指向不同的屬性文件。 每個屬性文件都具有區域/環境的配置。 我將盡可能地解釋當前的使用情況,以便它可以為其他人提供一些用途。
為了使用Maven配置文件,我們在我們的pom中創建了一個標識為development
的profile
,其中包含一個名為env
的區域配置屬性。 我還不知道在我們的項目中如何使用它,但是你會在下面看到我們的POM包括一個Maven編譯器插件和一個Maven Tomcat插件。
日復一日,作為開發人員,我們從IntelliJ內部在Tomcat本地運行我們的應用程序並提供env
屬性。 在啟動時, env
屬性作為參數提供,以設置為classpath*:dev-common.properties
。 此文件是屬性配置文件 - 為我們的不同區域設置占位符值。
env
的值可供PropertyPlaceholderConfigurer
示例1 - 在pom.xml中實現Maven profile
:
我們的pom中的profile
的實現是:
<profile>
<id>development</id>
<activation>
<property>
<name>env</name>
<value>development</value>
</property>
</activation>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.0-SNAPSHOT</version>
...
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
示例2 - 正常構建的Property placeholder configurer:
我們還使用Spring組件,一個PropertyPlaceholderConfigurer
。 to resource files. 我們與build參數協作使用它來設置資源文件的類路徑 。
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>
${env}
</value>
</list>
</property>
示例3 - 用於測試的屬性占位符配置器:
我們專門為集成測試設置了Spring Contexts,它也使用了PropertyPlaceholderConfigurer
。 這些是由集成測試類使用@ContextConfiguration(locations = {"classpath:test-dataexchange-application-context.xml"})
和@RunWith(SpringJUnit4ClassRunner.class)
)的組合來獲取的。
we configure the PropertyPlaceholderConfigurer
as follows to pick up the properties of an integration testing region: 在我們按如下方式配置PropertyPlaceholderConfigurer
以獲取集成測試區域的屬性:
<bean id="testpropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:dev-local-common.properties</value>
</list>
</property>
其他說明:
我們使用Team City進行構建管理,但我還沒有看到如何在那里使用這些設置,如果它們完全可用的話。 我可以想象,有能力將上述方法結合起來,以幫助持續集成和交付。
我沒有看到被識別為development
的配置文件在哪里使用。 這是我必須與我的團隊成員一起跟進的事情。
資源:
profile
一個POM或settings.xml中(Maven配置文件)之外,並說,誰使用這些外部設置的用戶現在應該把他們的settings.xml內 如果您使用Spring啟動,則可以通過簡單的方法執行此操作。
在maven中創建兩個配置文件,並在每個配置文件中設置一個屬性,其中包含要執行的Spring配置文件的名稱。
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<!-- Default spring profile to use -->
<spring.profiles.active>dev</spring.profiles.active>
<!-- Default environment -->
<environment>develop</environment>
</properties>
</profile>
在application.properties中,添加以下屬性:spring.profiles.active = $ {spring.profiles.active}
使用此模式application-profile.properties為每個配置文件創建application.property。 例如:application-dev.properties application-prod.properties
一定要在資源插件中激活過濾:
...
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
...
另一種方法是在maven執行期間創建一個名為activeprofile.properties的文件。 Spring啟動會查找此文件以加載活動配置文件。 您可以按如下方式創建此文件:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>prepare-package</phase>
<configuration>
<target>
<echo message="spring.profiles.active=${spring.profiles.active}" file="target/classes/config/activeprofile.properties" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<configuration>
</configuration>
</plugin>
目的是在中央服務器(CI / Build服務器)上一次為每個環境生成一個工件,旨在生成工件並在本地單擊一下啟動/測試應用程序,提供一致易學的檢查和運行方式您的構建,並檢入並配置您的CI。
您可以在Maven中使用配置文件並利用Maven目標來使用構建服務器來實現正確的構建,在我們的示例中,構建服務器是TeamCity。
在Spring上下文中使用property placeholder configurer,每個區域都有一個application.resources
文件和一個文件名掩碼,例如application-resources-${region}
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.