在第一篇里主要介绍了maven的几个核心概念,这一篇里咱们就以一个简单的例子来分析整个maven运行的过程。构建所使用的项目结构以下:java
项目结构apache
主要是一个echo项目,其包含了两个module,分别是api和biz。echo项目的pom.xml的内容以下:api
<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包的东西。这类项目的做用有两个:maven
管理子项目
例如这里的api和biz就是echo项目的两个module。若没有echo这个父项目,咱们须要到api和biz两个项目下分别执行mvn install命令才能完成整个构建过程,而有了echo这个父项目以后,咱们只需在echo项目中执行mvn install便可,maven会解析pom.xml,发现该项目有api和biz两个module,它会分别到这两个项目下去执行mvn install命令。当module数量比较多的时候,能大大提升构建的效率。ui
管理继承属性
好比api和biz都须要某个依赖,那么在echo项目的pom.xml中声明便可,由于根据PO对象的继承关系,api和biz项目会继承echo项目的依赖,这样就能够减小一些重复的输入。spa
effective pom包含了当前项目的PO对象,直到Super POM对应的PO对象中的信息。要看一个项目的effective pom,只需在项目中执行debug
mvn help:effective-pom
命令便可查看。这里顺带说一句,有的同窗可能不理解上面这个命令是什么意思。maven命令的语法为日志
mvn [options] [goal(s)] [phase(s)]
这里出现了第一篇中讲述的两个概念:goal和phase。maven容许你执行一个或者多个goals/phases。很明显这面的命令help:effective-pom并非一个phase(maven构建过程当中的phase请参考上一篇文章),那么也就是说它是一个goal。对这个goal只不过是采用了缩写的形式,其全称是这样的:code
org.apache.maven.plugins:maven-help-plugin:2.2:effective-pom
以分号为分隔符,包含了groupId,artifactId,version,goal四部分。若groupId为org.apache.maven.plugins则可使用上述的简写形式。也就是说orm
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的输出日志了吧?还有疑问?请留言。
做者:zlwen 连接:https://www.jianshu.com/p/2f7080a4858c 來源:简书 著做权归做者全部。商业转载请联系做者得到受权,非商业转载请注明出处。