maven内部运行原理解析(二)

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

 

    在第一篇里主要介绍了maven的几个核心概念,这一篇里我们就以一个简单的例子来分析整个maven运行的过程。构建所使用的项目结构如下:

项目结构


主要是一个echo项目,其包含了两个module,分别是api和biz。echo项目的pom.xml的内容如下:

 


    4.0.0
    org.maven
    echo
    1.0.0
    pom

    
        api
        biz
    

    这里有个比较费解的地方就是pom。若元素的内容是jar,那么我们很好理解,也就是说这个项目最终会被打包成一个jar包。那元素为pom又是什么意思呢?从字面上的意思来看,这个项目将打包成一个pom。我们不妨去maven仓库里去瞧瞧(前提是已经在项目下运行了mvn install命令)。我们发现在/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中元素的内容所对应的就是echo项目),而echo项目PO对象的父对象又是哪个呢?答案是Super POM对应的PO对象。这就是maven中project inheritance的概念。当实际执行maven命令的时候,会根据project inheritance关系对项目的pom.xml进行转化,得到真正执行时所用到的pom.xml,即所谓的effective pom。而pom的作用就在这里。因此可以得到一个结论:所有元素为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命令,你会得到如下输出:

    
    
    4.0.0
    
      org.maven
      echo
      1.0.0
    
    org.maven
    echo-biz
    1.0.0
    
      
        org.maven
        echo-api
        1.0.0
        compile
      
    
    
      /Users/allstarw/workspace/own-proj/misc/maven/echo/biz/src/main/java
      /Users/allstarw/workspace/own-proj/misc/maven/echo/biz/src/main/scripts
      /Users/allstarw/workspace/own-proj/misc/maven/echo/biz/src/test/java
      /Users/allstarw/workspace/own-proj/misc/maven/echo/biz/target/classes
      /Users/allstarw/workspace/own-proj/misc/maven/echo/biz/target/test-classes
      
        
          /Users/allstarw/workspace/own-proj/misc/maven/echo/biz/src/main/resources
        
      
      
        
          /Users/allstarw/workspace/own-proj/misc/maven/echo/biz/src/test/resources
        
      
      /Users/allstarw/workspace/own-proj/misc/maven/echo/biz/target
      echo-biz-1.0.0
      
        
          maven-clean-plugin
          2.4.1
          
            
              default-clean
              clean
              
                clean
              
            
          
        
        
          maven-install-plugin
          2.3.1
          
            
              default-install
              install
              
                install
              
            
          
        
        
          maven-resources-plugin
          2.5
          
            
              default-resources
              process-resources
              
                resources
              
            
            
              default-testResources
              process-test-resources
              
                testResources
              
            
          
        
        
          maven-surefire-plugin
          2.10
          
            
              default-test
              test
              
                test
              
            
          
        
        
          maven-compiler-plugin
          2.3.2
          
            
              default-testCompile
              test-compile
              
                testCompile
              
            
            
              default-compile
              compile
              
                compile
              
            
          
        
        
          maven-jar-plugin
          2.3.2
          
            
              default-jar
              package
              
                jar
              
            
          
        
        
          maven-deploy-plugin
          2.7
          
            
              default-deploy
              deploy
              
                deploy
              
            
          
        
      
    
    

篇幅有点长,省略了部分内容。对比biz项目的pom.xml,我们发现effective pom中增加了Super POM中继承过来的一些配置,比如说定义了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
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

转载于:https://my.oschina.net/newchaos/blog/1612755

你可能感兴趣的:(maven内部运行原理解析(二))