IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 网络协议 -> Maven生命周期详解 -> 正文阅读

[网络协议]Maven生命周期详解

文中加粗显示的英文是在官方文档中出现的术语,我使用的相应术语的中文翻译,不能保证十分准确,严谨起见,在使用中文直译的术语后,使用了对应的英文属于。由于博主水平有限,非常欢迎大家能够批评指正。

一 构建声明周期基础知识

我们在日常中,经常提及Maven(美式发音/?me?vn/ )中的一个专业术语:声明周期 Lifecycle,然而,它在官方文档中的名称却是:Build Lifecycle (直译为,构建生命周期)。”Build"一词,我个人更倾向于把它理解为操作或者处理,声明周期即是一个过程,就像人会生、老、病、死的一个明确被定义的过程。之所以这样理解Build,是为了和默认声明周期过程中的构建区别开来,这里的构建是把一个项目从无到有创建出来。构建声明周期就是处理项目的过程。为了方便描述和避免歧义,下文中的术语都是使用声明周期。

声明周期是Maven的核心概念,它清楚地定义了一个项目被构建(创建)和发布的过程。

Maven中内置了三套生命周期(依据不同的目的,定义了三种处理项目的过程):

  • default lifecycle 负责项目的构建、发布
  • clean lifecycle 负责清理项目
  • site lifecycle 负责生成项目站点,即API文档信息网站

二 生命周期是由阶段组成的

Each of these build lifecycles is defined by a different list of build
phases, wherein a build phase represents a stage in the lifecycle.

每个声明周期由若干不同的阶段(Phase( 美 /fe?z/ )组成。阶段代表了生命周期的特殊步骤。

例如,default lifecycle 由下列阶段组成(这里列出了部分阶段,完整阶段下文会介绍)

  • validate 验证项目是正确的,所有必要的信息都是可用的
  • compile 编译项目源代码
  • test 使用单元测试框架测试编译后的源代码
  • package 获取已编译的代码,并将其打包为可发行的格式,例如JAR。
  • verify获取已编译的代码,并将其打包为可发行的格式,例如JAR。
  • install将包安装到本地仓库,供本地项目使用
  • 将包发布到远程仓库(remote repository),方便其他开发人员和项目共享。

These lifecycle phases are executed sequentially.

生命周期阶段是按(在生命周期中定义的)顺序执行的。例如,在执行package阶段时,会先按顺序执行validate、compile、test阶段,最后执行package阶段。注意,上文提及的只是部分主要阶段,compile和test阶段之间存在着其他阶段,也是按照其在default lifecycle中定义的顺序执行的。

三 阶段是由插件目标组成的

However, even though a build phase is responsible for a specific step in the build lifecycle, the manner in which it carries out those responsibilities may vary. And this is done by declaring the plugin goals bound to those build phases.

阶段对生命周期的特殊步骤负责,它履行职责的方式可能不同,取决于绑定到阶段的插件目标。简而言之,阶段的执行依赖于插件目标。(plugin goal)

例如:compile阶段履行编译职责,依赖于插件目标 compiler:compile,这里compiler代表了插件maven-compiler-plugin (本质是maven-compiler-plugin-3.1.jar),compile表示(插件)目标(本质上应该是要选择类中的一个方法执行,具体还有看源码,主要就是选择一个模块执行)。

A plugin goal represents a specific task (finer than a build phase) which contributes to the building and managing of a project. It may be bound to zero or more build phases. A goal not bound to any build phase could be executed outside of the build lifecycle by direct invocation.

Moreover, if a goal is bound to one or more build phases, that goal will be called in all those phases.

Furthermore, a build phase can also have zero or more goals bound to it. If a build phase has no goals bound to it, that build phase will not execute. But if it has one or more goals bound to it, it will execute all those goals.

内容较多,挑选主要内容翻译。

1. 插件目标代表明确的任务。

例如:jar:jar把编译的二进制class文件打包,格式JAR;install:install把打包文件安装到本地仓库等等。

2. 一个生命周期阶段可以绑定零或多个插件目标。如果,某生命周期阶段没有绑定插件目标(或者说绑定零个插件目标),它将不会执行。绑定多个按绑定的先后顺序执行。(后文会详细介绍阶段如何绑定目标,及执行顺序)

例如: 打包方式(packaging)为pom时,生命周期阶段package没有绑定任何插件目标,因此不会执行。打包方式为pom时主要用于父项目,统一管理依赖。不进行打包,也是可以理解的。(不熟悉,没关系,后文会详细介绍打包方式)

打包方式为maven-plugin时,生命周期阶段package绑定了多个(实际为2个)插件目标jar:jar plugin:addPluginArtifactMetadata

3. 一个插件目标可以绑定到零个或者多个生命周期阶段上。如果某插件目标没有绑定到生命周期阶段上(即绑定到零个生命周期阶段),它执行任务的方式时直接调用。

例如:插件目标install:install,它被绑定到了install阶段,在命令行中输入mvn install,首先执行default lifecycle 中install之前的所有生命周期阶段,最后执行install阶段,此时,插件目标install:install执行任务。假设,还有其他插件目标绑定到install阶段,也会被调用来执行相应的任务。

有些插件目标,如,tomcat7:run没有绑定到任务生命周期阶段,它想要执行任务,可以通过直接调用(direct invocation),在命令行中输入mvn tomcat7:run,就可以执行任务。

另外,在命令行输入install:install,只会执行安装包到本地仓库的任务。如果,install阶段之前的所有生命周期阶段没有执行,此时会报错,因为没有install之前的阶段履行职责,可能根本不存在jar包。

简言之,插件目标执行任务,可以直接调用。也可以由阶段调用。

四 生命周期及履行职责

下面列出了default、clean和site生命周期的所有阶段,它们按照在下表中定义的顺序执行的。当执行某个阶段,先执行其之前的所有阶段,然后执行该阶段。用连字符命名的阶段(pre-*、post-或process-)通常不会直接从命令行调用。它们履行的职责主要是生成一些中间产物等等

Clean Lifecycle

PhaseDescription
pre-cleanexecute processes needed prior to the actual project cleaning
cleanremove all files generated by the previous build
post-cleanexecute processes needed to finalize the project cleaning

Default Lifecycle

PhaseDescription
validatevalidate the project is correct and all necessary information is available.
initializeinitialize build state, e.g. set properties or create directories.
generate-sourcesgenerate any source code for inclusion in compilation.
process-sourcesprocess the source code, for example to filter any values.
generate-resourcesgenerate resources for inclusion in the package.
process-resourcescopy and process the resources into the destination directory, ready for packaging.
compilecompile the source code of the project.
process-classespost-process the generated files from compilation, for example to do bytecode enhancement on Java classes.
generate-test-sourcesgenerate any test source code for inclusion in compilation.
process-test-sourcesprocess the test source code, for example to filter any values.
generate-test-resourcescreate resources for testing.
process-test-resourcescopy and process the resources into the test destination directory.
test-compilecompile the test source code into the test destination directory
process-test-classespost-process the generated files from test compilation, for example to do bytecode enhancement on Java classes.
testrun tests using a suitable unit testing framework. These tests should not require the code be packaged or deployed.
prepare-packageperform any operations necessary to prepare a package before the actual packaging. This often results in an unpacked, processed version of the package.
packagetake the compiled code and package it in its distributable format, such as a JAR.
pre-integration-testperform actions required before integration tests are executed. This may involve things such as setting up the required environment.
integration-testprocess and deploy the package if necessary into an environment where integration tests can be run.
post-integration-testperform actions required after integration tests have been executed. This may including cleaning up the environment.
verifyrun any checks to verify the package is valid and meets quality criteria.
installinstall the package into the local repository, for use as a dependency in other projects locally.
deploydone in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.

Site Lifecycle

PhaseDescription
pre-siteexecute processes needed prior to the actual project site generation
sitegenerate the project's site documentation
post-siteexecute processes needed to finalize the site generation, and to prepare for site deployment
site-deploydeploy the generated site documentation to the specified web server

四 如何为生命周期阶段赋予或绑定插件目标

主要有两种方式,第一种方式,使用Maven内置的绑定。Maven根据不同的打包方式,为某些阶段默认绑定插件目标。由于其打包生成的目标包不同,因此阶段和阶段绑定的插件目标也存在差异。第二种方式,通过配置pom.xml文件,为阶段绑定插件目标。如此,阶段可能包含多个插件目标,其执行顺序为,先执行maven内置的为阶段绑定的目标(如果该阶段绑定了插件目标的话),然后按照你在pom文件中配置的插件目标的顺序依次执行(如果你为该阶段配置了多个插件目标的话)

Packaging

通过POM元素<packaging>设置打包方式,有效值有:jarwarearpom。如果没有设置该值,默认值为jar

每种打包方式包含的阶段以及阶段绑定的插件目标存在些许差异。

默认声明周期-打包方式 ejb / ejb3 / jar / par / rar / war

Phaseplugin:goal
process-resourcesresources:resources
compilecompiler:compile
process-test-resourcesresources:testResources
test-compilecompiler:testCompile
testsurefire:test
packageejb:ejb or ejb3:ejb3 or jar:jar or par:par or rar:rar or war:war
installinstall:install
deploydeploy:deploy

Default Lifecycle Bindings - Packaging ear

Phaseplugin:goal
generate-resourcesear:generate-application-xml
process-resourcesresources:resources
packageear:ear
installinstall:install
deploydeploy:deploy

Default Lifecycle Bindings - Packaging maven-plugin

Phaseplugin:goal
generate-resourcesplugin:descriptor
process-resourcesresources:resources
compilecompiler:compile
process-test-resourcesresources:testResources
test-compilecompiler:testCompile
testsurefire:test
packagejar:jar and plugin:addPluginArtifactMetadata
installinstall:install
deploydeploy:deploy

Default Lifecycle Bindings - Packaging pom

Phaseplugin:goal
package
installinstall:install
deploydeploy:deploy

Clean Lifecycle Bindings

Phaseplugin:goal
cleanclean:clean

Site Lifecycle Bindings

Phaseplugin:goal
sitesite:site
site-deploysite:deploy
  网络协议 最新文章
使用Easyswoole 搭建简单的Websoket服务
常见的数据通信方式有哪些?
Openssl 1024bit RSA算法---公私钥获取和处
HTTPS协议的密钥交换流程
《小白WEB安全入门》03. 漏洞篇
HttpRunner4.x 安装与使用
2021-07-04
手写RPC学习笔记
K8S高可用版本部署
mySQL计算IP地址范围
上一篇文章      下一篇文章      查看所有文章
加:2022-03-08 22:56:03  更:2022-03-08 22:57:25 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年10日历 -2024/10/5 13:23:10-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码