Skip to content
hj yan edited this page Aug 10, 2018 · 1 revision

maven

maven生命周期

maven入门指南

POM

三套生命周期:clean, default, site

每套生命周期都包含了一些phase。这些phase的发生是有顺序的,例如在idea中的maven project->lifecycle 中执行install,会按顺序发生之前的phase,如compile,test,package直到install安装到本地仓库,但不会发生clean阶段,因为生命周期之间是独立的。

maven插件

上述lifecycle已经phase都是抽象的,具体的任务由插件来完成。每个插件包含可以完成的多个-goal。将goal绑定到phase完成每个lifecycle的任务。maven将生命周期和官方插件目标做了内置绑定。

可以自定义将插件目标绑定到phase。如spring-boot-maven-plugin:repackage绑定到package phase.

<packaging>类型会确定package阶段的goal.

maven依赖继承管理

利用maven从parent project中继承<parent>

从parent不只能继承依赖,大多数parent POM的element都会被继承包括插件。

子项目声明<parent>元素继承。注意子元素 Notice the relativePath element. It is not required, but may be used as a signifier to Maven to first search the path given for this project's parent, before searching the local and then remote repositories.

从父项目继承依赖的方式有两种。一种将依赖写在<dependencyManagement中(用来向子POM传递依赖),一种写在dependencies。前者需要在子类再次声明除了version。后者会被自动继承。

<scope>import</scope>

可以解决多继承的需求,只能用在dependencyManagement中,且只继承dependencyManagement。常把dependencyManagement放到单独的专门用来管理依赖的pom中 ,将<packaging>设为pom,--package phase时会执行目标site:attach-descriptor (removed from Maven 3.x)。

e.g.

<!--spring-boot-starter-parent POM 继承spring-boot-dependencies-->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>1.5.13.RELEASE</version>
    <relativePath>../../spring-boot-dependencies</relativePath>
</parent>
<!--spring-boot-dependencies POM实际管理dependencyManagement.因此可以在spring boot项目中使用import引入依赖 而让项目继承其他的父项目-->
<dependencyManagement>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>${spring.boot.version}</version>
        <type>pom</type>
        <scope>import</scope>
    </dependency>
</dependencyManagement>

unfinishing


tips

  • 在parent <packaging>pom</packaging>里执行install phase,子模块也会
  • mvn archetype:create -DgroupId=me.gacl -DartifactId=system-dao -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false,archetype为原型
  •  Consider a case in which your project uses two dependences, dep1 and dep2dep2 in turn also uses dep1, and requires a particular minimum version to function. If you then use dependencyManagement to specify an older version, dep2 will be forced to use the older version, and fail. So, you must be careful to check the entire dependency tree to avoid this problem; mvn dependency:tree is helpful.

Clone this wiki locally