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>
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,835评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,598评论 1 295
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,569评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,159评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,533评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,710评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,923评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,674评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,421评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,622评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,115评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,428评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,114评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,097评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,875评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,753评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,649评论 2 271

推荐阅读更多精彩内容

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