Maven进阶笔记

适用人群

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

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

  • mvn clean dependency:copy-dependencies package

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

    <phases>
        <phase>pre-clean</phase>
        <phase>clean</phase>
        <phase>post-clean</phase>
     </phases>
    <default-phases>
        <clean>
          org.apache.maven.plugins:maven-clean-plugin:2.5:clean
        </clean>
    </default-phases>
    
  • default

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

    <phases>
        <phase>pre-site</phase>
        <phase>site</phase>
        <phase>post-site</phase>
        <phase>site-deploy</phase>
    </phases>
    <default-phases>
        <site>
          org.apache.maven.plugins:maven-site-plugin:3.3:site
        </site>
        <site-deploy>
          org.apache.maven.plugins:maven-site-plugin:3.3:deploy
        </site-deploy>
    </default-phases>
    

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

  • pom

    <phases>
        <install>
          org.apache.maven.plugins:maven-install-plugin:2.4:install
        </install>
        <deploy>
          org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy
        </deploy>
    </phases>
    
  • jar

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

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

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
    <project>
    <modelVersion>4.0.0</modelVersion>
    
    <repositories>
      <repository>
        <id>central</id>
        <name>Central Repository</name>
        <url>http://repo.maven.apache.org/maven2</url>
        <layout>default</layout>
        <snapshots>
          <enabled>false</enabled>
        </snapshots>
      </repository>
    </repositories>
    
    <pluginRepositories>
      <pluginRepository>
        <id>central</id>
        <name>Central Repository</name>
        <url>http://repo.maven.apache.org/maven2</url>
        <layout>default</layout>
        <snapshots>
          <enabled>false</enabled>
        </snapshots>
        <releases>
          <updatePolicy>never</updatePolicy>
        </releases>
      </pluginRepository>
    </pluginRepositories>
    
    <build>
      <directory>${project.basedir}/target</directory>
      <outputDirectory>${project.build.directory}/classes</outputDirectory>
      <finalName>${project.artifactId}-${project.version}</finalName>
      <testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
      <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
      <scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>
      <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
      <resources>
        <resource>
          <directory>${project.basedir}/src/main/resources</directory>
        </resource>
      </resources>
      <testResources>
        <testResource>
          <directory>${project.basedir}/src/test/resources</directory>
        </testResource>
      </testResources>
      <pluginManagement>
        <!-- NOTE: These plugins will be removed from future versions of the super POM -->
        <!-- They are kept for the moment as they are very unlikely to conflict with lifecycle mappings (MNG-4453) -->
        <plugins>
          <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.3</version>
          </plugin>
          <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.2-beta-5</version>
          </plugin>
          <plugin>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.1</version>
          </plugin>
          <plugin>
            <artifactId>maven-release-plugin</artifactId>
            <version>2.0</version>
          </plugin>
        </plugins>
      </pluginManagement>
    </build>
    
    <reporting>
      <outputDirectory>${project.build.directory}/site</outputDirectory>
    </reporting>
    
    <profiles>
      <!-- NOTE: The release profile will be removed from future versions of the super POM -->
      <profile>
        <id>release-profile</id>
    
        <activation>
          <property>
            <name>performRelease</name>
            <value>true</value>
          </property>
        </activation>
    
        <build>
          <plugins>
            <plugin>
              <inherited>true</inherited>
              <artifactId>maven-source-plugin</artifactId>
              <executions>
                <execution>
                  <id>attach-sources</id>
                  <goals>
                    <goal>jar</goal>
                  </goals>
                </execution>
              </executions>
            </plugin>
            <plugin>
              <inherited>true</inherited>
              <artifactId>maven-javadoc-plugin</artifactId>
              <executions>
                <execution>
                  <id>attach-javadocs</id>
                  <goals>
                    <goal>jar</goal>
                  </goals>
                </execution>
              </executions>
            </plugin>
            <plugin>
              <inherited>true</inherited>
              <artifactId>maven-deploy-plugin</artifactId>
              <configuration>
                <updateReleaseInfo>true</updateReleaseInfo>
              </configuration>
            </plugin>
          </plugins>
        </build>
      </profile>
    </profiles>
    
    </project>
    
    • 继承和聚合
    <project>
        <modelVersion>4.0.0</modelVersion>
       
        <parent>
              <groupId>com.mycompany.app</groupId>
              <artifactId>my-app</artifactId>
              <version>1</version>
        </parent>
       
        <artifactId>my-module</artifactId>
    </project>
    
    <project>
        <modelVersion>4.0.0</modelVersion>
       
        <groupId>com.mycompany.app</groupId>
        <artifactId>my-app</artifactId>
        <version>1</version>
        <packaging>pom</packaging>
       
        <modules>
              <module>my-module</module>
        </modules>
    </project>
    
  • 内置properties

value evaluation result common examples
project.* POM content(参考 Super POM) {project.version}<br>{project.build.finalName}
{project.artifactId} <br>{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) {user.home} <br>{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)
  <mirrors>
    <mirror>
      <id>other-mirror</id>
      <name>Other Mirror Repository</name>
      <url>https://other-mirror.repo.other-company.com/maven2</url>
      <mirrorOf>central</mirrorOf>
    </mirror>
  </mirrors>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Maven概述 Maven定义Maven是一个项目管理和整合,统一管理jar包的工具;Maven为开发者提供了一套...
    THQ的简书阅读 810评论 0 0
  • 所有项目的构建都是有生命周期的,这个生命周期包括:项目清理、初始化、编译、测试、打包、集成测试、验证、部署、站点生...
    zlcook阅读 2,833评论 0 21
  • 一、为什么使用Maven这样的构建工具【why】 ① 一个项目就是一个工程 如果项目非常庞大,就不适合使用pack...
    问题_解决_分享_讨论_最优阅读 1,258评论 0 16
  • 1 为什么使用Maven这样的构建工具 【Why】 1.1 一个项目就是一个工程 如果项目非常...
    coder_girl阅读 499评论 0 1
  • Maven讲义 Maven概述 Maven是什么 Maven是一个由Apache基金会维护的项目构建工具。 项目构...
    立廷浅阅读 464评论 0 0