Maven学习总结(八)——使用Maven构建多模块项目


  在平时的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 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3 <modelVersion>4.0.0</modelVersion>
4
5 <groupId>me.gacl</groupId>
6 <artifactId>system-parent</artifactId>
7 <version>1.0-SNAPSHOT</version>
8 <packaging>pom</packaging>
9
10 <name>system-parent</name>
11 <url>http://maven.apache.org</url>
12
13 <properties>
14 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15 </properties>
16
17 <dependencies>
18 <dependency>
19 <groupId>junit</groupId>
20 <artifactId>junit</artifactId>
21 <version>3.8.1</version>
22 <scope>test</scope>
23 </dependency>
24 </dependencies>
25 </project>

二、创建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文件自动添加了如下内容:

<modules>
<module>system-domain</module>
</modules>

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

 1 <?xml version="1.0" encoding="UTF-8"?>
2 <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">
3 <modelVersion>4.0.0</modelVersion>
4
5 <groupId>me.gacl</groupId>
6 <artifactId>system-parent</artifactId>
7 <version>1.0-SNAPSHOT</version>
8 <packaging>pom</packaging>
9
10 <name>system-parent</name>
11 <url>http://maven.apache.org</url>
12
13 <properties>
14 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15 </properties>
16
17 <dependencies>
18 <dependency>
19 <groupId>junit</groupId>
20 <artifactId>junit</artifactId>
21 <version>3.8.1</version>
22 <scope>test</scope>
23 </dependency>
24 </dependencies>
25 <modules>
26 <module>system-domain</module>
27 </modules>
28 </project>

  修改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 <?xml version="1.0"?>
2 <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"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4 <modelVersion>4.0.0</modelVersion>
5 <parent>
6 <groupId>me.gacl</groupId>
7 <artifactId>system-parent</artifactId>
8 <version>1.0-SNAPSHOT</version>
9 </parent>
10
11 <artifactId>system-domain</artifactId>
12 <packaging>jar</packaging>
13
14 <name>system-domain</name>
15 <url>http://maven.apache.org</url>
16 </project>

三、创建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 <?xml version="1.0" encoding="UTF-8"?>
2 <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">
3 <modelVersion>4.0.0</modelVersion>
4
5 <groupId>me.gacl</groupId>
6 <artifactId>system-parent</artifactId>
7 <version>1.0-SNAPSHOT</version>
8 <packaging>pom</packaging>
9
10 <name>system-parent</name>
11 <url>http://maven.apache.org</url>
12
13 <properties>
14 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15 </properties>
16
17 <dependencies>
18 <dependency>
19 <groupId>junit</groupId>
20 <artifactId>junit</artifactId>
21 <version>3.8.1</version>
22 <scope>test</scope>
23 </dependency>
24 </dependencies>
25 <modules>
26 <module>system-domain</module>
27 <module>system-dao</module>
28 </modules>
29 </project>

  修改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 <?xml version="1.0"?>
2 <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"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4 <modelVersion>4.0.0</modelVersion>
5 <parent>
6 <groupId>me.gacl</groupId>
7 <artifactId>system-parent</artifactId>
8 <version>1.0-SNAPSHOT</version>
9 </parent>
10
11 <artifactId>system-dao</artifactId>
12 <packaging>jar</packaging>
13
14 <name>system-dao</name>
15 <url>http://maven.apache.org</url>
16 <properties>
17 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18 </properties>
19 <dependencies>
20 <!--system-dao需要使用到system-domain中的类,所以需要添加对system-domain模块的依赖-->
21 <dependency>
22 <groupId>me.gacl</groupId>
23 <artifactId>system-domain</artifactId>
24 <version>${project.version}</version>
25 </dependency>
26 </dependencies>
27 </project>

四、创建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 <?xml version="1.0" encoding="UTF-8"?>
2 <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">
3 <modelVersion>4.0.0</modelVersion>
4
5 <groupId>me.gacl</groupId>
6 <artifactId>system-parent</artifactId>
7 <version>1.0-SNAPSHOT</version>
8 <packaging>pom</packaging>
9
10 <name>system-parent</name>
11 <url>http://maven.apache.org</url>
12
13 <properties>
14 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15 </properties>
16
17 <dependencies>
18 <dependency>
19 <groupId>junit</groupId>
20 <artifactId>junit</artifactId>
21 <version>3.8.1</version>
22 <scope>test</scope>
23 </dependency>
24 </dependencies>
25 <modules>
26 <module>system-domain</module>
27 <module>system-dao</module>
28 <module>system-service</module>
29 </modules>
30 </project>

  修改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 <?xml version="1.0"?>
2 <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"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4 <modelVersion>4.0.0</modelVersion>
5 <parent>
6 <groupId>me.gacl</groupId>
7 <artifactId>system-parent</artifactId>
8 <version>1.0-SNAPSHOT</version>
9 </parent>
10
11 <artifactId>system-service</artifactId>
12 <packaging>jar</packaging>
13
14 <name>system-service</name>
15 <url>http://maven.apache.org</url>
16 <properties>
17 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18 </properties>
19 <dependencies>
20 <!--
21 system-service依赖system-dao和system-domain,
22 但是我们只需添加system-dao的依赖即可,因为system-dao已经依赖了system-domain
23 -->
24 <dependency>
25 <groupId>me.gacl</groupId>
26 <artifactId>system-dao</artifactId>
27 <version>${project.version}</version>
28 </dependency>
29 </dependencies>
30 </project>

五、创建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,如下图所示:

  

  里面的内容为

<html>
<body>
<h2>Hello World!</h2>
</body>
</html>

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

  

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

 1 <?xml version="1.0" encoding="UTF-8"?>
2 <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">
3 <modelVersion>4.0.0</modelVersion>
4
5 <groupId>me.gacl</groupId>
6 <artifactId>system-parent</artifactId>
7 <version>1.0-SNAPSHOT</version>
8 <packaging>pom</packaging>
9
10 <name>system-parent</name>
11 <url>http://maven.apache.org</url>
12
13 <properties>
14 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15 </properties>
16
17 <dependencies>
18 <dependency>
19 <groupId>junit</groupId>
20 <artifactId>junit</artifactId>
21 <version>3.8.1</version>
22 <scope>test</scope>
23 </dependency>
24 </dependencies>
25 <modules>
26 <module>system-domain</module>
27 <module>system-dao</module>
28 <module>system-service</module>
29 <module>system-web</module>
30 </modules>
31 </project>

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

 1 <?xml version="1.0"?>
2 <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"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4 <modelVersion>4.0.0</modelVersion>
5 <parent>
6 <groupId>me.gacl</groupId>
7 <artifactId>system-parent</artifactId>
8 <version>1.0-SNAPSHOT</version>
9 </parent>
10
11 <artifactId>system-web</artifactId>
12 <packaging>war</packaging>
13
14 <name>system-web Maven Webapp</name>
15 <url>http://maven.apache.org</url>
16 <dependencies>
17 <!--
18 system-web依赖system-service
19 -->
20 <dependency>
21 <groupId>me.gacl</groupId>
22 <artifactId>system-service</artifactId>
23 <version>${project.version}</version>
24 </dependency>
25 </dependencies>
26 <build>
27 <finalName>system-web</finalName>
28 </build>
29 </project>

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

六、编译运行项目

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

 1 <?xml version="1.0"?>
2 <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"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4 <modelVersion>4.0.0</modelVersion>
5 <parent>
6 <groupId>me.gacl</groupId>
7 <artifactId>system-parent</artifactId>
8 <version>1.0-SNAPSHOT</version>
9 </parent>
10
11 <artifactId>system-web</artifactId>
12 <packaging>war</packaging>
13
14 <name>system-web Maven Webapp</name>
15 <url>http://maven.apache.org</url>
16 <dependencies>
17 <!--
18 system-web依赖system-service
19 -->
20 <dependency>
21 <groupId>me.gacl</groupId>
22 <artifactId>system-service</artifactId>
23 <version>${project.version}</version>
24 </dependency>
25 </dependencies>
26 <build>
27 <finalName>system-web</finalName>
28 <plugins>
29 <!--配置Jetty插件-->
30 <plugin>
31 <groupId>org.mortbay.jetty</groupId>
32 <artifactId>maven-jetty-plugin</artifactId>
33 </plugin>
34 </plugins>
35 </build>
36 </project>

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

mvn clean install

  如下图所示:

  

  

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

  

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

mvn jetty:run

  如下图所示:

  

  

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

  

七、导入Eclipse中进行开发

  操作步骤如下所示:

  

  

  

  

  

Maven学习总结(8)——使用Maven构建多模块项目的更多相关文章

  1. Maven学习:Eclipse使用maven构建web项目(转)

    Maven学习:Eclipse使用maven构建web项目(转) 8.更改class路径:右键项目,Java Build Path -> Source 下面应该有4个文件夹.src/main/j ...

  2. (转)Maven学习总结(八)——使用Maven构建多模块项目

    孤傲苍狼只为成功找方法,不为失败找借口! Maven学习总结(八)——使用Maven构建多模块项目 在平时的Javaweb项目开发中为了便于后期的维护,我们一般会进行分层开发,最常见的就是分为doma ...

  3. (转)Maven学习总结(三)——使用Maven构建项目

    孤傲苍狼 只为成功找方法,不为失败找借口! Maven学习总结(三)——使用Maven构建项目 maven作为一个高度自动化构建工具,本身提供了构建项目的功能,下面就来体验一下使用maven构建项目的 ...

  4. Maven学习(五)使用Maven构建多模块项目

    使用Maven构建多模块项目 一般的web项目构成: 建立解决方案目录parent 首先使用命令进入到我们需要建立maven项目的目录: mvn archetype:generate -DgroupI ...

  5. 使用Maven构建多模块项目

    [转] 使用Maven构建多模块项目 在平时的Javaweb项目开发中为了便于后期的维护,我们一般会进行分层开发,最常见的就是分为domain(域模型层).dao(数据库访问层).service(业务 ...

  6. Maven学习笔记-03-Eclipse下maven项目在Tomcat7和Jetty6中部署调试

    现在最新的Eclipse Luna Release 已经内置了Maven插件,这让我们的工作简洁了不少,只要把项目直接导入就可以,不用考虑插件什么的问题,但是导入之后的项目既可以部署在Tomcat也可 ...

  7. Maven学习笔记-04-Eclipse下maven项目在Tomcat7和Jetty6中部署调试

    现在最新的Eclipse Luna Release 已经内置了Maven插件,这让我们的工作简洁了不少,只要把项目直接导入就可以,不用考虑插件什么的问题,但是导入之后的项目既可以部署在Tomcat也可 ...

  8. maven 单独构建多模块项目中的单个模块

    maven 单独构建多模块项目中的单个模块, maven选项说明 -pl, --projects Build specified reactor projects instead of all pro ...

  9. Maven单独构建多模块项目中的单个模块

    Maven单独构建多模块项目中的单个模块   说明: 1.可能存在的场景,多模块项目没有互相引用,那么此时可以单独构建单个项目,指定到子模块的pom.xml文件即可完成编译. 2.如果多模块项目各自都 ...

随机推荐

  1. VS2010 使用TeeChart绘图控件 - 之二 - 绘制图形(折线图,柱状图)

    1.前期准备 具体可见VS2010 使用TeeChart绘图控件 - 之一 控件和类的导入 1. 1 添加TeeChart控件,给控件添加变量m_TeeChart 添加TeeChart控件,右击控件, ...

  2. Java 集合列表排序

    主要是实现Comparator接口 数组排序: //按最后更新时间降序排列,时间相同的按照文件名生序排列 Arrays.sort(files, new Comparator<File>() ...

  3. Gym - 101982B 2018-2019 ACM-ICPC Pacific Northwest Regional Contest (Div. 1) B. Coprime Integers Mobius+容斥 ab间gcd(x,y)=1的对数

    题面 题意:给你 abcd(1e7),求a<=x<=b,c<=y<=d的,gcd(x,y)=1的数量 题解:经典题目,求从1的到n中选x,从1到m中选y的,gcd(x,y)=k ...

  4. 阿拉伯数字1与英语字母l造成的代码bug

    <img src="./images/demo3/1a.png" /> <img src="./images/demo3/la.png" /& ...

  5. php微信开放平台--第三方网页微信扫码登录(OAuth2.0)

    第一.OAuth2.0 OAuth(开放授权)是一个开放标准,允许用户让第三方应用访问该用户在某一网站上存储的私密的资源(如照片,视频,联系人列表),而无需将用户名和密码提供给第三方应用. 允许用户提 ...

  6. php从数据库读取中文显示问号??的解决办法

    出错原因:1.数据库编码格式不对 2.PHP编码格式不对 3.浏览器编码格式不对 上面三者编码格式不统一,就会出现问题 数据库读取的时候在mysqli_connect()之后要设置连接字符编码mysq ...

  7. javascript中window,document,body的解释

    解释javascript中window,document,body的区别: window对象表示浏览器中打开的窗口,即是一个浏览器窗口只有一个window对象. document对象是载入浏览器的ht ...

  8. $Vijos P1250$

    背包? 跑完并查集 分组背包完事 #include <bits/stdc++.h> #define rep(i,j,n) for(register int i=j;i<=n;i++) ...

  9. python之set集合及深浅拷贝

    一.知识点补充 1.1字符串的基本操作 li =["李李嘉诚", "麻花藤", "⻩黄海海峰", "刘嘉玲"] s = ...

  10. HTML+CSS(12)

    n  CSS浮动和清除 Float:让元素浮动,取值:left(左浮动).right(右浮动). Clear:清除浮动,取值:left(清除左浮动).right(清除右浮动).both(同时清除上面的 ...