在第一篇里主要介绍了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的输出日志了吧?还有疑问?请留言。

作者:zlwen
链接:https://www.jianshu.com/p/2f7080a4858c
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

maven install过程相关推荐

  1. 解决IDEA中进行maven install报:系统资源不足的问题

    一.背景 最近在idea中使用maven对公司的项目进行install的时候老是出现系统资源不足的问题导致install失败,在网上搜索也没找到很好的答案,自己不断摸索,最终在idea的配置里面找到了 ...

  2. maven安装过程以及手动添加jar包到本地仓库

    Maven安装过程及手动添加JAR包到本地仓库详解 https://blog.csdn.net/niityzu/article/details/50997544 分类: Maven(1) 版权声明:本 ...

  3. maven install后,java -jar XXXX.jar运行---找不到主类问题 以及 虚拟机中执行jar包后 访问页面出现Java heap space等其他问题

    这是前几天遇到的问题了,当天晚上想写下来来着,后来有事情就一直搁置到现在了. 由于我想将SpringCloud项目都导出jar包在虚拟机上运行,然后本地访问,所以先将SpringCloud中的注册中心 ...

  4. maven install 报错 source 1.5 中不支持 lambda 表达式

    maven install 报错 source 1.5 中不支持 lambda 表达式 maven-compiler-plugin 在编译的时候如果不指定jdk的版本,会默认使用jdk1.5 所以在编 ...

  5. Maven教程(3)--Maven导入工程常见问题(编码、MavenArchiver、Lifecycle Mapping、maven install 没有反应)...

    常见错误: 常见错误一:These projects must be migrated to correctly function in this version of MyEclipse 需要修改编 ...

  6. 针对maven install 报错:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1 解决方案...

    项目运行环境:jdk1.8+tomcat8 maven install 时报错:Failed to execute goal org.apache.maven.plugins:maven-compil ...

  7. eclipse中的maven build 、maven clean 、 maven install作用

    eclipse中的maven build .maven clean . maven install作用 转载于:https://www.cnblogs.com/ZeGod/p/10483605.htm ...

  8. eclipse中的maven build、maven clean、maven install和maven test的区别

    eclipse中的maven build.maven clean.maven install和maven test的区别 https://www.cnblogs.com/Marydon20170307 ...

  9. 【maven install报错】Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war

    maven install报错如下: [INFO] Scanning for projects... [INFO] Downloading: http://xxx.xx.xx.x:xxxx/nexus ...

  10. 【maven install报错】Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile

    maven install之后报的错误如下: [INFO] Scanning for projects... [INFO] Downloading: http://xxx.xxx.xxx.xxx:xx ...

最新文章

  1. java-web中的分层1-dao
  2. 关于质粒,学生物的你应该知道的那些事儿
  3. Transformer总结(2022版)
  4. begin again
  5. Redis和DB数据一致性解决方案
  6. 使用python将excel数据导入数据库
  7. JVM—GC垃圾回收器总结
  8. [导入]Reporting Services 3: 报表模型项目
  9. phpzend框架_PHP框架Zend
  10. Verilog 语言2选1数据选择器
  11. 移动端屏幕宽度自适应原理及实现
  12. jsp文件上传图片到服务器
  13. gatk过滤_详解GATK突变硬过滤 | 群体遗传专题
  14. 从C端市场延伸至B端业务拓展,流利说深耕行业英语价值几何?
  15. xshell6 评估期已过 解决办法
  16. 微信登录API使用小结
  17. http://www.dewen.net.cn/q/17095/SQL:用一条SQL语句统计出符合条件的内容
  18. WebStorm 配置SVN 提交代码“Commit dialog:Local Changes ReFrech”卡死
  19. 第十四章:监测和维护活动目录(一)(译自WindowsServer2008ActiveDirectoryResourceKit)
  20. 《设计模式之禅》-建造者模式

热门文章

  1. Delphi 日期函数列表
  2. Python---基础-运算符int和range函数
  3. 写代码:实现用户输入用户名和密码,当用户名为seven且密码为123时,显示登录成功,否则失败,失败时允许重复输入三次。...
  4. 36. 打印数组的主次对角线
  5. 跟踪wordcount计数器的运行信息
  6. linux pmap命令
  7. 记所内部“与顶级会议作者面对面”系列学术活动交流感受
  8. 统计字符串出现的次数(参照传智播客视频)
  9. C#效率优化(2)-- 方法内联
  10. 2016、11、17