详解使用Maven构建多模块项目(图文)

时间:2021-10-23 18:14:04

maven多模块项目,适用于一些比较大的项目,通过合理的模块拆分,实现代码的复用,便于维护和管理。尤其是一些开源框架,也是采用多模块的方式,提供插件集成,用户可以根据需要配置指定的模块。

项目结构如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
test-hd-parent   (父级)
  ---pom.xml
  ---test-hd-api     (第三方接口层)
     ----pom.xml
  ---test-hd-foundation  (基础工具层)
     ----pom.xml
  ---test-hd-resource  (资源层)
     ----pom.xml
  ---test-hd-service    (逻辑业务层)
     ----pom.xml
  ---test-hd-modules    (web层)
     ----pom.xml
       ---test-hd-www      (web模块1)
         ----pom.xml
       ---test-hd-admin      (web模块2)
         ----pom.xml  

创建一个父maven工程

新建一个maven项目,选择存储位置,并选择创建一个简单的maven工程

详解使用Maven构建多模块项目(图文)

详解使用Maven构建多模块项目(图文)

输入group id、artifact id、packaging,packaging选择pom包

详解使用Maven构建多模块项目(图文)    

生成父工程,pom.xml如下

详解使用Maven构建多模块项目(图文)   

删除工程中的src 目录

详解使用Maven构建多模块项目(图文)    

创建子模块

右击父工程名---》new---》project,然后选择新建一个maven module工程

详解使用Maven构建多模块项目(图文)

详解使用Maven构建多模块项目(图文)    

设置子工程名以及父工程,再设置快速创建模式

详解使用Maven构建多模块项目(图文)

详解使用Maven构建多模块项目(图文)    

得到子工程(test-hd-api,第三方接口层),设置编译的jdk

详解使用Maven构建多模块项目(图文)

详解使用Maven构建多模块项目(图文)    

同理设置,子模块:test-hd-foundation(基础工具层)、test-hd-resource(资源层) 、test-hd-service(逻辑业务层)
新建test-hd-modules (web层),选择创建一个a simple project,输入group id、artifact id、packaging,packaging选择pom包

详解使用Maven构建多模块项目(图文)

详解使用Maven构建多模块项目(图文)        

创建web子模块

web子模块在建在test-hd-modules (web层)里面,右击test-hd-modules 工程名---》new---》project,然后选择新建一个maven module工程,设置子工程名以及父工程,选择新建web项目

详解使用Maven构建多模块项目(图文)    

配置maven web项目,参照:【maven】eclipse 使用maven创建java web项目

同理可以配置其他的web子模块   test-hd-admin(web模块2)

详解使用Maven构建多模块项目(图文)    

配置个模块的依赖

在parent项目pom.xml中建立依赖管理(dependencymanagement)

?
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
<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>com.hd</groupid>
 <artifactid>test-hd-parent</artifactid>
 <version>0.0.1-snapshot</version>
 <packaging>pom</packaging>
 <modules>
  <module>test-hd-api</module>
  <module>test-hd-service</module>
  <module>test-hd-resource</module>
  <module>test-hd-foundation</module>
  <module>test-hd-modules</module>
 </modules>
 
 
 <!-- maven依赖 -->
 <dependencymanagement>
 
  <dependencies>
   <!-- hd -->
   <dependency>
    <groupid>com.hd</groupid>
    <artifactid>test-hd-api</artifactid>
    <version>0.0.1-snapshot</version>
   </dependency>
 
   <dependency>
    <groupid>com.hd</groupid>
    <artifactid>test-hd-service</artifactid>
    <version>0.0.1-snapshot</version>
   </dependency>
 
   <dependency>
    <groupid>com.hd</groupid>
    <artifactid>test-hd-resource</artifactid>
    <version>0.0.1-snapshot</version>
   </dependency>
 
   <dependency>
    <groupid>com.hd</groupid>
    <artifactid>test-hd-foundation</artifactid>
    <version>0.0.1-snapshot</version>
   </dependency>
 
   <!-- servlet -->
   <dependency>
    <groupid>javax.servlet</groupid>
    <artifactid>javax.servlet-api</artifactid>
    <version>3.0.1</version>
    <scope>provided</scope>
   </dependency>
   <dependency>
    <groupid>javax.servlet.jsp</groupid>
    <artifactid>jsp-api</artifactid>
    <version>2.2</version>
    <scope>provided</scope>
   </dependency>
 
   <!-- jstl -->
   <dependency>
    <groupid>javax.servlet</groupid>
    <artifactid>jstl</artifactid>
    <version>1.2</version>
   </dependency>
 
   <dependency>
    <groupid>taglibs</groupid>
    <artifactid>standard</artifactid>
    <version>1.1.2</version>
   </dependency>
 
   <dependency>
    <groupid>junit</groupid>
    <artifactid>junit</artifactid>
    <version>3.8.1</version>
    <scope>test</scope>
   </dependency>
 
  </dependencies>
 </dependencymanagement>
 
</project>

test-hd-foundation中的依赖

?
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
<?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.hd</groupid>
  <artifactid>test-hd-parent</artifactid>
  <version>0.0.1-snapshot</version>
 </parent>
 <artifactid>test-hd-foundation</artifactid>
 
 <dependencies>
 
  <!-- servlet -->
  <dependency>
   <groupid>javax.servlet</groupid>
   <artifactid>jstl</artifactid>
  </dependency>
 
  <dependency>
   <groupid>taglibs</groupid>
   <artifactid>standard</artifactid>
  </dependency>
 
  <dependency>
   <groupid>junit</groupid>
   <artifactid>junit</artifactid>
  </dependency>
 </dependencies>
 
 <build>
  <plugins>
   <!-- define the project compile level -->
   <plugin>
    <groupid>org.apache.maven.plugins</groupid>
    <artifactid>maven-compiler-plugin</artifactid>
    <version>2.3.2</version>
    <configuration>
     <source>1.7</source>
     <target>1.7</target>
    </configuration>
   </plugin>
  </plugins>
 </build>
</project>

test-hd-api中的依赖关系

?
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
<?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.hd</groupid>
  <artifactid>test-hd-parent</artifactid>
  <version>0.0.1-snapshot</version>
 </parent>
 <artifactid>test-hd-api</artifactid>
 <dependencies>
 
  <dependency>
   <groupid>com.hd</groupid>
   <artifactid>test-hd-foundation</artifactid>
  </dependency>
 
  <!-- servlet -->
  <dependency>
   <groupid>javax.servlet</groupid>
   <artifactid>jstl</artifactid>
  </dependency>
 
  <dependency>
   <groupid>taglibs</groupid>
   <artifactid>standard</artifactid>
  </dependency>
 
  <dependency>
   <groupid>junit</groupid>
   <artifactid>junit</artifactid>
  </dependency>
 </dependencies>
 <build>
  <plugins>
   <!-- define the project compile level -->
   <plugin>
    <groupid>org.apache.maven.plugins</groupid>
    <artifactid>maven-compiler-plugin</artifactid>
    <version>2.3.2</version>
    <configuration>
     <source>1.7</source>
     <target>1.7</target>
    </configuration>
   </plugin>
  </plugins>
  <finalname>test-hd-api</finalname>
 </build>
</project>

test-hd-resource中的依赖关系

?
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
<?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.hd</groupid>
  <artifactid>test-hd-parent</artifactid>
  <version>0.0.1-snapshot</version>
 </parent>
 <artifactid>test-hd-resource</artifactid>
 <dependencies>
 
  <dependency>
   <groupid>junit</groupid>
   <artifactid>junit</artifactid>
  </dependency>
 </dependencies>
 
 <build>
  <plugins>
   <!-- define the project compile level -->
   <plugin>
    <groupid>org.apache.maven.plugins</groupid>
    <artifactid>maven-compiler-plugin</artifactid>
    <version>2.3.2</version>
    <configuration>
     <source>1.7</source>
     <target>1.7</target>
    </configuration>
   </plugin>
  </plugins>
 </build>
</project>

test-hd-service中的依赖关系

?
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
<?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.hd</groupid>
  <artifactid>test-hd-parent</artifactid>
  <version>0.0.1-snapshot</version>
 </parent>
 <artifactid>test-hd-service</artifactid>
 <dependencies>
 
  <dependency>
   <groupid>com.hd</groupid>
   <artifactid>test-hd-foundation</artifactid>
  </dependency>
 
  <dependency>
   <groupid>com.hd</groupid>
   <artifactid>test-hd-api</artifactid>
  </dependency>
 
  <!-- servlet -->
  <dependency>
   <groupid>javax.servlet</groupid>
   <artifactid>jstl</artifactid>
  </dependency>
 
  <dependency>
   <groupid>taglibs</groupid>
   <artifactid>standard</artifactid>
  </dependency>
 
  <dependency>
   <groupid>junit</groupid>
   <artifactid>junit</artifactid>
  </dependency>
 </dependencies>
 
 
 <build>
  <plugins>
   <!-- define the project compile level -->
   <plugin>
    <groupid>org.apache.maven.plugins</groupid>
    <artifactid>maven-compiler-plugin</artifactid>
    <version>2.3.2</version>
    <configuration>
     <source>1.7</source>
     <target>1.7</target>
    </configuration>
   </plugin>
  </plugins>
  <finalname>test-hd-service</finalname>
 </build>
</project>

test-hd-module中的依赖关系 

?
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
<?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/xsd/maven-4.0.0.xsd">
 <modelversion>4.0.0</modelversion>
 <parent>
  <groupid>com.hd</groupid>
  <artifactid>test-hd-parent</artifactid>
  <version>0.0.1-snapshot</version>
 </parent>
 
 <artifactid>test-hd-modules</artifactid>
 <packaging>pom</packaging>
 
 <modules>
  <module>test-hd-www</module>
  <module>test-hd-admin</module>
 </modules>
 
 <dependencies>
 
  <dependency>
   <groupid>com.hd</groupid>
   <artifactid>test-hd-foundation</artifactid>
  </dependency>
 
  <dependency>
   <groupid>com.hd</groupid>
   <artifactid>test-hd-service</artifactid>
  </dependency>
  <dependency>
   <groupid>com.hd</groupid>
   <artifactid>test-hd-api</artifactid>
  </dependency>
 
  <dependency>
   <groupid>com.hd</groupid>
   <artifactid>test-hd-resource</artifactid>
  </dependency>
 
  <!-- servlet -->
  <dependency>
   <groupid>javax.servlet</groupid>
   <artifactid>jstl</artifactid>
  </dependency>
 
  <dependency>
   <groupid>taglibs</groupid>
   <artifactid>standard</artifactid>
  </dependency>
 
  <dependency>
   <groupid>junit</groupid>
   <artifactid>junit</artifactid>
  </dependency>
 
 </dependencies>
</project>

 test-hd-www中的依赖关系 

?
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
<?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.hd</groupid>
  <artifactid>test-hd-modules</artifactid>
  <version>0.0.1-snapshot</version>
 </parent>
 <artifactid>test-hd-www</artifactid>
 <packaging>war</packaging>
 
 <build>
  <plugins>
   <!-- define the project compile level -->
   <plugin>
    <groupid>org.apache.maven.plugins</groupid>
    <artifactid>maven-compiler-plugin</artifactid>
    <version>2.3.2</version>
    <configuration>
     <source>1.7</source>
     <target>1.7</target>
    </configuration>
   </plugin>
  </plugins>
  <finalname>test-hd-www</finalname>
 </build>
 
</project>

最后使用maven-update整个工程,右击父工程名--》maven--》update project

详解使用Maven构建多模块项目(图文)    

打包和发布

打包,右击父工程名 test-hd-parent---->run as--->maven install

详解使用Maven构建多模块项目(图文)

详解使用Maven构建多模块项目(图文)

打包web子工程,右击工程名test-hd-www--->run as ---> maven build...---> goals: clean package--->run

详解使用Maven构建多模块项目(图文)

详解使用Maven构建多模块项目(图文)  

      详解使用Maven构建多模块项目(图文)      

右击工程名test-hd-www,进行刷新,找到war包,放到tomcat的webapps中,启动tomcat,即可访问工程http://localhost:8080/test-hd-www

详解使用Maven构建多模块项目(图文)

详解使用Maven构建多模块项目(图文)    

可以去tomcat下面webapps》test-hd-www》web-inf》lib中,看到引用的jar包

详解使用Maven构建多模块项目(图文)    

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。 

原文链接:http://www.cnblogs.com/h--d/p/6001366.html