Maven 如何為不同的環境打包 —— 開發、測試和生產環境

來源:互聯網
上載者:User

在開發過程中,我們的軟體會面對不同的運行環境,比如開發環境、測試環境、生產環境,而我們的軟體在不同的環境中,有的配置可能會不一樣,比如資料來源配置、記錄檔配置、以及一些軟體運行過程中的基本配置,那每次我們將軟體部署到不同的環境時,都需要修改相應的設定檔,這樣來回修改,是個很麻煩的事情。有沒有一種方法能夠讓我們不用修改配置就能發布到不同的環境中呢。當然有,這就是接下來要做的事。

當然,這裡的前提是使用maven做為構建工具。

使用maven來實現多環境的構建可移植性,需要藉助maven提供的profile功能,通過不同的環境啟用不同的profile來達到構建的可移植性。

一、配置profile

首先是profile配置,在pom.xml中添加如下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>product</id>
<properties>
<profiles.active>product</profiles.active>
</properties>
</profile>
</profiles>

二、設定檔

針對不同的環境,我們定義不同的設定檔,而這些設定檔都做為資源檔放到maven工程的resources目錄下,即src/main/resources目錄下,
且各個環境的配置分別放到相應的目錄下,而所有環境都公用的配置,直接放到src/main/resources目錄下或WEB-INF/目錄下。如下圖所示:

三、maven資源外掛程式配置

 在設定檔在resource目錄下時,可以直接如下配置:

<build/>
  <filters>
  <filter>${project.basedir}/src/main/resources/environment/env-${profiles.active}.properties</filter>
  </filters>
  <resources>
  <resource>
  <directory>src/main/resources</directory>
  <filtering>true</filtering>
  </resource>
  </resources>
</build>

但是當設定檔在WEB-INF目錄下時,需要增加如下配置:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warName>acfun-online</warName>
<webResources>
<resource>
<directory>src/main/webapp</directory>
<filtering>true</filtering>
</resource>
</webResources>
</configuration>
</plugin>


From: http://www.cnblogs.com/tartis/p/5391079.html

另外可參考:https://my.oschina.net/vernon/blog/271970



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.