maven profile實現多環境打包


快速解決:

項目目錄

 

1.pom文件中添加profile

<profiles>
<profile>
<!-- 本地開發環境 -->
<id>dev</id>
<properties>
<profiles.active>dev</profiles.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<!-- 測試環境 -->
<id>test</id>
<properties>
<profiles.active>test</profiles.active>
</properties>

</profile>
<profile>
<!-- 生產環境 -->
<id>prod</id>
<properties>
<profiles.active>prod</profiles.active>
</properties>
</profile>
</profiles> 

 

2. pom中指定 filter文件夾 和 maven-war-plugin指定替換文件夾

 <build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>spring-content.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<archiveClasses>true</archiveClasses>
<warName>${project.artifactId}</warName>
<warSourceDirectory>${basedir}/src/main/webapp</warSourceDirectory>
<webXml>${basedir}/src/main/webapp/WEB-INF/web.xml</webXml>
<webappDirectory>${project.build.directory}/${project.artifactId}
</webappDirectory>
<webResources>
<resource>
<!-- 由於我是把配置文件都在/WEB-INF/config/文件夾-->
<!-- 所以把src/main/resources 被filter替換的文件替換dao WEB-INF/config/下-->
<directory>src/main/resources</directory>
<targetPath>WEB-INF/config</targetPath>
<filtering>true</filtering>
</resource>
</webResources>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.3.0.M2</version>
<configuration>
<scanIntervalSeconds>6</scanIntervalSeconds>
<httpConnector>
<port>5004</port>
</httpConnector>
<webAppConfig>
<contextPath>/xxxx</contextPath>
<!--<defaultsDescriptor>${basedir}/src/main/resources/webdefault.xml</defaultsDescriptor>-->
</webAppConfig>
</configuration>
</plugin>
</plugins>
</build>

 

 3. 對比 WEB-INF/config下aplicationContent.xml(將被后面替換)和 src/main/resources 下aplicationContent.xml

<bean id="configProperties"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>/WEB-INF/config/application_dev.properties</value>
</list>
</property>
</bean>

 VS

<bean id="configProperties"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<!--${profiles.active} 此處占位符 會被mvn替換從pom中profile.active的環境變量
第三步中 maven-war-plugin配置替換文件 完成多環境切換-->
<value>/WEB-INF/config/application_${profiles.active}.properties</value>
</list>
</property>
</bean>

 4.maven 編譯打包  mvn  clean package  -Dmaven.test.skip=true -Ptest 

 指定-Dmaven.test.skip=true表示跳過測試   -Ptest 激活Profile id=test的環境參數 

實現效果

 

config下 原本application_dev.properties 編譯完成 替換為application_test.properties

從而實現了加載多環境配置. 

 

概念簡介 

構建項目時可能會遇到在測試(如單元測試)、開發、模擬、生產等不同環境下需要不同配置.
如果需要修改的項目很多而且復雜的話,則應該使用 Apache Maven 的 Profile 和 Filtering 功能來解決。

Filtering 功能

Filtering 是 Maven Resources Plugin 的一個功能,它會使用系統屬性或者項目屬性的值替換資源文件(*.properties,*.xml)當中 ${…} 符號的值。比如你系統屬性有一項 “user.name=foobar”,那么資源文件當中的 ${user.name} 符號會在 Maven 編譯時自動被替換為 “foobar”。

Profile 功能

Profile 的作用是允許你在項目文件(pom.xml)里定義若干個 profile 段,然后在編譯時選擇其中的一個用於覆蓋項目文件原先的定義。接着上一個例子,如果我們需要為開發環境和生產環境定義不同的 user.name 屬性值,則我們在項目目錄里創建兩個屬性文件:

profile-development.properties,內容

user.name=foobar

profile-production.properties,內容

user.name=tom

然后在項目文件(pom.xml)里增加 profile 段,如下:

<build>
<filters>
<filter>src/main/filters/filter-${env}.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<profiles>
<profile>
<id>develop</id>
<properties>
<env>develop</env>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<env>test</env>
</properties>
</profile>
<profile>
<id>product</id>
<properties>
<env>product</env>
</properties>
</profile>
</profiles>
pasting

 

在編譯項目時,可以使用 -P 參

數指定需要使用的 profile 的 id,比如下面命令將會使用 development profile:

$mvn clean compile -Pdevelopment

如果想使用 production profile 則執行如下命令:

$mvn clean compile -Pproduction

假如不指定 -P 參數的話,則會使用 activeByDefault=true 的一項(即 development)。

至此,通過 filtering 和 profile 功能實現了為開發環境和生產環境使用不同配置值的目的。當然 profile 還可以允許你添加更多的定義,比如為某一個 profile 添加不同的資源文件。在一些大中型項目里,不同的環境可能僅僅修改配置值並不足夠,可能還需要某個配置文件整個替換,那么就應該在 profiles/profile/build/resources 段里指定了。詳細的可以參閱附錄鏈接。

http://archboy.org/2012/05/21/apache-maven-profile-filtering-multiple-build-environments/

http://www.kafeitu.me/solution/2013/06/29/a-successful-cofiguration-file-management-solution.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM