1. 程式人生 > >利用maven-war-plugin打包專案部署在不同環境

利用maven-war-plugin打包專案部署在不同環境

一、前言

背景環境:希望將不同環境的需要修改的配置檔案放到不同的資料夾中,打包的時候根據環境選擇不同的資料夾作為配置檔案。
maven-war-plugin外掛用於將彙集的Java類檔案、依賴的jar檔案和資原始檔等一起打包以構建war包檔案。
maven-war-plugin中的配置詳解:
https://maven.apache.org/plugins/maven-war-plugin/war-mojo.html

二、專案目錄結構

這裡寫圖片描述
如上面所說,這裡我寫了兩個環境的配置檔案,一個dev環境一個為qa環境,他們的配置檔案的結構基本相同,props資料夾下存放的是properties檔案。檔案內容如下:

TYPE=${filtername}

三、pom.xml檔案

profile配置

<profiles>
        <profile>
            <id>qa</id>
            <properties>
                <package.environment>qa/conf</package.environment>
                <package.webxml>qa</package.webxml>
                <filtername
>
FNameQA</filtername> </properties> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <id>dev</id> <properties
>
<package.environment>dev/conf</package.environment> <package.webxml>dev</package.webxml> <filtername>FNameDEV</filtername> </properties> </profile> </profiles>

上面定義了兩個profile,分別是qa環境和dev環境,配置了三個引數
* package.environment 配置檔案的路徑,在這裡是conf路徑下的檔案,例如qa/conf,即代表在去resource目錄下的qa/conf取配置檔案。
* package.webxml web.xml的路徑,例如qa即代表在resource/qa目錄下取web.xml
* filtername properties檔案中所要使用的引數

plugin配置

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.3</version>
    <configuration>
        <archive>
            <!-- Whether the created archive will contain these two Maven files -->
            <addMavenDescriptor>false</addMavenDescriptor>
        </archive>
        <warName>solrT</warName>
        <webResources>
            <resource>
                <directory>src/main/resources/${package.environment}/</directory>
                <targetPath>WEB-INF/classes</targetPath>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/resources/${package.webxml}</directory>
                <targetPath>WEB-INF</targetPath>
                <filtering>true</filtering>
            </resource>
        </webResources>
        <warSourceExcludes>src/main/resources/dev/test.txt,src/main/resources/qa/test.txt</warSourceExcludes>
        <packagingExcludes>
            WEB-INF/conf/**,WEB-INF/classes/dev/**,WEB-INF/classes/qa/**,WEB-INF/test.txt,
            WEB-INF/classes/test.txt,WEB-INF/classes/web.xml
        </packagingExcludes>
    </configuration>
</plugin>

在上面的配置中,通過configuration標籤設定打包的一些配置,其中:
* directory:定義了要進行打包的檔案路徑,
* targetPath: 打包的目錄資料夾,即將檔案打成war包的時候放到war包的此目錄下
* filtering:在打包的時候是否將配置檔案中的用${}表示的值給替換。

這裡寫圖片描述

四、打包結果

1.原先的web.xml:

這裡寫圖片描述

是沒有過濾器的屬性的,但是通過profile打包時修改qa目錄下的web.xml,並將其打包到web-inf目錄下,替換原先的web.xml,打包完成後的web.xml:
這裡寫圖片描述

這裡寫圖片描述

這一點內容修改是我們想要看到的。

2.同樣的properties檔案

原先properties:TYPE=${filtername}

打包後:
這裡寫圖片描述

我們想要實現的修改內容已經基本完成,證明此外掛是可用的。

五、瑕疵的地方

問題1:

打包的時候src/main/resources目錄下的檔案本身就會被打包到classes目錄下,而通過外掛的resource標籤,也可實現此功能,所以會造成兩者重複。

這裡寫圖片描述

可以發現他將src/main/resources目錄下的dev和qa都打包進來了,而我最初的目的是隻將qa/conf和dev/conf下的內容給打包出來。

這裡需要注意兩個標籤:warSourceExcludes和packagingExcludes

warSourceExcludes是在拷貝檔案到war資料夾時忽略掉指定檔案或者資料夾(但是如果war命令前沒有clean指令,而war資料夾下已經包含了指定檔案或者資料夾時,最後生成的war包裡還是會包含這些檔案或資料夾,哪怕沒有拷貝它們到war資料夾).

packagingExcludes是在生成war包時不包含指定檔案或資料夾到war檔案中,不論它們是否存在於war資料夾下。

因此慎重起見,更傾向於使用packagingExcludes引數。

通過pom檔案中packagingExcludes標籤的設定,
* WEB-INF/conf/**, WEB-INF下的conf資料夾不需要
* WEB-INF/classes/dev/**, classes下的dev資料夾不需要
* WEB-INF/test.txt, WEB-INF下的test.txt檔案不需要

最終打包的時候會排除這些檔案和資料夾。打包結果如下所示:

WEB-INF
這裡寫圖片描述
classes
這裡寫圖片描述