博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Maven构建多模块项目
阅读量:4358 次
发布时间:2019-06-07

本文共 9859 字,大约阅读时间需要 32 分钟。

  转自:

      在平时的Javaweb项目 开发中为了便于后期的维护,我们一般会进行分层开发,最常见的就是分为domain(域模型层)、dao(数据库访问层)、service(业务逻辑 层)、web(表现层),这样分层之后,各个层之间的职责会比较明确,后期维护起来也相对比较容易,今天我们就是使用Maven来构建以上的各个层。

  项目结构如下:

  system-parent

        |----pom.xml
        |----system-domain
                |----pom.xml
        |----system-dao
                |----pom.xml
        |----system-service
                |----pom.xml
        |----system-web
                |----pom.xml

一、创建system-parent项目

  创建system-parent,用来给各个子模块继承。

  进入命令行,输入以下命令:

mvn archetype:create -DgroupId=me.gacl -DartifactId=system-parent -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

  如下图所示:

  

  命令执行完成之后可以看到在当前目录(C:\Documents and Settings\Administrator)生成了system-parent目录,里面有一个src目录和一个pom.xml文件,如下图所示:

  

  将src文件夹删除,然后修改pom.xml文件,将<packaging>jar</packaging>修改为<packaging>pom</packaging>,pom表示它是一个被继承的模块,修改后的内容如下:

1 
3
4.0.0
4 5
me.gacl
6
system-parent
7
1.0-SNAPSHOT
8
pom
9 10
system-parent
11
http://maven.apache.org
12 13
14
UTF-8
15
16 17
18
19
junit
20
junit
21
3.8.1
22
test
23
24
25

二、创建sytem-domain模块

  在命令行进入创建好的system-parent目录,然后执行下列命令:

mvn archetype:create -DgroupId=me.gacl -DartifactId=system-domain -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

  如下图所示:

  

  命令执行完成之后可以看到在system-parent目录中生成了system-domain,里面包含src目录和pom.xml文件。如下图所示:

  

  

  同时,在system-parent目录中的pom.xml文件自动添加了如下内容:

system-domain

  这时,system-parent的pom.xml文件如下:

1 
2
3
4.0.0
4 5
me.gacl
6
system-parent
7
1.0-SNAPSHOT
8
pom
9 10
system-parent
11
http://maven.apache.org
12 13
14
UTF-8
15
16 17
18
19
junit
20
junit
21
3.8.1
22
test
23
24
25
26
system-domain
27
28

  修改system-domain目录中的pom.xml文件,把<groupId>me.gacl</groupId><version>1.0-SNAPSHOT</version>去掉,加上<packaging>jar</packaging>,因为groupId和version会继承system-parent中的groupId和version,packaging设置打包方式为jar

  修改过后的pom.xml文件如下:

1 
2
4
4.0.0
5
6
me.gacl
7
system-parent
8
1.0-SNAPSHOT
9
10 11
system-domain
12
jar
13 14
system-domain
15
http://maven.apache.org
16

三、创建sytem-dao模块

  在命令行进入创建好的system-parent目录,然后执行下列命令:

mvn archetype:create -DgroupId=me.gacl -DartifactId=system-dao -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

  如下图所示:

  

  命令执行完成之后可以看到在system-parent目录中生成了system-dao,里面包含src目录和pom.xml文件。如下图所示:

  

  同时,在system-parent目录中的pom.xml文件自动变成如下内容:

1 
2
3
4.0.0
4 5
me.gacl
6
system-parent
7
1.0-SNAPSHOT
8
pom
9 10
system-parent
11
http://maven.apache.org
12 13
14
UTF-8
15
16 17
18
19
junit
20
junit
21
3.8.1
22
test
23
24
25
26
system-domain
27
system-dao
28
29

  修改system-dao目录中的pom.xml文件,,把<groupId>me.gacl</groupId><version>1.0-SNAPSHOT</version>去掉,加上<packaging>jar</packaging>因为groupId和version会继承system-parent中的groupId和version,packaging设置打包方式为jar,同时添加对system-domain模块的依赖,修改后的内容如下:

1 
2
4
4.0.0
5
6
me.gacl
7
system-parent
8
1.0-SNAPSHOT
9
10 11
system-dao
12
jar
13 14
system-dao
15
http://maven.apache.org
16
17
UTF-8
18
19
20
21
22
me.gacl
23
system-domain
24
${project.version}
25
26
27

四、创建system-service模块

  在命令行进入创建好的system-parent目录,然后执行下列命令:

mvn archetype:create -DgroupId=me.gacl -DartifactId=system-service -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

  如下图所示:

  

  命令执行完成之后可以看到在system-parent目录中生成了system-service,里面包含src目录和pom.xml文件。如下图所示:

  

  同时,在system-parent目录中的pom.xml文件自动变成如下内容:

1 
2
3
4.0.0
4 5
me.gacl
6
system-parent
7
1.0-SNAPSHOT
8
pom
9 10
system-parent
11
http://maven.apache.org
12 13
14
UTF-8
15
16 17
18
19
junit
20
junit
21
3.8.1
22
test
23
24
25
26
system-domain
27
system-dao
28
system-service
29
30

  修改system-service目录中的pom.xml文件,,把<groupId>me.gacl</groupId><version>1.0-SNAPSHOT</version>去掉,加上<packaging>jar</packaging>因为groupId和version会继承system-parent中的groupId和version,packaging设置打包方式为jar,同时添加对system-dao模块的依赖system-service依赖system-dao和system-domain,但是我们只需添加system-dao的依赖即可,因为system-dao已经依赖了system-domain。修改后的内容如下:

1 
2
4
4.0.0
5
6
me.gacl
7
system-parent
8
1.0-SNAPSHOT
9
10 11
system-service
12
jar
13 14
system-service
15
http://maven.apache.org
16
17
UTF-8
18
19
20
24
25
me.gacl
26
system-dao
27
${project.version}
28
29
30

五、创建system-web模块

  在命令行进入创建好的system-parent目录,然后执行下列命令:

mvn archetype:create -DgroupId=me.gacl -DartifactId=system-web -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

  如下图所示:

  

  命令执行完成之后可以看到在system-parent目录中生成了system-web,里面包含src目录和pom.xml文件。如下图所示:

  

  在\system-web\src\main\webapp目录中还生成了一个简单的index.jsp,如下图所示:

  

  里面的内容为

 

Hello World!

  system-web\src\main\webapp\WEB-INF目录中生成了web.xml

  

  同时,在system-parent目录中的pom.xml文件自动变成如下内容:

1 
2
3
4.0.0
4 5
me.gacl
6
system-parent
7
1.0-SNAPSHOT
8
pom
9 10
system-parent
11
http://maven.apache.org
12 13
14
UTF-8
15
16 17
18
19
junit
20
junit
21
3.8.1
22
test
23
24
25
26
system-domain
27
system-dao
28
system-service
29
system-web
30
31

  修改system-web目录中的pom.xml文件,,把<groupId>me.gacl</groupId><version>1.0-SNAPSHOT</version>去掉,因为groupId和version会继承system-parent中的groupId和version,同时添加对system-service模块的依赖修改后的内容如下:

1 
2
4
4.0.0
5
6
me.gacl
7
system-parent
8
1.0-SNAPSHOT
9
10 11
system-web
12
war
13 14
system-web Maven Webapp
15
http://maven.apache.org
16
17
20
21
me.gacl
22
system-service
23
${project.version}
24
25
26
27
system-web
28
29

   注意,web项目的打包方式是war

六、编译运行项目

  经过上面的五个步骤,相关的模块全部创建完成,怎么运行起来呢。由于最终运行的是system-web模块,所以我们对该模块添加jetty支持,方便测试运行。修改system-web项目的pom.xml如下:

1 
2
4
4.0.0
5
6
me.gacl
7
system-parent
8
1.0-SNAPSHOT
9
10 11
system-web
12
war
13 14
system-web Maven Webapp
15
http://maven.apache.org
16
17
20
21
me.gacl
22
system-service
23
${project.version}
24
25
26
27
system-web
28
29
30
31
org.mortbay.jetty
32
maven-jetty-plugin
33
6.1.10
34
35
36

  在命令行进入system-parent目录,然后执行下列命令:

mvn clean install

  如下图所示:

  

  

  命令执行完后,在system-web目录下多出了target目录,里面有了system-web.war,如下图所示:

  

  命令行进入sytem-web目录,执行如下命令,启动jetty

mvn jetty:run

  如下图所示:

  

  

  启动jetty服务器后,访问http://localhost:8080/system-web/ 运行结果如下图所示:

  

转载于:https://www.cnblogs.com/1995hxt/p/5246364.html

你可能感兴趣的文章
uva11214 Guarding the Chessboard
查看>>
CentOS6.4下Git服务器Gitosis安装配置
查看>>
007 斐波那契数列
查看>>
《Docker入门实战》笔记(一)
查看>>
hdu 3635 Dragon Balls (并查集)
查看>>
文件操作
查看>>
7.java集合,泛型简单总结,IO流
查看>>
杭电2007 平方和与立方和
查看>>
JS邮箱验证-正则验证
查看>>
关于SQL查询效率,100w数据,查询只要1秒
查看>>
Quartz 2D绘图
查看>>
JS Fetch
查看>>
EJB 笔记
查看>>
【delete】Android自定义控件(四) 自定义ImageView动态设置ImageView的高度
查看>>
HDUOJ------(1230)火星A+B
查看>>
Servlet
查看>>
基于jquery地图特效全国网点查看代码
查看>>
【leetcode】867 - Transpose Matrix
查看>>
(转)Intellij Idea工具栏添加打开选中文件的资源管理器位置
查看>>
Linux 的面试小题 9
查看>>