一、maven生命周期
1、生命周期简介
validate
initialize
generate-sources
process-sources
处理资源文件
compile
编译
test
基本重复上面步骤...
test
测试
prepare-
package
package
打包
...
verify
install
发布到本地maven仓库
deploy
发布到远程maven仓库
2、命令行与生命周期
二、maven插件
1、插件与生命周期绑定关系
maven的插件目标与生命周期的阶段相互绑定。绑定关系如下图(不全):
生命周期阶段
|
插件目标
|
执行任务
|
---|---|---|
compile | maven-compiler-plugin:compile | 编译代码至输出目录 |
deploy | maven-deploy-plugin:deploy | 将项目输出到远程仓库 |
install | maven-install-plugin:install | 将项目输出到本地仓库 |
package | maven-jar-plugin:jar | 打包 |
process-sources | maven-resource-plugin:resources | 复制主资源任务至输出目录 |
test | maven-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>
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中的该依赖,这样做的好吃是只需要在一处管理依赖配置。