1. 程式人生 > >maven外掛 按配置載入不同環境配置檔案進行打包(maven-war-plugin)

maven外掛 按配置載入不同環境配置檔案進行打包(maven-war-plugin)

1.配置多種不同環境

如(本地local,開發dev,測試test 環境等)

 1   <profiles>
 2         <profile>
 3             <id>local</id>
 4             <properties>
 5                 <package.environment>local</package.environment>
 6             </properties>
 7         </profile>
 8
9 <profile> 10 <id>dev</id> 11 <properties> 12 <package.environment>dev</package.environment> 13 </properties> 14 <!-- 指定使用哪一個配置 --> 15 <activation> 16 <activeByDefault>true
</activeByDefault> 17 </activation> 18 </profile> 19 20 <profile> 21 <id>test</id> 22 <properties> 23 <package.environment>test</package.environment> 24 </properties> 25
</profile> 26 </profiles>

其中 第15-17行程式碼指定使用此環境配置,可以放在不同的<profile>下,需要在專案目錄下建立對應配置的資料夾如local、dev、test等 如下

common資料夾下為專案通用配置,dev下為開發環境的配置,test為測試環境下的

2.在build下配置以下內容

 1 <build>
 2         <finalName>demo專案名稱</finalName>
 3         <resources>
 4             <resource>  
 5                 <directory>src/main/java</directory>  
 6                 <includes>  
 7                     <include>**/*.xml</include>  
 8                 </includes>  
 9                 <filtering>false</filtering>  
10             </resource>  
11             <resource>
12                 <directory>src/main/resources</directory>
13                 <filtering>true</filtering>
14                 <excludes>
15                     <exclude>local/*</exclude>
16                     <exclude>test/*</exclude>
17                     <exclude>dev/*</exclude>
18                     <exclude>common/*</exclude>
19                 </excludes>
20             </resource>
21         </resources>
22         <plugins>
23             <plugin>
24                 <groupId>org.apache.maven.plugins</groupId>
25                 <artifactId>maven-war-plugin</artifactId>
26                 <version>2.3</version>
27                 <configuration>
28                     <encoding>UTF-8</encoding>  
29                     <archive>
30                         <addMavenDescriptor>false</addMavenDescriptor>
31                     </archive>
32                     <warName>NACPortal_${package.environment}</warName>
33                     <webResources>
34                         <resource>
35                             <directory>src/main/resources/${package.environment}</directory>
36                             <targetPath>WEB-INF/classes</targetPath>
37                             <filtering>true</filtering>
38                         </resource>
39                         <resource>
40                             <directory>src/main/resources/common</directory>
41                             <targetPath>WEB-INF/classes</targetPath>
42                             <filtering>true</filtering>
43                         </resource>
44                     </webResources>
45                 </configuration>
46             </plugin>
47             <!-- 編譯配置 按照不同版本進行配置-->
48             <plugin>
49               <artifactId>maven-compiler-plugin</artifactId>
50               <configuration>
51                   <source>1.8</source>
52                   <target>1.8</target>
53                   <encoding>UTF-8</encoding>
54                   <compilerArguments>
55                    <extdirs>src\main\webapp\WEB-INF\lib</extdirs>
56                   </compilerArguments>
57               </configuration>
58             </plugin>
59         </plugins>
60 </build>

開發的過程中就可以通過配置

<activation>
    <activeByDefault>true</activeByDefault>
</activation>

在相應配置環境 進行不同環境下的打包。

執行mvn install 後即可生成相應的war包 如 project_dev.war,project_test.war等

至此,大功告成!!

 

 

 

profile   ['prəʊfaɪl]   詳細 X 基本翻譯 n. 側面;輪廓;外形;剖面;簡況 vt. 描…的輪廓;扼要描述 vi. 給出輪廓 網路釋義 Profile: 輪廓 Bevel Profile: 輪廓倒角 tooth profile: 齒廓