利用Maven继承关系简化项目POM配置

昨天看到有人问依赖管理的问题,所以就想把自己在使用maven时的经验与大家进行分享。

 

1、首先建一个root/pom.xml

 主要作用就是配置一般项目都需要的基本信息。

如:编码,编译版本,生成eclipse项目时的编码,生成manifest描述,单元测试常用依赖,生成javadoc,生成source,eclipse:eclipse时自动关联source与javadoc,本地仓库配置等。

我定义的Final Name的格式是 ${project.artifactId}-${project.version}-r${svn.revision}-${maven.build.timestamp},其中包含了svn的rev号与构建时的时间(我们用的是svn)

注意我用的maven-svn-revision-number-plugin 1.7目前不支持1.7格式的svn,官方插件目前已经升级到1.12了说是可以了,大家可以试试。

这种格式方便我们对产出的项目版本进行有效的跟踪(manifest中也有相关的信息)

文件名如:project1-core-1.0-SNAPSHOT-r82-20120503091343.jar

?
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
< 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 >org.noahx</ groupId >
     < artifactId >noahx-root</ artifactId >
     < version >1.0</ version >
     < packaging >pom</ packaging >
 
     < properties >
         < maven.build.timestamp.format >yyyyMMddHHmmss</ maven.build.timestamp.format >
         < project.build.sourceEncoding >UTF-8</ project.build.sourceEncoding >
         < project.reporting.outputEncoding >UTF-8</ project.reporting.outputEncoding >
         < maven.compile.source >1.5</ maven.compile.source >
         < maven.compile.target >1.5</ maven.compile.target >
 
         < maven-compiler-plugin.version >2.3.2</ maven-compiler-plugin.version >
         < maven-eclipse-plugin.version >2.9</ maven-eclipse-plugin.version >
 
         < maven-svn-revision-number-plugin.version >1.7</ maven-svn-revision-number-plugin.version >
         < maven-jar-plugin.version >2.4</ maven-jar-plugin.version >
         < maven-war-plugin.version >2.2</ maven-war-plugin.version >
         < maven-source-plugin.version >2.1.2</ maven-source-plugin.version >
         < maven-javadoc-plugin.version >2.8.1</ maven-javadoc-plugin.version >
         < maven-deploy-plugin.version >2.7</ maven-deploy-plugin.version >
 
         < jmock.version >2.5.1</ jmock.version >
         < junit.version >4.10</ junit.version >
     </ properties >
 
 
     < build >
         < plugins >
             < plugin >
                 < groupId >com.google.code.maven-svn-revision-number-plugin</ groupId >
                 < artifactId >maven-svn-revision-number-plugin</ artifactId >
                 < version >${maven-svn-revision-number-plugin.version}</ version >
                 < executions >
                     < execution >
                         < goals >
                             < goal >revision</ goal >
                         </ goals >
                     </ execution >
                 </ executions >
                 < configuration >
                     < entries >
                         < entry >
                             < prefix >svn</ prefix >
                         </ entry >
                     </ entries >
                 </ configuration >
             </ plugin >
 
             < plugin >
                 < groupId >org.apache.maven.plugins</ groupId >
                 < artifactId >maven-compiler-plugin</ artifactId >
                 < version >${maven-compiler-plugin.version}</ version >
                 < configuration >
                     < source >${maven.compile.source}</ source >
                     < target >${maven.compile.target}</ target >
                     < encoding >${project.build.sourceEncoding}</ encoding >
                 </ configuration >
             </ plugin >
 
             < plugin >
                 < groupId >org.apache.maven.plugins</ groupId >
                 < artifactId >maven-eclipse-plugin</ artifactId >
                 < version >${maven-eclipse-plugin.version}</ version >
                 < configuration >
                     < downloadSources >true</ downloadSources >
                     < downloadJavadocs >true</ downloadJavadocs >
                     < useProjectReferences >true</ useProjectReferences >
                     < wtpversion >2.0</ wtpversion >
                     < additionalConfig >
                         < file >
                             <!-- Text file encoding : UTF-8 -->
                             < name >.settings/org.eclipse.core.resources.prefs</ name >
                             < content
                               <![CDATA[eclipse.preferences.version=1${line.separator}encoding/<project>=UTF-8${line.separator}]]>
                             </ content >
                         </ file >
                         < file >
                             <!-- New text file line delimiter : Unix -->
                             < name >.settings/org.eclipse.core.runtime.prefs</ name >
                             < content
                               <![CDATA[eclipse.preferences.version=1${line.separator}line.separator=\n${line.separator}]]>
                             </ content >
                         </ file >
                     </ additionalConfig >
                     < classpathContainers >
                         < classpathContainer >org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6</ classpathContainer >
                     </ classpathContainers >
                 </ configuration >
             </ plugin >
 
             < plugin >
                 < groupId >org.apache.maven.plugins</ groupId >
                 < artifactId >maven-jar-plugin</ artifactId >
                 < version >${maven-jar-plugin.version}</ version >
                 < configuration >
                     < archive >
                         < manifest >
                             < addDefaultImplementationEntries >true</ addDefaultImplementationEntries >
                         </ manifest >
                         < manifestEntries >
                             < Build-Time >${maven.build.timestamp}</ Build-Time >
                             < SCM-Revision >${svn.revision}</ SCM-Revision >
                             < X-Compile-Source-JDK >${maven.compile.source}
                             </ X-Compile-Source-JDK >
                             < X-Compile-Target-JDK >${maven.compile.target}
                             </ X-Compile-Target-JDK >
                         </ manifestEntries >
                     </ archive >
                 </ configuration >
             </ plugin >
 
             < plugin >
                 < groupId >org.apache.maven.plugins</ groupId >
                 < artifactId >maven-war-plugin</ artifactId >
                 < version >${maven-war-plugin.version}</ version >
                 < configuration >
                     < archive >
                         < manifest >
                             < addDefaultImplementationEntries >true</ addDefaultImplementationEntries >
                         </ manifest >
                         < manifestEntries >
                             < Build-Time >${maven.build.timestamp}</ Build-Time >
                             < SCM-Revision >${svn.revision}</ SCM-Revision >
                             < X-Compile-Source-JDK >${maven.compile.source}</ X-Compile-Source-JDK >
                             < X-Compile-Target-JDK >${maven.compile.target}</ X-Compile-Target-JDK >
                         </ manifestEntries >
                     </ archive >
                 </ configuration >
             </ plugin >
 
             < plugin >
                 < groupId >org.apache.maven.plugins</ groupId >
                 < artifactId >maven-source-plugin</ artifactId >
                 < version >${maven-source-plugin.version}</ version >
                 < executions >
                     < execution >
                         < id >attach-sources</ id >
                         < goals >
                             < goal >jar-no-fork</ goal >
                         </ goals >
                     </ execution >
                 </ executions >
             </ plugin >
 
             < plugin >
                 < groupId >org.apache.maven.plugins</ groupId >
                 < artifactId >maven-javadoc-plugin</ artifactId >
                 < version >${maven-javadoc-plugin.version}</ version >
                 < configuration >
                     < locale >en_US</ locale >
                 </ configuration >
                 < executions >
                     < execution >
                         < id >attach-javadocs</ id >
                         < goals >
                             < goal >jar</ goal >
                         </ goals >
                     </ execution >
                 </ executions >
             </ plugin >
 
             < plugin >
                 < groupId >org.apache.maven.plugins</ groupId >
                 < artifactId >maven-deploy-plugin</ artifactId >
                 < version >${maven-deploy-plugin.version}</ version >
             </ plugin >
 
         </ plugins >
 
         < finalName >${project.artifactId}-${project.version}-r${svn.revision}-${maven.build.timestamp}</ finalName >
     </ build >
 
     < dependencies >
 
         < dependency >
             < groupId >org.jmock</ groupId >
             < artifactId >jmock-junit4</ artifactId >
             < version >${jmock.version}</ version >
             < type >jar</ type >
             < scope >test</ scope >
         </ dependency >
 
         < dependency >
             < groupId >org.jmock</ groupId >
             < artifactId >jmock-legacy</ artifactId >
             < version >${jmock.version}</ version >
             < type >jar</ type >
             < scope >test</ scope >
         </ dependency >
 
         < dependency >
             < groupId >junit</ groupId >
             < artifactId >junit</ artifactId >
             < version >${junit.version}</ version >
             < scope >test</ scope >
         </ dependency >
 
     </ dependencies >
<!--  本地artifactory配置
     <distributionManagement>
         <repository>
             <id>central</id>
             <name>My Artifactory-releases</name>
             <url>http://localhost/artifactory/libs-release-local</url>
         </repository>
         <snapshotRepository>
             <id>snapshots</id>
             <name>My Artifactory-snapshots</name>
             <url>http://localhost/artifactory/libs-snapshot-local</url>
         </snapshotRepository>
     </distributionManagement> -->
</ project >

2、java项目(jar),project1/pom.xml

在parent中声明是上面的pom。这样你的项目本身的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
< 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 >
 
     < parent >
         < groupId >org.noahx</ groupId >
         < artifactId >noahx-root</ artifactId >
         < version >1.0</ version >
     </ parent >
 
     < groupId >org.noahx.project1</ groupId >
     < artifactId >project1-core</ artifactId >
     < version >1.0-SNAPSHOT</ version >
     < packaging >jar</ packaging >
 
     < properties >
         < spring.version >3.0.4.RELEASE</ spring.version >
     </ properties >
 
     < dependencies >
 
         < dependency >
             < groupId >org.springframework</ groupId >
             < artifactId >spring-core</ artifactId >
             < version >${spring.version}</ version >
         </ dependency >
 
     </ dependencies >
 
</ project >

3、web项目(war),project2/pom.xml

同java一样进行瘦身。

注意packaging是war。wtpContextName这个只对eclipse wtp启作用,自动设置你的web上下文。对myeclipse无效需要手动修改。

?
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
< 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 >
 
     < parent >
         < groupId >org.noahx</ groupId >
         < artifactId >noahx-root</ artifactId >
         < version >1.0</ version >
     </ parent >
 
     < groupId >org.noahx.project2</ groupId >
     < artifactId >project2-web</ artifactId >
     < version >1.0-SNAPSHOT</ version >
     < packaging >war</ packaging >
 
     < properties >
         < servlet-api.version >2.4</ servlet-api.version >
     </ properties >
 
     < build >
         < plugins >
 
             < plugin >
                 < groupId >org.apache.maven.plugins</ groupId >
                 < artifactId >maven-eclipse-plugin</ artifactId >
                 < configuration >
                     < wtpContextName >p2</ wtpContextName >
                 </ configuration >
             </ plugin >
 
         </ plugins >
     </ build >
 
     < dependencies >
 
         < dependency >
             < groupId >javax.servlet</ groupId >
             < artifactId >servlet-api</ artifactId >
             < version >${servlet-api.version}</ version >
             < scope >provided</ scope >
         </ dependency >
 
     </ dependencies >
</ project >

4、总结

大家当然还可以定义项目公共的pom继续提炼共性。

noahx-root<--project-root

project-root<--project-core

project-root<--project-ext

一般的开源东西的pom都是一级一级继承下来的。大大减小了pom文件大小。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值