这又是一个系列,一个要把Maven讲透的系列,希望能够对大家有帮助!
前言
通过前面几篇文章,我把Maven中的坐标、仓库、依赖、生命周期和插件这五大核心概念进行了总结,掌握了这五大核心概念,基本上也就把住了Maven的脉。从这篇文章开始,我们将从实际的应用场景出发,总结Maven的一些比较常见的用户,通过这些常见的用户来更好的把握Maven,学习Maven。
我们的需求
在前后台分离,微服务大行其道的今天,我们的应用不再是一个超级大的包了,而是分成了多个子项目模块,每个子项目模块都是一个单独的工程项目。这就出现应用多的情况,此时如果你负责一个功能开发时,需要修改多个子项目模块时,就需要去修改不同的工程项目,当你开发完进行联调时,就需要一次构建多个工程项目,当出现问题时,又可能同时修改多个工程项目,此时你是一个一个工程项目的去构建呢?还是希望有一种办法一次性可以构建多个工程项目。
当然了,我们肯定希望存在一种办法,我们通过点击某个按钮就可以开始构建多个工程项目,然后我们去喝杯咖啡的。那我们的这种需求在Maven中是否能实现呢?毫无疑问,Maven是可以搞定这个问题的,这就是Maven中的聚合,通过聚合我们就可以解决这个痛点问题。下面我就通过实际的项目来说说Maven中的聚合到底是个什么鬼。
聚合实战
现在我准备了两个基于Maven的子工程项目,分别是Project-A和Project-B。这两个项目都可以单独编译,单独构建。但是为了能够使用一条命令就可以构建Project-A和Project-B这两个子工程项目,我们需要创建一个额外的名为Project-Aggregator的工程项目,然后通过该模块构建整个项目的所有模块。由于这个Project-Aggregator的工程项目是一个聚合项目,它是不需要src和test目录的,只需要有一个POM就OK了,下面就是这个Project-Aggregator工程项目的POM文件内容:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jellythink.AggregatorDemo</groupId>
<artifactId>Project-Aggregator</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Project-Aggregator</name>
<modules>
<module>../Project-A</module>
<module>../Project-B</module>
</modules>
</project>
接下来,我们在Project-Aggregator工程项目目录下执行mvn clean package
命令,就会看到以下输出:
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] Project-A
[INFO] Project-B
[INFO] Project-Aggregator
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Project-A 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ Project-A ---
[INFO] Deleting E:\Code\Spring\Project-A\target
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ Project-A ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:\Code\Spring\Project-A\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ Project-A ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to E:\Code\Spring\Project-A\target\classes
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ Project-A ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:\Code\Spring\Project-A\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ Project-A ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to E:\Code\Spring\Project-A\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ Project-A ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.jellythink.AggregatorDemo.AppTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.032 s - in com.jellythink.AggregatorDemo.AppTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO]
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ Project-A ---
[INFO] Building jar: E:\Code\Spring\Project-A\target\Project-A-1.0-SNAPSHOT.jar
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Project-B 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ Project-B ---
[INFO] Deleting E:\Code\Spring\Project-B\target
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ Project-B ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:\Code\Spring\Project-B\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ Project-B ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to E:\Code\Spring\Project-B\target\classes
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ Project-B ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:\Code\Spring\Project-B\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ Project-B ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to E:\Code\Spring\Project-B\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ Project-B ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.jellythink.AggregatorDemo.AppTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.046 s - in com.jellythink.AggregatorDemo.AppTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO]
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ Project-B ---
[INFO] Building jar: E:\Code\Spring\Project-B\target\Project-B-1.0-SNAPSHOT.jar
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Project-Aggregator 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ Project-Aggregator ---
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] Project-A .......................................... SUCCESS [ 3.513 s]
[INFO] Project-B .......................................... SUCCESS [ 1.424 s]
[INFO] Project-Aggregator ................................. SUCCESS [ 0.047 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.093 s
[INFO] Finished at: 2019-04-22T21:54:08+08:00
[INFO] Final Memory: 19M/274M
[INFO] ------------------------------------------------------------------------
从输出可以看到,我们在构建Project-Aggregator项目时,Project-A和Project-B就会一同被构建,在存在多个项目时,这是非常方便的。
总结
对于聚合,内容不多,但是这种使用手法在实际工作中我们也会碰到,以后你在碰到这种用法时,不要疑惑。希望这篇文章能够帮助到你,更加丰富你的Maven知识,让你对Maven的使用有一个更新的认识。
果冻想,玩代码,玩技术!
2019年4月22日,于内蒙古呼和浩特。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。