開發項目時會遇到這個問題:開發環境,測試環境,生產環境的配置文件不同,打包時經常要手動更改配置文件,更改的少還可以接受,但是如果需要更多個配置文件,手動的方法就顯得非常笨重了。
下面介紹一種方法,利用maven插件來打包不同環境的配置文件。我們用到的是maven-war-plugin這個插件。
首先貼出整個pom文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
<project xmlns= "http://maven.apache.org/pom/4.0.0" xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation= "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelversion> 4.0 . 0 </modelversion> <groupid>cms</groupid> <artifactid>cms</artifactid> <packaging>war</packaging> <version> 0.0 . 1 -snapshot</version> <name>cms</name> <url>http: //maven.apache.org</url> <properties> <spring.version> 4.1 . 6 .release</spring.version> <project.build.sourceencoding>utf- 8 </project.build.sourceencoding> </properties> <dependencies> <!-- 依賴省略--> </dependencies> <profiles> <profile> <!-- 本地開發環境 --> <id>dev</id> <properties> < package .environment>dev</ package .environment> </properties> <activation> <activebydefault> true </activebydefault> </activation> </profile> <profile> <!-- 測試環境 --> <id>test</id> <properties> < package .environment>test</ package .environment> </properties> </profile> <profile> <!-- 生產環境 --> <id>prod</id> <properties> < package .environment>prod</ package .environment> </properties> </profile> </profiles> <build> <finalname>cms</finalname> <sourcedirectory>src</sourcedirectory> <plugins> <plugin> <artifactid>maven-compiler-plugin</artifactid> <version> 3.3 </version> <configuration> <source> 1.7 </source> <target> 1.7 </target> </configuration> </plugin> <plugin> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-war-plugin</artifactid> <version> 2.1 . 1 </version> <configuration> <webxml>webroot\web-inf\web.xml</webxml> <warsourcedirectory>webroot</warsourcedirectory> <archive> <addmavendescriptor> false </addmavendescriptor> </archive> <warname>cms</warname> <webresources> <resource> <directory>src/main/resoreces/${ package .environment}</directory> <targetpath>web-inf/classes</targetpath> <filtering> true </filtering> </resource> </webresources> </configuration> </plugin> </plugins> <resources> <resource> <directory>src</directory> <filtering> true </filtering> <includes> <include>** /*.properties</include> <include>**/ *.xml</include> </includes> </resource> </resources> </build> </project> |
簡單說明幾個地方:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<profiles> <profile> <!-- 本地開發環境 --> <id>dev</id> <properties> < package .environment>dev</ package .environment> </properties> <activation> <activebydefault> true </activebydefault> </activation> </profile> <profile> <!-- 測試環境 --> <id>test</id> <properties> < package .environment>test</ package .environment> </properties> </profile> <profile> <!-- 生產環境 --> <id>prod</id> <properties> < package .environment>prod</ package .environment> </properties> </profile> </profiles> |
此處借助profiles定義幾個不同的環境文件夾,相同的需要在項目里面創建同id的文件夾,用來存放特定環境的配置文件。
我之前的resource目錄:
我的目錄結構是相對復雜的一種有2層目錄,只有一層目錄的也一樣更簡單。外層xml文件,還有一個properties文件夾。這里我針對不同環境需要更改的配置文件有4個,標紅的。
再看一下改造后resource的目錄結構:
隨便展開一個dev文件夾是這樣:
可見需要更改的配置文件,需要copy到各個環境的文件夾當中去,而不需要更改的文件,則不需要復制一份。
此處需要說明的是,如果我指定的是dev,則maven會將dev下的所有文件拿出來,db.xml覆蓋掉外面的db.xml,dev.properties文件夾中的配置文件會拿出來放到外面的properties文件夾中。所以說這里是非常靈活的,你需要哪些文件定制,完全由自己來控制。
再來看pom文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<plugin> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-war-plugin</artifactid> <version> 2.1 . 1 </version> <configuration> <webxml>webroot\web-inf\web.xml</webxml> <warsourcedirectory>webroot</warsourcedirectory> <archive> <addmavendescriptor> false </addmavendescriptor> </archive> <warname>test</warname> <webresources> <resource> <directory>src/main/resources/${ package .environment}</directory> <targetpath>web-inf/classes</targetpath> <filtering> true </filtering> </resource> </webresources> </configuration> </plugin> |
這里使用的是
1
|
maven-war-plugin |
這個插件,此插件的功能是很強大的,想深入了解,可以到官網去看。
1
|
${ package .environment} |
動態指定目錄,接收參數。
1
|
targetpath |
目標路徑。
另外說2點,這兩個標簽
1
2
|
<webxml>webroot\web-inf\web.xml</webxml> <warsourcedirectory>webroot</warsourcedirectory> |
1.如果maven打包錯誤說找不到web.xml,說明你得項目結構不是標準的,用webxml標簽指定一下就可以了
2.如果jsp打包沒有的話,同樣的問題,指定一下目錄,我的項目結構就不是maven標準結構,所以需要指定一下。
改造完畢,接下來就是利用maven打包了。
mvn clean ; mvn compile;
mvn -p test package; 傳相應環境參數就ok了
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/li295214001/article/details/52044800