在上一篇里主要介绍了maven的几个核心概念,这一篇里我们就以一个简单的例子来分析整个maven运行的过程。构建所使用的项目结构如下:
主要是一个echo项目,其包含了两个module,分别是api和biz。echo项目的pom.xml的内容如下:
<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>org.maven</groupId> <artifactId>echo</artifactId> <version>1.0.0</version> <packaging>pom</packaging> <modules> <module>api</module> <module>biz</module> </modules></project>
这里有个比较费解的地方就是<packaging>pom</packaging>。若<packaging>元素的内容是jar,那么我们很好理解,也就是说这个项目最终会被打包成一个jar包。那<packaging>元素为pom又是什么意思呢?从字面上的意思来看,这个项目将打包成一个pom。我们不妨去maven仓库里去瞧瞧(前提是已经在项目下运行了mvn install命令)。我们发现在<maven仓库路径>/org/maven/echo/1.0.0目录下有一个echo-1.0.0.pom文件,细心的读者可能已经发现这个文件其实和echo项目中的pom.xml是同一个文件。这样做的目的是什么呢?还记得上一篇中说过的PO对象吧,我们说过PO对象也是有继承关系的,比如说这里echo项目对应的PO对象就是api项目对应的PO对象的父对象(api项目是echo项目的一个module,在api项目的pom.xml中<parent>元素的内容所对应的就是echo项目),而echo项目PO对象的父对象又是哪个呢?答案是Super POM对应的PO对象。这就是maven中project inheritance的概念。当实际执行maven命令的时候,会根据project inheritance关系对项目的pom.xml进行转化,得到真正执行时所用到的pom.xml,即所谓的effective pom。而<packaging>pom</packaging>的作用就在这里。因此可以得到一个结论:所有<packaging>元素为pom的项目其实并不会输出一个可供外部使用,类似于jar包的东西。这类项目的作用有两个:
管理子项目
例如这里的api和biz就是echo项目的两个module。若没有echo这个父项目,我们需要到api和biz两个项目下分别执行mvn install命令才能完成整个构建过程,而有了echo这个父项目之后,我们只需在echo项目中执行mvn install即可,maven会解析pom.xml,发现该项目有api和biz两个module,它会分别到这两个项目下去执行mvn install命令。当module数量比较多的时候,能大大提高构建的效率。
管理继承属性
比如api和biz都需要某个依赖,那么在echo项目的pom.xml中声明即可,因为根据PO对象的继承关系,api和biz项目会继承echo项目的依赖,这样就可以减少一些重复的输入。
effective pom包含了当前项目的PO对象,直到Super POM对应的PO对象中的信息。要看一个项目的effective pom,只需在项目中执行
mvn help:effective-pom
命令即可查看。这里顺带说一句,有的同学可能不理解上面这个命令是什么意思。maven命令的语法为
mvn [options] [goal(s)] [phase(s)]
这里出现了上篇中讲述的两个概念:goal和phase。maven允许你执行一个或者多个goals/phases。很明显这面的命令help:effective-pom并不是一个phase(maven构建过程中的phase请参考上一篇文章),那么也就是说它是一个goal。对这个goal只不过是采用了缩写的形式,其全称是这样的:
org.apache.maven.plugins:maven-help-plugin:2.2:effective-pom
以分号为分隔符,包含了groupId,artifactId,version,goal四部分。若groupId为org.apache.maven.plugins则可以使用上述的简写形式。也就是说
mvn help:effective-pom mvn org.apache.maven.plugins:maven-help-plugin:2.2:effective-pom
是等价的,都是执行了maven-help-plugin这个plugin中的effective-pom这个goal。
好了,继续回到effective pom。我们说过maven在真正构建的时候用的就是effective pom,那么说明effective pom中包含了构建的所有信息,我们以biz项目中的effective pom为例来看下effective pom长什么样子。在biz项目中执行mvn help:effective-pom命令,你会得到如下输出:
<?xml version="1.0"?> <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> <parent> <groupId>org.maven</groupId> <artifactId>echo</artifactId> <version>1.0.0</version> </parent> <groupId>org.maven</groupId> <artifactId>echo-biz</artifactId> <version>1.0.0</version> <dependencies> <dependency> <groupId>org.maven</groupId> <artifactId>echo-api</artifactId> <version>1.0.0</version> <scope>compile</scope> </dependency> </dependencies> <build> <sourceDirectory>/Users/allstarw/workspace/own-proj/misc/maven/echo/biz/src/main/java</sourceDirectory> <scriptSourceDirectory>/Users/allstarw/workspace/own-proj/misc/maven/echo/biz/src/main/scripts</scriptSourceDirectory> <testSourceDirectory>/Users/allstarw/workspace/own-proj/misc/maven/echo/biz/src/test/java</testSourceDirectory> <outputDirectory>/Users/allstarw/workspace/own-proj/misc/maven/echo/biz/target/classes</outputDirectory> <testOutputDirectory>/Users/allstarw/workspace/own-proj/misc/maven/echo/biz/target/test-classes</testOutputDirectory> <resources> <resource> <directory>/Users/allstarw/workspace/own-proj/misc/maven/echo/biz/src/main/resources</directory> </resource> </resources> <testResources> <testResource>
<directory>/Users/allstarw/workspace/own-proj/misc/maven/echo/biz/src/test/resources</directory> </testResource> </testResources> <directory>/Users/allstarw/workspace/own-proj/misc/maven/echo/biz/target</directory> <finalName>echo-biz-1.0.0</finalName> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>2.4.1</version> <executions> <execution> <id>default-clean</id> <phase>clean</phase> <goals> <goal>clean</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.3.1</version> <executions> <execution> <id>default-install</id> <phase>install</phase> <goals> <goal>install</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>2.5</version> <executions> <execution> <id>default-resources</id> <phase>process-resources</phase> <goals> <goal>resources</goal> </goals> </execution> <execution> <id>default-testResources</id> <phase>process-test-resources</phase> <goals> <goal>testResources</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.10</version> <executions> <execution> <id>default-test</id> <phase>test</phase> <goals> <goal>test</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <executions> <execution> <id>default-testCompile</id> <phase>test-compile</phase> <goals> <goal>testCompile</goal> </goals> </execution> <execution> <id>default-compile</id> <phase>compile</phase> <goals> <goal>compile</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-jar-plugin</artifactId> <version>2.3.2</version> <executions> <execution> <id>default-jar</id> <phase>package</phase> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.7</version> <executions> <execution> <id>default-deploy</id> <phase>deploy</phase> <goals> <goal>deploy</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
篇幅有点长,省略了部分内容。对比biz项目的pom.xml,我们发现effective pom中增加了Super POM中继承过来的一些配置,比如说<sourceDirectory>定义了biz项目的源码路径,以及Lifecycle中各个phase绑定的goal:
[phase] [goal] compile maven-compiler-plugin:2.3.2:compile package maven-jar-plugin:2.3.2:jar install maven-install-plugin:2.3.1:install ... ...
有了effective pom的概念之后,再来看maven构建的输出日志,是不是有点豁然开朗的感觉?
[INFO] Scanning for projects...[INFO] ------------------------------------------------------------------------[INFO] Reactor Build Order: [INFO] [INFO] echo[INFO] echo-api[INFO] echo-biz[INFO] [INFO] ------------------------------------------------------------------------[INFO] Building echo 1.0.0[INFO] ------------------------------------------------------------------------[INFO] [INFO] --- maven-install-plugin:2.3.1:install (default-install) @ echo ---[INFO] Installing /Users/allstarw/workspace/own-proj/misc/maven/echo/pom.xml to /Users/allstarw/.m2/repository/org/maven/echo/1.0.0/echo-1.0.0.pom[INFO] [INFO] ------------------------------------------------------------------------[INFO] Building echo-api 1.0.0[INFO] ------------------------------------------------------------------------[INFO] [INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ echo-api ---[debug] execute contextualize[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent![INFO] skip non existing resourceDirectory /Users/allstarw/workspace/own-proj/misc/maven/echo/api/src/main/resources[INFO] [INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ echo-api ---[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent![INFO] Compiling 1 source file to /Users/allstarw/workspace/own-proj/misc/maven/echo/api/target/classes[INFO] [INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ echo-api ---[debug] execute contextualize[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent![INFO] skip non existing resourceDirectory /Users/allstarw/workspace/own-proj/misc/maven/echo/api/src/test/resources[INFO] [INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ echo-api ---[INFO] No sources to compile[INFO] [INFO] --- maven-surefire-plugin:2.10:test (default-test) @ echo-api ---[INFO] No tests to run.[INFO] Surefire report directory: /Users/allstarw/workspace/own-proj/misc/maven/echo/api/target/surefire-reports ------------------------------------------------------- T E S T S------------------------------------------------------- Results : Tests run: 0, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] --- maven-jar-plugin:2.3.2:jar (default-jar) @ echo-api ---[INFO] Building jar: /Users/allstarw/workspace/own-proj/misc/maven/echo/api/target/echo-api-1.0.0.jar[INFO] [INFO] --- maven-install-plugin:2.3.1:install (default-install) @ echo-api ---[INFO] Installing /Users/allstarw/workspace/own-proj/misc/maven/echo/api/target/echo-api-1.0.0.jar to /Users/allstarw/.m2/repository/org/maven/echo-api/1.0.0/echo-api-1.0.0.jar[INFO] Installing /Users/allstarw/workspace/own-proj/misc/maven/echo/api/pom.xml to /Users/allstarw/.m2/repository/org/maven/echo-api/1.0.0/echo-api-1.0.0.pom[INFO] [INFO] ------------------------------------------------------------------------[INFO] Building echo-biz 1.0.0[INFO] ------------------------------------------------------------------------[INFO] [INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ echo-biz ---[debug] execute contextualize[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent![INFO] skip non existing resourceDirectory /Users/allstarw/workspace/own-proj/misc/maven/echo/biz/src/main/resources[INFO] [INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ echo-biz ---[INFO] Nothing to compile - all classes are up to date[INFO] [INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ echo-biz ---[debug] execute contextualize[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent![INFO] skip non existing resourceDirectory /Users/allstarw/workspace/own-proj/misc/maven/echo/biz/src/test/resources[INFO] [INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ echo-biz ---[INFO] No sources to compile[INFO] [INFO] --- maven-surefire-plugin:2.10:test (default-test) @ echo-biz ---[INFO] No tests to run.[INFO] Surefire report directory: /Users/allstarw/workspace/own-proj/misc/maven/echo/biz/target/surefire-reports ------------------------------------------------------- T E S T S------------------------------------------------------- Results : Tests run: 0, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] --- maven-jar-plugin:2.3.2:jar (default-jar) @ echo-biz ---[INFO] [INFO] --- maven-install-plugin:2.3.1:install (default-install) @ echo-biz ---[INFO] Installing /Users/allstarw/workspace/own-proj/misc/maven/echo/biz/target/echo-biz-1.0.0.jar to /Users/allstarw/.m2/repository/org/maven/echo-biz/1.0.0/echo-biz-1.0.0.jar[INFO] Installing /Users/allstarw/workspace/own-proj/misc/maven/echo/biz/pom.xml to /Users/allstarw/.m2/repository/org/maven/echo-biz/1.0.0/echo-biz-1.0.0.pom[INFO] ------------------------------------------------------------------------[INFO] Reactor Summary:[INFO] [INFO] echo .............................................. SUCCESS [0.461s][INFO] echo-api .......................................... SUCCESS [2.581s][INFO] echo-biz .......................................... SUCCESS [0.382s][INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 3.593s[INFO] Finished at: Wed Jul 06 00:22:50 CST 2016[INFO] Final Memory: 10M/156M[INFO] ------------------------------------------------------------------------
日志的输出十分清楚,分别构建echo,api,biz这三个项目(因为biz项目依赖api项目,因此api项目需要首先构建)。对每个项目构建时,将lifecycle中的phase所对应的goal依次执行。现在你能够看懂maven的输出日志了吧?还有疑问?请留言。
https://mp.weixin.qq.com/s/vkvYJnKfQyuUeD_BDQy_1g
获取更多学习资料,可以加群:473984645或扫描下方二维码