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

本文介绍了Maven的clean、default、sit生命周期及其各阶段,解释了如何通过命令行调用这些生命周期。同时,深入探讨了Maven插件与生命周期的绑定关系及自定义绑定方法。此外,还讲解了如何利用聚合与继承来简化多模块项目的配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、maven生命周期

1、生命周期简介

maven具有三套相对独立的生命周期,clean、default、sit。
clean的生命周期包括pre-clean,clean,post-clean三个阶段。
default的生周期包括:
validate
initialize
generate-sources
process-sources 处理资源文件
compile 编译
test 基本重复上面步骤...
test 测试
prepare-package
package 打包
...
verify
install 发布到本地maven仓库
deploy 发布到远程maven仓库

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的插件目标与生命周期的阶段相互绑定。绑定关系如下图(不全):


生命周期阶段
插件目标
执行任务
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>
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中的该依赖,这样做的好吃是只需要在一处管理依赖配置。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值