Maven进阶笔记

适用人群

  • 对maven有一些基本的概念,会一些日常的操作
  • 希望深入了解maven的概念和机制,解释一些底层的细节

生命周期、阶段、插件、目标

  • mvn clean dependency:copy-dependencies package

    • 执行clean生命周期中clean阶段之前(包括)所有的阶段
    • 执行 dependency插件copy-dependencies这个目标
    • 执行default生命周期中package阶段之前(包括)所有的阶段
  • clean

    
        pre-clean
        clean
        post-clean
     
    
        
          org.apache.maven.plugins:maven-clean-plugin:2.5:clean
        
    
    
  • default

      
        validate
        initialize
        generate-sources
        process-sources
        generate-resources
        process-resources
        compile
        process-classes
        generate-test-sources
        process-test-sources
        generate-test-resources
        process-test-resources
        test-compile
        process-test-classes
        test
        prepare-package
        package
        pre-integration-test
        integration-test
        post-integration-test
        verify
        install
        deploy
    
    
  • site

    
        pre-site
        site
        post-site
        site-deploy
    
    
        
          org.apache.maven.plugins:maven-site-plugin:3.3:site
        
        
          org.apache.maven.plugins:maven-site-plugin:3.3:deploy
        
    
    

不同打包类型绑定的阶段、插件和目标

  • pom

    
        
          org.apache.maven.plugins:maven-install-plugin:2.4:install
        
        
          org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy
        
    
    
  • jar

    
        
          org.apache.maven.plugins:maven-resources-plugin:2.6:resources
        
        
          org.apache.maven.plugins:maven-compiler-plugin:3.1:compile
        
        
          org.apache.maven.plugins:maven-resources-plugin:2.6:testResources
        
        
          org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile
        
        
          org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test
        
        
          org.apache.maven.plugins:maven-jar-plugin:2.4:jar
        
        
          org.apache.maven.plugins:maven-install-plugin:2.4:install
        
        
          org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy
        
    
    
    
  • war

    
        
          org.apache.maven.plugins:maven-resources-plugin:2.6:resources
        
        
          org.apache.maven.plugins:maven-compiler-plugin:3.1:compile
        
        
          org.apache.maven.plugins:maven-resources-plugin:2.6:testResources
        
        
          org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile
        
        
          org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test
        
        
          org.apache.maven.plugins:maven-war-plugin:2.2:war
        
        
          org.apache.maven.plugins:maven-install-plugin:2.4:install
        
        
          org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy
        
    
    

POM(Project Object Model)

  • Super POM

    • 位置 $MAVEN_HOME/lib/maven-model-builder-x.y.z.jar!/org/apache/maven/model/pom-4.0.0.xml
    
    4.0.0
    
    
      
        central
        Central Repository
        http://repo.maven.apache.org/maven2
        default
        
          false
        
      
    
    
    
      
        central
        Central Repository
        http://repo.maven.apache.org/maven2
        default
        
          false
        
        
          never
        
      
    
    
    
      ${project.basedir}/target
      ${project.build.directory}/classes
      ${project.artifactId}-${project.version}
      ${project.build.directory}/test-classes
      ${project.basedir}/src/main/java
      src/main/scripts
      ${project.basedir}/src/test/java
      
        
          ${project.basedir}/src/main/resources
        
      
      
        
          ${project.basedir}/src/test/resources
        
      
      
        
        
        
          
            maven-antrun-plugin
            1.3
          
          
            maven-assembly-plugin
            2.2-beta-5
          
          
            maven-dependency-plugin
            2.1
          
          
            maven-release-plugin
            2.0
          
        
      
    
    
    
      ${project.build.directory}/site
    
    
    
      
      
        release-profile
    
        
          
            performRelease
            true
          
        
    
        
          
            
              true
              maven-source-plugin
              
                
                  attach-sources
                  
                    jar
                  
                
              
            
            
              true
              maven-javadoc-plugin
              
                
                  attach-javadocs
                  
                    jar
                  
                
              
            
            
              true
              maven-deploy-plugin
              
                true
              
            
          
        
      
    
    
    
    
    • 继承和聚合
    
        4.0.0
       
        
              com.mycompany.app
              my-app
              1
        
       
        my-module
    
    
    
        4.0.0
       
        com.mycompany.app
        my-app
        1
        pom
       
        
              my-module
        
    
    
  • 内置properties

value evaluation result common examples
project.* POM content(参考 Super POM) {project.build.finalName}
{project.build.directory}
project.basedir the directory containing the pom.xml file ${project.basedir}
project.baseUri the directory containing the pom.xml file as URI ${project.baseUri}
build.timestamp
maven.build.timestamp
the UTC timestamp of build start, in yyyy-MM-dd'T'HH:mm:ss'Z' default format, which can be overridden with maven.build.timestamp.format POM property ${maven.build.timestamp}
* user properties, set from CLI with -Dproperty=value ${skipTests}
* model properties, such as project properties set in the pom ${any.key}
maven.home The path to the current Maven home. ${maven.home}
maven.version The version number of the current Maven execution (since 3.0.4). For example, "3.0.5". ${maven.version}
maven.build.version The full build version of the current Maven execution (since 3.0.4). For example, "Apache Maven 3.2.2 (r01de14724cdef164cd33c7c8c2fe155faf9602da; 2013-02-19T14:51:28+01:00)". ${maven.build.version}
maven.repo.local The repository on the local machine Maven shall use to store installed and downloaded artifacts (POMs, JARs, etc). ${user.home}/.m2/repository
* Java system properties(see JDK reference) {java.home}
env.*
*
environment variables ${env.PATH}
settings.* Local user settings (see settings reference) ${settings.localRepository}

依赖管理

  • 解决依赖冲突,最近原则

    • D 1.0 生肖
      A
    ├── B
    │   └── C
    │       └── D 2.0
    └── E
        └── D 1.0
    
    • D 3.0 生效
      A
    ├── B
    │   └── C
    │       └── D 2.0
    ├── E
    │   └── D 1.0
    │
    └── D 3.0    
    │
    └── D 4.0    
    
  • scope

    • compile 默认类型 参与所有机制

    • provided 代表由环境提供,不用在打包时包含

    • runtime 只在运行时需要,不参与编译

    • test 只在测试代码中需要

    • system 类似provided 不从maven仓库拿包,从系统路径拿包

    • import 特殊类型,只导入dependencyManagement

    • 不同类型的生效关系

    scope\生效阶段 编译时 测试 运行时 传递依赖
    compile
    provided
    runtime
    test
    system
    • 上层依赖scope与传递依赖冲突时的判定
    上层scope\传递依赖 compile provided runtime test
    compile compile - runtime -
    provided provided - provided -
    runtime runtime - runtime -
    test test - test -
  • type

    • pom
    • jar
    • maven-plugin
    • ebb
    • war
    • ear
    • rar
    • java-source
    • javadoc
    • ejb-client
    • test-jar
  • classifier

    • sources
    • javadoc
    • client
    • tests
  • 最小粒度依赖 {groupId, artifactId, type, classifier}

  • 默认依赖 {groupId, artifactId, 'jar', null}

  • 阻止传递依赖

    • 被动: optional B->C(optional) A->B-✕(C)
    • 主动: exclusion

仓库配置

  • 仓库加载顺序

    • $maven_home/conf/settings.xml
    • $user_home/.m2/settings.xml
    • local pom
    • parent pom
    • super pom
    • dependency pom
  • 镜像,替换某个(某些)仓库

    • 替换中央仓库(mirrorOf)
  
    
      other-mirror
      Other Mirror Repository
      https://other-mirror.repo.other-company.com/maven2
      central
    
  

你可能感兴趣的:(Maven进阶笔记)