一點一點學maven(10)——eclipse實現maven模組化開發
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] ————————————————————————