您的位置:首页 > 编程语言 > Java开发

eclipse上搭建maven多模块Java Web项目

2017-08-28 21:56 417 查看
http://blog.csdn.net/jrainbow/article/details/50546524

版权声明:本文为博主原创文章,未经博主允许不得转载。

目录(?)[+]


1、模块化需求及项目模块说明

手头上有个已上线的系统,但因老板的特殊要求,系统需要不断的修改。还有就是公司市场部不定期地在举行一些微信活动,每一个活动都是周期性的,活动完了这个功能就要在系统中移除。

系统中就有三种模块:已经在系统中正常运行不需要再变更的模块、经常性变更的模块、用完就要移除的活动模块。

所以,我们需要把项目分成了下面几个模块。



简单说明一下:

timetable-common是常用工具包存放的模块。
wechat-api是微信接口模块,此模块用到了timetable-common包下的内容。
timetable-system是系统管理相关的内容,主要是用户相关的逻辑和接口,此模块用到了timetable-common及wechat-api。
timetable-common、wechat-api、timetable-system都是不需要再变更的模块。
timetable-main是一个web模块,也是整个项目的入口。主模块用到了timetable-system、timetable-common、wechat-api模块。
每一次微信活动都可以作为一个web模块,如timetable-activity,活动中可能会用到其它模块(除timetable-main以外)的内容。在系统发布的时候,可以将活动模块引入到主模块中一起发布。当活动结束后,我们可以快速地将活动模块移除。




2、准备工作

eclipse版本:
Eclipse Java EE IDE for Web Developers.
Version: Luna Service Release 2 (4.4.2)
Build id: 20150219-0600
1
2
3
1
2
3
[/code]

由于涉及到maven项目在eclipse上进行tomcat的集成调试,如果调试异常的朋友可以考虑更换版本比较高的eclipse版本。

maven版本: 2.2

maven相关的内容:http://blog.csdn.net/p_3er/article/category/2505419


3、模块化过程


3.1 主项目设置

maven多模块项目需要一个主项目来聚合各个子模块,不过其实就是需要一个父pom.xml。

这个pom.xml主要有两个内容:

packaging方式为pom。所有带有子模块的项目的packaging都为pom。packaging如果不进行配置,它的默认值是jar,代表Maven会将项目打成一个jar包。

<packaging>pom</packaging>
1
1
[/code]

使用module标签引入各个子模块。如果通过Maven build app-parent的时候,它会根据子模块的相互依赖关系整理一个build顺序,然后依次build。其实此配置不需要手动输写,通过eclipse创建maven module时,eclipse会自动加入到此配置文件中。

<modules>
<module>timetable-common</module>
<module>wechat-api</module>
<module>timetable-system</module>
<module>timetable-activity</module>
<module>timetable-main</module>
</modules>
1
2
3
4
5
6
7
1
2
3
4
5
6
7
[/code]

完整的timetable/pom.xml为:
<?xml version="1.0" encoding="UTF-8"?>
<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>com.benben</groupId>
<artifactId>timetable</artifactId>
<packaging>pom</packaging>
<version>2.0.0</version>
<name>timetable</name>
<url>http://maven.apache.org</url>

<properties>
<project.version>2.0.0</project.version>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<modules>
<module>timetable-common</module>
<module>wechat-api</module>
<module>timetable-activity</module>
<module>timetable-main</module>
<module>timetable-system</module>
</modules>
</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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[/code]


3.2 独立的maven简单模块

通过eclipse,新建Maven Module.



新建一个模块名为timetable-common的simple project。



创建完成后的模块类似一个简单的Java项目:



timetable-common/pom.xml:
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.benben</groupId>
<artifactId>timetable</artifactId>
<version>2.0.0</version>
</parent>
<artifactId>timetable-common</artifactId>
<name>timetable-common</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<!-- 设置变量:用于统一管理Library的版本 -->
<javax.version>7.0</javax.version>
<javax.servlet.version>3.1.0</javax.servlet.version>
<javax.servlet.jstl.version>1.2</javax.servlet.jstl.version>
<junit.version>4.12</junit.version>
<log4j.version>1.2.16</log4j.version>
<org.aspectj.version>1.7.3</org.aspectj.version>
<net.sf.ehcache.version>2.3.2</net.sf.ehcache.version>
<org.slf4j.version>1.6.1</org.slf4j.version>
<mysql.jdbc.version>5.1.29</mysql.jdbc.version>
<org.hibernate.entitymanager.version>3.6.3.Final</org.hibernate.entitymanager.version>
<org.springframework.version>4.1.1.RELEASE</org.springframework.version>
<org.codehaus.jackson.version>1.7.4</org.codehaus.jackson.version>
<net.sf.json-lib.version>2.4</net.sf.json-lib.version>
<proxool.version>0.8.3</proxool.version>
<javamail.version>1.4.1</javamail.version>
<commons-codec.version>1.9</commons-codec.version>
<commons-io.version>1.3.2</commons-io.version>
<commons-fileupload.version>1.3.1</commons-fileupload.version>
<xstream.version>1.4.3</xstream.version>
<org.apache.jcs.version>1.3</org.apache.jcs.version>
<joda-time.version>2.3</joda-time.version>
<org.apache.poi.version>3.8</org.apache.poi.version>
<com.fasterxml.jackson.core.version>2.3.3</com.fasterxml.jackson.core.version>
<commons-httpclient.version>3.1</commons-httpclient.version>
<jaxen.version>1.1.6</jaxen.version>
<com.drewnoakes.version>2.8.1</com.drewnoakes.version>
<com.google.zxing.version>3.0.0</com.google.zxing.version>
<io.netty.version>5.0.0.Alpha2</io.netty.version>
</properties>

<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>${org.slf4j.version}</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>${javax.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

<!-- hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${org.hibernate.entitymanager.version}</version>
</dependency>

<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>${net.sf.ehcache.version}</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${javax.servlet.jstl.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${javax.servlet.version}</version>
</dependency>

<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>

<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${org.aspectj.version}</version>
</dependency>

<!-- JDBC -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.jdbc.version}</version>
</dependency>
<!-- proxool -->
<dependency>
<groupId>proxool</groupId>
<artifactId>proxool</artifactId>
<version>${proxool.version}</version>
</dependency>

<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>${org.codehaus.jackson.version}</version>
</dependency>

<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-lgpl</artifactId>
<version>${org.codehaus.jackson.version}</version>
</dependency>

<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>${org.codehaus.jackson.version}</version>
</dependency>

<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-lgpl</artifactId>
<version>${org.codehaus.jackson.version}</version>
</dependency>

<!-- spring framework -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${org.springframework.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${org.springframework.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${org.springframework.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${org.springframework.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${org.springframework.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${com.fasterxml.jackson.core.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${com.fasterxml.jackson.core.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${com.fasterxml.jackson.core.version}</version>
</dependency>

<!-- joda-time -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>${joda-time.version}</version>
</dependency>
<!-- json -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>${net.sf.json-lib.version}</version>
<classifier>jdk15</classifier>
</dependency>

<!-- mail -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>${javamail.version}</version>
</dependency>

<!-- Jcscache -->
<dependency>
<groupId>org.apache.jcs</groupId>
<artifactId>jcs</artifactId>
<version>${org.apache.jcs.version}</version>
</dependency>

<!-- commons-codec -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>${commons-codec.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>${commons-fileupload.version}</version>
</dependency>

<!-- 微信接口使用到 -->
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>${xstream.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5</version>
</dependency>

<!-- POI -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${org.apache.poi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${org.apache.poi.version}</version>
</dependency>

<!-- commons-httpclient 支付宝接口使用 -->
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>${commons-httpclient.version}</version>
</dependency>
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>${jaxen.version}</version>
</dependency>

<!-- 获取图片信息 -->
<dependency>
<groupId>com.drewnoakes</groupId>
<artifactId>metadata-extractor</artifactId>
<version>${com.drewnoakes.version}</version>
</dependency>

<!-- google二维码工具 -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>${com.google.zxing.version}</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>${com.google.zxing.version}</version>
</dependency>

<!-- netty -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>${io.netty.version}</version>
</dependency>
</dependencies>

</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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
[/code]

配置比较简单:

timetable-common模块继承了timetable父模块,因此这个pom的一开始就声明了对timetable的引用,该引用是通过Maven坐标GAV实现的。而关于项目timetable-common本身,它却没有声明完整GAV,这里我们只看到了artifactId。这个pom并没有错,groupId和version默认从父模块继承了。实际上子模块从父模块继承一切东西,包括依赖,插件配置等等。

<parent>
<groupId>com.benben</groupId>
<artifactId>timetable</artifactId>
<version>2.0.0</version>
</parent>
<artifactId>timetable-common</artifactId>
<name>timetable-common</name>
<url>http://maven.apache.org</url>
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
[/code]

如果packaging不写,那么缺省值是jar。我们也可以写上此标签。

<packaging>jar</packaging>
1
1
[/code]

比较特殊的是timetable-common模块是项目中最底层的模块,其它的模块都会依赖到此模块的内容。所以我在这里把项目中用到的所以依赖都放在这里配置。你也可以在使用到某些依赖的时候再在当前模块里配置。但统一配置的好处是能够方便的查看到项目中用到的依赖。当然,如果是只用于用完就要移除的活动模块中的依赖还是在本身的pom中配置。因为这些依赖随着模块的移除也用不上了。


3.3 依赖其它模块的maven简单模块

使用a中相同的方法新建wechat-api模块。

wechat-api/pom.xml:
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.benben</groupId>
<artifactId>timetable</artifactId>
<version>2.0.0</version>
</parent>
<artifactId>wechat-api</artifactId>
<name>wechat-api</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>com.benben</groupId>
<artifactId>timetable-common</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</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
27
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
[/code]

wechat-api模块依赖timetable-common模块的内容

<dependencies>
<dependency>
<groupId>com.benben</groupId>
<artifactId>timetable-common</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
1
2
3
4
5
6
7
1
2
3
4
5
6
7
[/code]

timetable-system模块的新建与模块与wechat-api模块的方式与配置雷同。只是timetable-system模块依赖的是wechat-api模块。当然,通过依赖wechat-api,同时也依赖了timetable-common。


3.4 依赖其它模块的maven web模块

与使用eclipse创建一个独立的maven web project一样,创建一个maven web module。

注意:我们不是创建简单的模块



通过Filter过滤后,选择maven-archetype-webapp。



创建完成后的模块类似一个的Java web项目:



在此模块下,我们和正常的java web开发一下,在src/main/java下写java代码,src/main/resources下放配置文件,src/main/webapp下写web相关的代码及资源。

timetable-activity/pom.xml:
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.benben</groupId>
<artifactId>timetable</artifactId>
<version>2.0.0</version>
</parent>

<artifactId>timetable-activity</artifactId>
<packaging>war</packaging>
<name>timetable-activity Maven Webapp</name>
<url>http://maven.apache.org</url>

<dependencies>
<dependency>
<groupId>com.benben</groupId>
<artifactId>timetable-system</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

<build>
<finalName>timetable-activity</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</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
27
28
29
30
31
32
33
34
35
36
37
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
[/code]

此模块依赖于timetable-system模块

<dependency>
<groupId>com.benben</groupId>
<artifactId>timetable-system</artifactId>
<version>${project.version}</version>
</dependency>
1
2
3
4
5
1
2
3
4
5
[/code]

web 模块的packing为war。

<packaging>war</packaging>
1
1
[/code]

packaging类型是war的maven工程或模块,默认是必须要有web.xml的。如果此maven模块作为其它工程的一个模块,不独立配置web.xml。

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
1
2
3
4
5
6
7
1
2
3
4
5
6
7
[/code]


3.5 主web模块

像3.4那样创建一个timetable-main的maven web module。

timetable-main/pom.xml:
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.benben</groupId>
<artifactId>timetable</artifactId>
<version>2.0.0</version>
</parent>
<artifactId>timetable-main</artifactId>
<packaging>war</packaging>
<name>timetable-main</name>
<url>http://maven.apache.org</url>

<dependencies>
<dependency>
<groupId>com.benben</groupId>
<artifactId>timetable-system</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>com.benben</groupId>
<artifactId>timetable-activity</artifactId>
<version>${project.version}</version>
<type>war</type>
</dependency>
</dependencies>

<build>
<plugins>
<!-- 合并多个war -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<packagingExcludes>WEB-INF/web.xml</packagingExcludes>
<overlays>
<overlay>
<groupId>com.benben</groupId>
<artifactId>timetable-activity</artifactId>
</overlay>
</overlays>

</configuration>
</plugin>
</plugins>
</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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
[/code]

事实上,system-main只依赖timetable-system模块,但我们需要把一个活动模块也同时发布时,需要把活动模块作为war依赖引入主模块。

<dependency>
<groupId>com.benben</groupId>
<artifactId>timetable-activity</artifactId>
<version>${project.version}</version>
<type>war</type>
</dependency>
1
2
3
4
5
6
1
2
3
4
5
6
[/code]

通过maven-war-plugin合并其它的war模块。
packagingExcludes标签,合并的时候,不包括其它模块的WEB-INF/web.xml文件。

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<packagingExcludes>WEB-INF/web.xml</packagingExcludes>
<overlays>
<overlay>
<groupId>com.benben</groupId>
<artifactId>timetable-activity</artifactId>
</overlay>
</overlays>

</configuration>
</plugin>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[/code]


4、调试

多web项目在eclipse+tomcat环境下部署调试和单个maven web项目的方式是一样的:

创建server,打开window-prefrences-server-runTime Environment-new ….选择tomcat路径,jdk信息,这一步和原来完全一样。
打开server视图,new server,这里选中刚才创建的tomcat,点击next,这步只需要主web项目添加进来即可。
双击创建的server,选择将项目发布到tomcat的webapps目录下,点击pushlish,完成。

再去tomcat目录,发现已经将合并后的web发布到对应目录下了。 依赖的打包方式为jar的maven module已经被打包成jar发布到lib下了,而依赖的打包方式为war则和之前一样,编译文件会发布到classes下。不过你可以打个断点试试,依然可以调试,修改一段代码,发现热部署也是可以的,这样就和原来的开发习惯完全一样了。





注意:

如果项目发布不成功,查看主模块(system-main)的编译配置是否正确。



依赖的jar包没有发布到lib目录。

http://blog.csdn.net/jrainbow/article/details/38762041

依赖的打包方式为jar的maven module没有被打包成jar发布到lib下,查看Java Build Path的各项配置是否正确。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: