maven 的 Build lifecycle

本文详细介绍了Maven构建过程中的三个核心生命周期:default、clean和site。解析了各个阶段的任务及其绑定的目标,阐述了如何通过配置<packaging>元素来定制打包阶段,并探讨了插件及其目标如何与构建阶段绑定。

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

build 生命周期定义了building,testing,distributing等,是maven project的核心

3个内置的lifecycle  : default,clean,site

 

default lifecycle

   包含20 build phases,

   validate : 检查所有的project information是可用的并且是正确的

   initialize  初始化状态,如设置properties或者创建目录

   generate-sources

   process-sources

   generate-resources

   process-resources

   compile  :编译

   process-classes

  generate-test-sources

  process-test-sources

  generate-test-resources

  process-test-resources

  test-compile

  process-test-classes

   test     : 运行unit tests

   prepare-package

   package  : distribution format打包编译好的代码

   pre-integration-test

   Integration-test : 集成测试

   post-integration-test

   verify  :  检查集成测试的结果

   install : package 安装到local repository

   deploy  : package 安装到remote repository

 

当你执行一个build phase,所有以前的phases都会顺序执行

mvn install

mvn clean deploy

clean lifecycle

 负责项目清理

  pre-clean  执行clean前要求的processes

  clean       删除以前builds 产生的所有文件

  post-clean  执行完成clean后要求的processes

 

site lifecycle

  负责产生和部署project site documentation

 

pre-site: site动作前需要执行的processes

site:  产生项目的site documentation

Post-site : site后要执行的processes并准备布署

Site-deploy : 布署文档到指定的web server

 

plugin goals

  每个build phase 可以通过绑定不同的plugin goals 来完成不同的行为

一个plugin goal代表一个特定的task,它的范围比build phase要小; 一个plugin goal可以梆定到0个或者多个build phases. 没有绑定到任何build phasegoal可以通过直接调用在build lifecycle外执行;执行的顺序依赖于goalsbuild phases 调用的顺序。

 

mvn clean dependency:copy-dependencies package

如果一个goal 绑定到一个或多个build phases,在所有的这些phases执行时,这个goal都会被调用

一个build phase可以绑定0个或多个goals

maven 2.0.5及以后, 绑定到一个phase中的多个goals按照它们在pom中声明的顺序执行,但是

同一个pluginmultiple instances 是不支持的,maven 2.0.11及以后同一个pluginmultiple

instances 成为一组并按顺序一起执行

 

通常并不从命令行调用某些phases,因为它们之前或之后可能绑定了一些goal,pre-*,post-*或者process-*

 

使用

Packaging

   通过pom中的<packaging> 元素来定义package phase,合法的值包括jar,war,ear,pom,默认是jar

   每个packaging 包含了一些绑定到特定phasegoals,例如jar packaging 会绑定下面的goals到默认的

lifecyclebuild phases

 

process-resources           resources:resources

compile                       compiler:compile

process-test-resources   resources:testResources

test-compile               compiler:testCompile

test                       surefire:test

package                        jar:jar

install                        install:install

deploy                        deploy:deploy

 

 

packaging 的值是pom时,只给install deploy 绑定goals,

为了使某些packing types 可用,可能也需要在<build>节点指明

<extensions>true</extensions> for that plugin

 

 

Plugins

plugins 是给maven提供goalsartifacts.  每个plugin 可以有一个或多个goals.

如果每个phase 绑定了多个goals,来自于packaging 的先执行,在pom中配置的后执行。可以使用

<executions> 节点来获得更多的控制权

 

放在<build>节点的<plugins>节点的<plugin>节点里面

 

Built-in Lifecycle Bindings

一些phase 默认绑定了一些goals,对于default lifecycle,这些绑定依赖于packaging value.

 

clean                       clean:clean

 

Default Lifecycle Bindings - Packaging ejb / ejb3 / jar / par / rar / war

 

process-resources               resources:resources

compile                         compiler:compile

process-test-resources         resources:testResources

test-compile                     compiler:testCompile

test                             surefire:test

package          ejb:ejb or ejb3:ejb3 or jar:jar or par:par or rar:rar or war:war

install              install:install

deploy                   deploy:deploy

 

 

Default Lifecycle Bindings - Packaging ear

 

generate-resources          ear:generate-application-xml

process-resources          resources:resources

package                     ear:ear

install                     install:install

deploy                         deploy:deploy

 

 

Default Lifecycle Bindings - Packaging maven-plugin

 

generate-resources             plugin:descriptor

process-resources               resources:resources

compile                         compiler:compile

process-test-resources         resources:testResources

test-compile                      compiler:testCompile

test                              surefire:test

package                       jar:jar and plugin:addPluginArtifactMetadata

install                         install:install

deploy                              deploy:deploy

 

Default Lifecycle Bindings - Packaging pom

 

package              site:attach-descriptor

install              install:install

deploy                  deploy:deploy

 

 

Site Lifecycle Bindings

site                 site:site

site-deploy          site:deploy

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值