1、新建父项目modules-container,选择maven project,作为所有子模块的容器
2、新建子项目modules-demo01,选择maven module,module name为子模块名,parent project选择父项目modules-container
3、创建成功之后,父项目自动对子项目进行聚合
<groupId>com.maven.demo</groupId>
<artifactId>modules-container</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- mavan模块化开发,父项目更改packaging为pom -->
<packaging>pom</packaging>
<name>modules-container</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- 使用 dependencyManagement对所有依赖进行管理,以便子项目继承使用-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 自动聚合子模块项目 -->
<modules>
<module>modules-demo01</module>
<module>modules-demo02</module>
</modules>
4、子项目自动继承父项目,修改pom.xml将公共groupId和version去掉
<!-- 自动继承父项目 -->
<parent>
<groupId>com.maven.demo</groupId>
<artifactId>modules-container</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<!-- 去掉groupId、version,继承使用父项目的 -->
<!--
<groupId>com.maven.demo</groupId>
<version>0.0.1-SNAPSHOT</version>
-->
<artifactId>modules-demo01</artifactId>
<name>modules-demo01</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<!-- 使用父项目dependencyManagement管理的版本
<version>3.8.1</version>
-->
<scope>test</scope>
</dependency>
</dependencies>
5、按照modules-demo01创建子项目modules-demo02,最终的目录结构如图:
补充:创建module子项目过程,eclipse可能抛出如图异常:
details信息为:
Unable to create project from archetype [org.apache.maven.archetypes:maven-archetype-quickstart:RELEASE]
Unable to read parent POM
解决的办法:将父项目pom.xml中所有中文删除,包括注释,所有子项目创建完毕后,需要的话再加回来。
6、然后对父项目进行编译、测试、打包、安装操作,实现对所有聚合子项目的共同操作
[INFO] Scanning for projects…
[INFO] ————————————————————————
[INFO] Reactor Build Order:
[INFO]
[INFO] modules-container
[INFO] modules-demo01
[INFO] modules-demo02
[INFO]
[INFO] Using the builderorg.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]
[INFO] ————————————————————————
[INFO] Building modules-container 0.0.1-SNAPSHOT
[INFO] ————————————————————————
[INFO]
[INFO] — maven-clean-plugin:2.5:clean (default-clean) @ modules-container —
[INFO] Deleting E:\workspace_II\modules-container\target
[INFO]
[INFO] ————————————————————————
[INFO] Building modules-demo01 0.0.1-SNAPSHOT
[INFO] ————————————————————————
[INFO]
[INFO] — maven-clean-plugin:2.5:clean (default-clean) @ modules-demo01 —
[INFO] Deleting E:\workspace_II\modules-container\modules-demo01\target
[INFO]
[INFO] — maven-resources-plugin:2.6:resources (default-resources) @ modules-demo01 —
[INFO] Using ‘UTF-8’ encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:\workspace_II\modules-container\modules-demo01\src\main\resources
[INFO]
[INFO] — maven-compiler-plugin:2.5.1:compile (default-compile) @ modules-demo01 —
[INFO] Compiling 1 source file to E:\workspace_II\modules-container\modules-demo01\target\classes
[INFO]
[INFO] ————————————————————————
[INFO] Building modules-demo02 0.0.1-SNAPSHOT
[INFO] ————————————————————————
[INFO]
[INFO] — maven-clean-plugin:2.5:clean (default-clean) @ modules-demo02 —
[INFO] Deleting E:\workspace_II\modules-container\modules-demo02\target
[INFO]
[INFO] — maven-resources-plugin:2.6:resources (default-resources) @ modules-demo02 —
[INFO] Using ‘UTF-8’ encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:\workspace_II\modules-container\modules-demo02\src\main\resources
[INFO]
[INFO] — maven-compiler-plugin:2.5.1:compile (default-compile) @ modules-demo02 —
[INFO] Compiling 1 source file to E:\workspace_II\modules-container\modules-demo02\target\classes
[INFO] ————————————————————————
[INFO] Reactor Summary:
[INFO]
[INFO] modules-container …………………………… SUCCESS [ 0.187 s]
[INFO] modules-demo01 ……………………………… SUCCESS [ 0.786 s]
[INFO] modules-demo02 ……………………………… SUCCESS [ 0.042 s]
[INFO] ————————————————————————
[INFO] BUILD SUCCESS
[INFO] ————————————————————————
[INFO] Total time: 1.101 s
[INFO] Finished at: 2016-08-01T18:24:51+08:00
[INFO] Final Memory: 10M/153M
[INFO] ————————————————————————