`

Maven 生命周期与插件

阅读更多

Maven 生命周期与插件

 

一、生命周期

Maven有三套独立的生命周期,分别为clean,default和site。clean生命周期的目的是清理之前的构建,default的目的是构建项目,site的目的是建立项目站点。

 

每个生命周期都包含一些阶段(build phase),这些阶段是有顺序的,并且后面的阶段依赖于前面的阶段。三套生命周期是完全独立的,我们可以仅调用clean生命周期的某个阶段,或者仅调用default生命周期的某个阶段。

 

执行生命周期:

 

mvn [options] [<goal(s)>] [<phase(s)>]

 

clean生命周期

pre-clean execute processes needed prior to the actual project cleaning
clean remove all files generated by the previous build
post-clean execute processes needed to finalize the project cleaning

default生命周期

 

validate validate the project is correct and all necessary information is available.
initialize initialize build state, e.g. set properties or create directories.
generate-sources generate any source code for inclusion in compilation.
process-sources process the source code, for example to filter any values.
generate-resources generate resources for inclusion in the package.
process-resources copy and process the resources into the destination directory, ready for packaging.
compile compile the source code of the project.
process-classes post-process the generated files from compilation, for example to do bytecode enhancement on Java classes.
generate-test-sources generate any test source code for inclusion in compilation.
process-test-sources process the test source code, for example to filter any values.
generate-test-resources create resources for testing.
process-test-resources copy and process the resources into the test destination directory.
test-compile compile the test source code into the test destination directory
process-test-classes post-process the generated files from test compilation, for example to do bytecode enhancement on Java classes. For Maven 2.0.5 and above.
test run tests using a suitable unit testing framework. These tests should not require the code be packaged or deployed.
prepare-package perform any operations necessary to prepare a package before the actual packaging. This often results in an unpacked, processed version of the package. (Maven 2.1 and above)
package take the compiled code and package it in its distributable format, such as a JAR.
pre-integration-test perform actions required before integration tests are executed. This may involve things such as setting up the required environment.
integration-test process and deploy the package if necessary into an environment where integration tests can be run.
post-integration-test perform actions required after integration tests have been executed. This may including cleaning up the environment.
verify run any checks to verify the package is valid and meets quality criteria.
install install the package into the local repository, for use as a dependency in other projects locally.
deploy done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.

site生命周期

pre-site execute processes needed prior to the actual project site generation
site generate the project's site documentation
post-site execute processes needed to finalize the site generation, and to prepare for site deployment
site-deploy deploy the generated site documentation to the specified web server

二、插件与目标

插件:Maven的生命周期是抽象的,也就是说,执行一个生命周期实际上什么也没干,而实际的构建的活是由插件(Plugin)完成的,插件以独立的构件存在,Maven会在必要时下载这些构件以支持构建。

目标:对于插件本身,为了能够复用代码,它一般能够完成多项功能,例如maven-dependency-pugin插件,它能够基于项目依赖做很多的事情,比如分析项目依赖,列出项目的依赖树,列出项目已解析的依赖等等。为每个这样的功能都编写一个独立的插件是不合理的,为了代码的复用,Maven将这些功能聚集在一个插件里,每个功能就是一个插件目标(Plugin Goal)

 

Maven生命周期与插件相互绑定,更具体的说是与插件的目标绑定,以完成某个具体的构建任务。A build phase is made up of plugin goals

 

内置绑定

为了能让用户几乎不用任何配置就能进行构建,Maven为一些主要的生命周期阶段绑定了一些插件目标,当通过命令执行生命周期时,相应的插件目标就会执行。

 
clean生命周期的内置绑定
clean maven-clean-plugin:clean
default生命周期的内置绑定

default生命周期的绑定关系与具体的打包类型(pom中的packaging元素)相关

 

打包类型为 jar、ejb、ejb3、par、rar、war时,其绑定关系如下:

process-resources maven-resources-plugin:resources
compile maven-compiler-plugin:compile
process-test-resources maven-resources-plugin:testResources
test-compile maven-compiler-plugin:testCompile
test maven-surefire-plugin:test
package 以jar为例 maven-jar-plugin:jar
install maven-install-plugin:install
deploy maven-deploy-plugin:deploy

 打包类型为ear时,其绑定关系如下:

generate-resources maven-ear-plugin:generate-application-xml
process-resources maven-resources-plugin:resources
package maven-ear-plugin:ear
install maven-install-plugin:install
deploy maven-deploy-plugin:deploy

打包类型为maven-plugin 时,其绑定关系如下:

generate-resources maven-plugin-plugin:descriptor
process-resources maven-resources-plugin:resources
compile maven-compiler-plugin:compile
process-test-resources maven-resources-plugin:testResources
test-compile maven-compiler-plugin:testCompile
test maven-surefire-plugin:test
package maven-jar-plugin:jar and maven-plugin-plugin:addPluginArtifactMetadata
install maven-install-plugin:install
deploy maven-deploy-plugin:deploy

打包类型为pom时, 其绑定关系如下:

package maven-site-plugin:attach-descriptor
install maven-install-plugin:install
deploy maven-deploy-plugin:deploy

 

site生命周期的内置绑定
site maven-site-plugin:site
site-deploy maven-site-plugin:deploy

三、自定义绑定

大部分情况下内置绑定都不能完全满足我们的需求,因此我们需要通过配置pom.xml来手动绑定一些插件目标。

 

举个例子,我想把多个模块生成的jar包以及外部依赖都复制到一个lib目录下,那么maven-dependency-plugin的copy和copy-dependencies目标可以帮我们实现这个需求:

 

...
<build>
<plugin>
	<artifactId>maven-dependency-plugin</artifactId>
	<version>2.2</version>
	<configuration>
		<excludeTransitive>false</excludeTransitive>
		<stripVersion>false</stripVersion>
                <outputDirectory>${libPath}</outputDirectory>  <!-- 复制到哪个目录 -->
	</configuration>
	<executions>
		<execution>
		        <id>copy-dependencies</id>     
		        <phase>package</phase>    <!-- 绑定到default生命周期的package阶段 -->
		        <goals>
			        <goal>copy-dependencies</goal>      <!-- 指定目标:复制外部依赖 -->
		        </goals>
		        <configuration>
		                <includeScope>runtime</includeScope>
		                <overWriteIfNewer>true</overWriteIfNewer>
		        </configuration>
		</execution>
		<execution>
			<id>copy</id>    
			<phase>package</phase>     <!-- 在package阶段执行该任务 -->
			<goals>
				<goal>copy</goal>      <!-- 指定目标:复制模块生成的jar -->
			</goals>
			<configuration>
			        <artifactItems>
				        <artifactItem>       <!-- -->
					        <groupId>${project.groupId}</groupId>
					        <artifactId>${project.artifactId}</artifactId>
					        <version>${project.version}</version>
					        <overWrite>true</overWrite>
				        </artifactItem>
			        </artifactItems>
			</configuration>
		</execution>
	</executions>
</plugin>
</build>
...

 

更详细的插件信息 http://maven.apache.org/plugins/index.html

 

0
0
分享到:
评论

相关推荐

    Maven3实战笔记04Maven的生命周期和插件

    #### Maven生命周期与插件的关系 在Maven的生命周期中,每个阶段都可以绑定一个或多个插件的目标。当Maven执行到某个阶段时,会自动调用绑定在这个阶段上的插件目标来完成相应的任务。例如,在`compile`阶段,Maven...

    Maven的生命周期和插件

    通过以上对Maven生命周期和插件的介绍,我们可以看到,Maven通过标准化的流程和灵活的插件机制,为Java开发带来了极大的便利。深入理解并熟练掌握这些概念,能帮助我们更高效地管理和构建项目。

    Maven 生命周期.pdf

    Maven生命周期的每个阶段都有一个或者多个插件行为(插件目标)与之对应。这些插件目标是在实际执行时完成相应工作。例如,在clean生命周期中,clean阶段会调用maven-clean-plugin插件的clean目标来执行清理工作。...

    大话Maven生命周期

    Maven非常强大的重要原因是它有十分完善的生命周期模型 lifecycle 这个生命周期可以从两方面来理解 第一 顾名思义 运行Maven的每个步骤都由它来定义的 这种预定义的默认行为使得我们使用Maven变得简单 相比而言 Ant...

    maven definitive guide

    #### 五、Maven生命周期与插件 - **Maven生命周期**:Maven的构建过程分为多个阶段,每个阶段对应一系列操作,这些操作由不同的Maven插件完成。 - **Maven插件和目标**:Maven插件包含一系列目标,这些目标在特定的...

    maven2.0+eclipse插件

    3. **生命周期(Lifecycle)**:Maven有三个主要的生命周期阶段:clean、default和site。default生命周期包括编译、测试、打包、验证和部署等步骤。 4. **构建过程(Build Phases)**:每个生命周期阶段由多个构建...

    Maven介绍与安装和配置

    **Maven生命周期与插件** Maven的生命周期包括清理(clean)、默认(default)和站点(site)三个阶段。其中,清理阶段负责删除生成的中间文件;默认阶段包含了编译(compile)、测试(test)、打包(package)、...

    apache-maven-3.5.2-bin

    6. **Maven生命周期与插件**: - Maven生命周期包含一系列阶段,如compile、test、install等,插件绑定了这些阶段,负责执行具体任务。 - `maven(三):maven项目结构及其运行机制.pdf`和`maven(四):一个基本maven...

    尚硅谷Maven课程笔记代码资源

    三、Maven生命周期与插件 Maven的生命周期由多个阶段(如编译、测试、打包、部署等)组成,每个阶段对应一组特定的目标(goal)。开发者可以通过命令行指定执行某个阶段,或者让Maven按顺序执行整个生命周期。同时,...

    Maven 实战之背景案例.pdf

    6. Maven生命周期与插件的使用 通过案例,读者可以了解到Maven的构建生命周期(clean、validate、compile、test、package、install、deploy)和如何在构建过程中应用不同的插件,如编译插件、测试插件、打包插件等...

    maven从入门到精通系列教材

    3. **Maven生命周期与插件** Maven的生命周期分为clean、default和site三个阶段,每个阶段包含多个阶段(如compile、test)。开发者可以通过指定阶段的命令执行相应任务。同时,Maven的灵活性体现在插件机制,通过...

    第四课:Maven私服构建与插件开发1

    【描述】:本课程主要讲解Maven生命周期、自定义插件开发以及如何基于Nexus构建企业级的私有Maven仓库。 【标签】:“Maven”,“生命周期”,“插件开发”,“Nexus”,“私服” 【部分内容】: 1. Maven生命...

    Maven生命周期和及插件原理用法详解

    Maven生命周期和插件原理用法详解 Maven生命周期是Maven项目构建的核心机制,它定义了项目从编译到部署的整个过程。Maven生命周期可以分为三大阶段:clean周期、default周期和site周期。 1. Clean周期:负责清理...

    Maven视频教程

    ### Maven生命周期与插件 1. **生命周期详解**:详细介绍Maven的生命周期,包括clean、default和site三个主要阶段,以及每个阶段所包含的目标。 2. **常用插件介绍**:列举并解释Maven常用的插件,如maven-compiler...

    Eclipse Maven插件,SVN插件

    它支持POM(Project Object Model)配置,自动下载项目依赖,执行构建生命周期,以及与其他Eclipse特性如JUnit测试的集成。通过m2e,开发者可以充分利用Maven的中央仓库,简化依赖管理,提高开发效率。 SVN...

    maven安装与配置.zip

    ### Maven生命周期与插件 Maven的生命周期包括`clean`、`default`(也叫`compile`)、`site`三个阶段,每个阶段包含多个阶段。开发者可以自定义插件来扩展Maven的功能。例如,使用`maven-jar-plugin`来控制JAR文件...

    maven3.0.5安装包

    Maven生命周期与插件 Maven生命周期包括`clean`、`default`(或`compile`)、`site`等阶段,每个阶段由一系列的目标(goals)组成。插件是实现这些目标的组件,例如`maven-compiler-plugin`用于编译Java源代码。 ...

Global site tag (gtag.js) - Google Analytics