您的位置:首页 > 其它

maven 生命周期与插件、聚合与继承

2016-07-28 17:54 387 查看

一、maven生命周期

1、生命周期简介

maven具有三套相对独立的生命周期,clean、default、sit。
clean的生命周期包括pre-clean,clean,post-clean三个阶段。
default的生周期包括:

sit不过多介绍

2、命令行与生命周期

从命令行执行mvn最主要就是调用maven的生命周期阶段。各个生命周期是独立的,一个生命周期阶段是有前后依赖关系的。
mvn clean:调用clean生命周期clean阶段,从pre-clean到clean。
mvn test:调用default生命周期的test阶段,从validate到test。
mvn clean install:调用clean生命周期的clean阶段和default的install阶段。也就是从pre-clean到clean,从validate到install。

二、maven插件

1、插件与生命周期绑定关系

maven的插件目标与生命周期的阶段相互绑定。绑定关系如下图(不全):

4000

生命周期阶段
插件目标
执行任务
compilemaven-compiler-plugin:compile编译代码至输出目录
deploymaven-deploy-plugin:deploy将项目输出到远程仓库
installmaven-install-plugin:install将项目输出到本地仓库
packagemaven-jar-plugin:jar打包
process-sources

maven-resource-plugin:resources复制主资源任务至输出目录
testmaven-surefire-plugin:test执行测试代码

2、自定义绑定

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>


executions下每个execution用来配置一个任务,该任务默认配置到verify阶段,也可以在execution下配置phase元素指定生命周期阶段。

mvn help:describe -Dplugin=compiler 可获取插件的详细信息,包括插件的目标等。

三、聚合与继承

聚合各个模块的公共配置,然后各个模块再继承这个公共配置,能消除很大的重复性管理也统一到了同一地方。

父pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>

<groupId>xxx</groupId>
<artifactId>xxx</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<spring.version>4.0.7.RELEASE</spring.version>
</properties>
<!-- 非每个模块都必须 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>test</groupId>
<artifactId><span style="font-family: SimSun;">test</span></artifactId>
<version>1.0.6-SNAPSHOT</version>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 每个模块都必须 -->
<dependencies>
<!--spring相关依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${java-version}</source>
<target>${java-version}</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>install</id>
<phase>install</phase>
<goals>
<goal>sources</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.8.1</version>
<configuration>
<skipTests>true</skipTests>
<junitArtifactName>junit:junit</junitArtifactName>
<excludes>
<exclude>**/*_Roo_*</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.5</version>
<executions>
</executions>
</plugin>
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>3.0.0</version> <executions> <execution> <id>attach-sources</id> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin>
</plugins>
</build>
<distributionManagement>
<snapshotRepository>
<id>aaa</id>
<name>bbb</name>
<url>ccc</url>
</snapshotRepository>
</distributionManagement>

<modules>
<module>framework</module>
<module>service</module>
</modules>
</project>

configuration--特定插件或任务的配置
下面解释下父pom中特殊的节点:

modelVersion--聚合pom必须指定该节点

packaging--必须为pom

dependencyManagement--在父pom中管理依赖配置

modules--指定聚合的模块名

子pom

<?xml version="1.0"  encoding="UTF-8" ?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>xxx</groupId>
<artifactId>xxx</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>service</artifactId>
<name>service</name>

<dependencies>
<dependency>
<groupId>test</groupId>
<artifactId>test</artifactId>
</dependency>
</dependencies>
</project>




parent--指定父pom的坐标

artifactId--和父pom的modules中指定一致。
子pom继承了父pom的dependency与plugin,properties等配置,但要引用dependencyManagement中的依赖需要指定groupId和artifactId而无须指定其他配置。根据groupId和artifactId就可以确定父pom中的该依赖,这样做的好吃是只需要在一处管理依赖配置。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息