我的原文:Maven-多环境打包配置
以前公司用的是Ant做编译、打包发布的工具,就这而已,Ant已经做的相当不错了,去年学习了Maven,瞬间有种相逢恨晚的感觉,Maven不止能做编译、打包、发布,而且还能有效的管理项目的依赖。工程使用Maven后,查看源代码和依赖的版本的控制变得简单,这里主要对开发完成后发版本这个问题做一研究和实践。
工作中会有开发环境、测试环境、预发布环境、正式环境,每个阶段发到每个环境,环境不同配置也会有出入,如果手动替换配置文件效率不高,且容易出错,以前用Ant的时候是可以每个环境打不同的包便于发布,我想Maven肯定也有类似的功能,功夫不负有心人,经过网上查找一些资料和动手配置,在我的工程中成功使用了多环境管理打包的配置,废话不多说,上代码。
pom.xml
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
|
<
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/maven-v4_0_0.xsd"
>
<
modelVersion
>4.0.0</
modelVersion
>
<
groupId
>cn.jon</
groupId
>
<
artifactId
>inspires</
artifactId
>
<
packaging
>war</
packaging
>
<
version
>0.0.1-SNAPSHOT</
version
>
<
name
>inspires Maven Webapp</
name
>
<
url
>http://maven.apache.org</
url
>
<
build
>
<
finalName
>inspires</
finalName
>
<
plugins
>
<
plugin
>
<
groupId
>org.apache.maven.plugins</
groupId
>
<
artifactId
>maven-war-plugin</
artifactId
>
<
configuration
>
<
warName
>inspires</
warName
>
<
archive
>
<
addMavenDescriptor
>false</
addMavenDescriptor
>
</
archive
>
<
webResources
>
<
resource
>
<!-- this is relative to the pom.xml directory -->
<!-- 下面代码很关键 {package.environment} 为profiles里面的配置 -->
<
directory
>src/main/resources-${package.environment}</
directory
>
<
targetPath
>WEB-INF/classes</
targetPath
>
<
filtering
>true</
filtering
>
</
resource
>
</
webResources
>
</
configuration
>
</
plugin
>
</
plugins
>
</
build
>
<
profiles
>
<
profile
>
<
id
>pro</
id
>
<
properties
>
<
package.environment
>pro</
package.environment
>
<!-- 正式环境 -->
</
properties
>
</
profile
>
<
profile
>
<
id
>dev</
id
>
<
properties
>
<
package.environment
>dev</
package.environment
>
<!-- 开发环境 -->
</
properties
>
</
profile
>
<!-- 。。。测试环境 等等 -->
</
profiles
>
<
dependencies
>
<
dependency
>
<
groupId
>junit</
groupId
>
<
artifactId
>junit</
artifactId
>
<
version
>4.11</
version
>
</
dependency
>
</
dependencies
>
</
project
>
|