在实际开发中,为了得到更清晰的设计及更高的重用性,我们往往会采用各种方式对软件划分模块。
遇到一些问题:
比如:我们需要对每一个模块分别进行构建,非常浪费我们的时间。每个模块的JAR包版本的统一管理变成有点困难。
Maven方案:
Maven使用了聚合特性,把项目的各个模块聚合在一起,一次性构建所有模块。 通过”继承特性“则帮助抽取各模块相同的依赖和插件等有共性的配置,在简化POM的同时,还能促进各个模块配置的一致性。
1.1 聚合
场景假设:
你有一个项目,包含两个模块
account-email
Account-persist
有一个简单的需求,你想一次构建两个项目,而不是两个模块目录下分别执行mvn命令。
使用maven的聚合方案:
创建一个聚合模块,account-aggregator,
聚合模块特点:
1.本身也是一个maven项目
2.必须要有自己的POM,但packaging的值为POM,而像accout-email为JAR或不声明(默认为jar)
3.声明需要聚合的模块module,每一个module的值都是一个当前POM的相对目录。
maven中的两种结构:
1)父子目录结构关系:聚合模块是被聚合模块父文件夹
配置方式:<module>account-email</module>
<module>account-persist</module>
2)平行目录结构:聚合模块是被聚合模块是平级的文件夹(我通常使用该方式)
配置方式:<module>../account-email</module>
<module>../account-persist</module>
4.聚合模块只包含一个pom.xml文件,不需要src/main/java,src/test/java等目录,原因是聚合模块仅仅是帮助聚合其它模块构建的工具,它本身并无实质内容的。
聚合模块POM配置如下:
Pom.xml
-----------------------------------------------------------------------------------
<projectxmlns="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.0http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.juvenxu.mvnbook.account</groupId>
<artifactId>account-aggregator</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>AccountAggregator</name>
<modules>
<module>../account-email</module>
<module>../account-persist</module>
</modules>
</project>
-----------------------------------------------------------------------------------
Delete the unnecessary item.
Note:Just only keep the pom.xml file in account-aggregator project.
聚合模块执行原理:
Maven会首先解析聚合模块的POM,分析要构建的模块,并计算出一个反应堆构建顺序(Reactor Build Order),然后根据这个顺序依次建各个模块。
我们执行install命令,观察其执行情况.
mvn install
[INFO] Scanning forprojects...
[INFO]------------------------------------------------------------------------
[INFO] Reactor BuildOrder:
[INFO]
[INFO] Account Email
[INFO] AccountPersist
[INFO] AccountAggregator
[INFO]
[INFO]------------------------------------------------------------------------
[INFO] BuildingAccount Email 1.0.0-SNAPSHOT
[INFO]------------------------------------------------------------------------
[INFO]
[INFO] ---maven-resources-plugin:2.6:resources (default-resources) @ account-email ---
[INFO] Using 'UTF-8'encoding to copy filtered resources.
[INFO] Copying 1resource
[INFO]
[INFO] ---maven-compiler-plugin:3.1:compile (default-compile) @ account-email ---
[INFO] Nothing tocompile - all classes are up to date
[INFO]
[INFO] ---maven-resources-plugin:2.6:testResources (default-testResources) @account-email ---
[INFO] Using 'UTF-8'encoding to copy filtered resources.
[INFO] Copying 1resource
[INFO]
[INFO] ---maven-compiler-plugin:3.1:testCompile (default-testCompile) @ account-email ---
[INFO] Nothing tocompile - all classes are up to date
[INFO]
[INFO] ---maven-surefire-plugin:2.12.4:test (default-test) @ account-email ---
[INFO] Surefirereport directory:D:\Workspaces\eclipse-jee-oxygen_x86_64\account-email\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Runningcom.juvenxu.mvnbook.account.email.AccountEmailServiceTest
Test Subject
<h3>Test</h3>
Tests run: 1,Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.707 sec
Results :
Tests run: 1,Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ---maven-jar-plugin:2.4:jar (default-jar) @ account-email ---
[INFO] Building jar:D:\Workspaces\eclipse-jee-oxygen_x86_64\account-email\target\account-email-1.0.0-SNAPSHOT.jar
[INFO]
[INFO] ---maven-install-plugin:2.4:install (default-install) @ account-email ---
[INFO] InstallingD:\Workspaces\eclipse-jee-oxygen_x86_64\account-email\target\account-email-1.0.0-SNAPSHOT.jarto D:\Workspaces\maven_repository\com\juven\mvnbook\account\account-email\1.0.0-SNAPSHOT\account-email-1.0.0-SNAPSHOT.jar
[INFO] InstallingD:\Workspaces\eclipse-jee-oxygen_x86_64\account-email\pom.xml toD:\Workspaces\maven_repository\com\juven\mvnbook\account\account-email\1.0.0-SNAPSHOT\account-email-1.0.0-SNAPSHOT.pom
[INFO]
[INFO]------------------------------------------------------------------------
[INFO] BuildingAccount Persist 1.0.0-SNAPSHOT
[INFO]------------------------------------------------------------------------
[INFO]
[INFO] ---maven-resources-plugin:2.6:resources (default-resources) @ account-persist ---
[INFO] Using 'UTF-8'encoding to copy filtered resources.
[INFO] Copying 1resource
[INFO]
[INFO] ---maven-compiler-plugin:3.1:compile (default-compile) @ account-persist ---
[INFO] Nothing tocompile - all classes are up to date
[INFO]
[INFO] ---maven-resources-plugin:2.6:testResources (default-testResources) @account-persist ---
[INFO] Using 'UTF-8'encoding to copy filtered resources.
[INFO] Copying 1resource
[INFO]
[INFO] ---maven-compiler-plugin:3.1:testCompile (default-testCompile) @ account-persist---
[INFO] Nothing tocompile - all classes are up to date
[INFO]
[INFO] ---maven-surefire-plugin:2.12.4:test (default-test) @ account-persist ---
[INFO] Surefirereport directory:D:\Workspaces\eclipse-jee-oxygen_x86_64\account-persist\target\surefire-reports
<