Maven 生命周期

article/2025/9/14 6:49:14

1. Maven 构建生命周期

Maven 构建生命周期就是 Maven 将一个整体任务划分为一个个的阶段,类似于流程图,按顺序依次执行。也可以指定该任务执行到中间的某个阶段结束。
Maven 的内部有三个构建生命周期,分别是 clean, default, site。其中 default 生命周期的核心阶段如下所示:

                                                                            

2. 如何使用构建生命周期来完成构建工作

  • 可以指定某个生命周期的阶段

执行 mvn install 命令,将完成 validate, compile, test, package, verify, install 阶段,并将 package 生成的包发布到本地仓库中。其中某些带有连字符的阶段不能通过 shell 命令单独指定。例如:(pre-, post-, or process-*)

mvn install
  • 可以指定多个不同构建生命周期的阶段

执行 mvn clean deploy 命令,首先完成的 clean lifecycle,将以前构建的文件清理,然后再执行 default lifecycle 的 validate, compile, test, package, verify, insstall, deploy 阶段,将 package 阶段创建的包发布到远程仓库中。

mvn clean deploy

3. 阶段与插件的关系

如上所述,Maven 将构建过程定义为 default lifecycle,并将 default lifecycle 划分为一个个的阶段 phase,这一系列 phase 仅仅是规定执行顺序,至于每个阶段做什么工作?由谁来做?答案就在 插件(plugins) 中。
Maven 对工程的所有操作实实在在的都是由 插件 来完成的。一个插件可以支持多种功能,称之为目标(goal),例如:compiler 插件有两个目标:compile 和 testCompile,分别实现编译源代码 和 编译测试代码。
如何将插件与 Maven 的构建生命周期绑定在一起呢?通过将插件的目标(goal)与 build lifecycle 中 phase 绑定到一起,这样,当要执行某个 phase 时,就调用插件来完成绑定的目标。
如下图所示:从图中可以看出,每一个阶段可以绑定0 个 或 多个目标,每个插件可以提供 1 个或多个目标。


4. 如何为自己的工程创建构建生命周期

  • 设置不同的 packaging 类型

在 pom.xml 文件中,packaging 类型支持 jar, war, ear, pom 等多种类型,不同的 packaging 类型会使得不同的 phase 绑定不同的 plugin goal。下面是 packaging 类型为 jar 时,phase 与 plugin goal 的映射关系。

阶段目标
process-resourcesresources:resources
compilecompiler:compile
process-test-resourcesresources:testResources
test-compilecompiler:testCompile
testsurefire:test
packagejar:jar
installinstall:install
deploydeploy:deploy
  • 配置 plugin

在 pom.xml 文件中, <build> <plugins> 元素下可以添加 <plugin>,通过指定 goal 和 phase 来进行绑定。
例如:将插件 modello-maven-plugin 的 java 目标绑定到 generate-sources 阶段。

<plugin><groupId>org.codehaus.modello</groupId><artifactId>modello-maven-plugin</artifactId><version>1.8.1</version><executions><execution><configuration><models><model>src/main/mdo/maven.mdo</model></models><version>4.0.0</version></configuration><phase>generate-sources</phase><goals><goal>java</goal></goals></execution></executions>
</plugin>

5. 我没有在 pom.xml 指定任何 plugin,但是也能正常构建工程

你可以能会疑问,默认的 pom.xml 文件并没有配置各种 plugin,但是也能正常构建工程?答案是 Maven 自己默认指定了 plugin。
下面是一个没有配置任何 plugin 的 pom.xml,执行 mvn install 的输出日志,从日志中可以看到 一系列的 插件(plugin):版本号:目标(phase),例如 maven-resources-plugin:2.6:resources (default-resources),maven-compiler-plugin:3.1:compile (default-compile) ,maven-resources-plugin:2.6:testResources (default-testResources),maven-compiler-plugin:3.1:testCompile (default-testCompile),maven-surefire-plugin:2.12.4:test (default-test),maven-jar-plugin:2.4:jar (default-jar) ,maven-install-plugin:2.4:install (default-install),

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building my-app 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ my-app ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ my-app ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/zhangguanghui/git/my-app/target/classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ my-app ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/zhangguanghui/git/my-app/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ my-app ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/zhangguanghui/git/my-app/target/test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ my-app ---
[INFO] Surefire report directory: /Users/zhangguanghui/git/my-app/target/surefire-reports-------------------------------------------------------T E S T S
-------------------------------------------------------
Running com.mycompany.app.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.004 secResults :Tests run: 1, Failures: 0, Errors: 0, Skipped: 0[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ my-app ---
[INFO] Building jar: /Users/zhangguanghui/git/my-app/target/my-app-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ my-app ---
[INFO] Installing /Users/zhangguanghui/git/my-app/target/my-app-1.0-SNAPSHOT.jar to /Users/zhangguanghui/.m2/repository/com/mycompany/app/my-app/1.0-SNAPSHOT/my-app-1.0-SNAPSHOT.jar
[INFO] Installing /Users/zhangguanghui/git/my-app/pom.xml to /Users/zhangguanghui/.m2/repository/com/mycompany/app/my-app/1.0-SNAPSHOT/my-app-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.726 s
[INFO] Finished at: 2016-11-20T00:41:11+08:00
[INFO] Final Memory: 15M/310M
[INFO] ------------------------------------------------------------------------

5. 完整的 clean, default, site build lifecycle

  • clean lifecycle
phasefunction
pre-clean executeexecute 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
phasefunction
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. For Maven 2.0.5 and above.
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. (Maven 2.1 and above)
packagetake the compiled code and package it in its distributable format, such as a JAR.
pre-integration-test performactions 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
phasefunction
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

6. 参考文档

参考

maven 入门指南
maven 生命周期
Maven 默认插件以及功能
maven 依赖管理
maven-shade-plugin 入门指南
maven-assembly-plugin 入门指南
Introduction to the Build Lifecycle



作者:data4
链接:https://www.jianshu.com/p/fd43b3d0fdb0
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


http://chatgpt.dhexx.cn/article/5LAaZPE8.shtml

相关文章

Maven生命周期

Maven生命周期 个人网站 https://blog.deschen.cn/ 文章目录 Maven生命周期一、Maven生命周期的定义二、Maven三套独立的生命周期三、Maven的插件 一、Maven生命周期的定义 Maven的生命周期就是为了对所有的构建过程进行抽象和统一。包括项目的清理&#xff0c;初始化&#x…

Maven —— 生命周期

每个生命周期的各个环节都是由各种插件完成&#xff01;&#xff01;&#xff01;Maven有三个相互独立的生命周期&#xff08;Maven的这三个生命周期不能看成一个整体&#xff09;&#xff01;&#xff01;&#xff01; mvn clean&#xff1a;清理编译的项目mvn compile&#x…

代码统计利器--CLOC

MAC下安装命令:$ brew install cloc其他的linux安装 $ aptitude install cloc使用方法.到目录下运行: $ cloc .The default output will show you a breakdown by language. Here’s an example of what it’ll look like: php much? You can, of course, customize the poop …

代码统计工具cloc使用

简介 CLOC(Count Lines of Code)&#xff0c;是一个可以统计多种编程语言中空行、评论行和物理行的工具。这个工具还是蛮实用的&#xff0c;可以帮我们快速了解一个项目中代码的信息。 安装使用 windows 10 win10下可以去github上下载其最新版&#xff0c;截止本文时&#…

windows代码量计算开源工具cloc安装和使用教程

windows代码量计算开源工具cloc 下载cloc使用cloc 下载cloc cloc下载地址: https://github.com/AlDanial/cloc/releases. 选择exe版本的&#xff0c;也可以下载我上传的1.90版本链接: 点击跳转下载地址 使用cloc 下载好cloc-1.90.exe之后&#xff0c;重命名未cloc.exe&…

Linux统计代码量命令cloc

记录一下Linux中一个非常好用的代码量统计命令&#xff1a;cloc 安装步骤&#xff1a; sudo apt-get install cloc使用方法&#xff1a; 进入到要统计的工程根目录&#xff1a; cloc .运行结果:

Windows环境下用cloc统计代码量

cloc一款开源代码统计工具&#xff0c;支持windows和Linux环境。能统计指定文件夹或文件夹中文件数files、空白行数blank、注释行数comment和代码行数code。今天介绍windows环境下的使用方法。 使用简单&#xff1a; 下载&#xff1a; Releases AlDanial/cloc (github.com)…

代码统计工具CLOC的使用

简介 CLOC(Count Lines of Code)&#xff0c;是一个可以统计多种编程语言中空行、评论行和物理行的工具。这个工具还是蛮实用的&#xff0c;可以帮我们快速了解一个项目中代码的信息。 安装使用 windows 10 win10下可以去github上下载其最新版&#xff0c;截止本文时&#…

cloc 代码统计工具

安装 yum -y install cloc使用 [rootnode1 new-website]# cloc .135 text files.134 unique files.20 files ignored.github.com/AlDanial/cloc v 1.70 T3.63 s (32.0 files/s, 26416.0 lines/s) ------------------------------------------------------------------------…

10分钟掌握高效代码行统计工具——cloc

cloc 一款高效的代码行统计工具&#xff0c;且跨多平台&#xff1a; WindowLinuxMac… … 高效是其优点&#xff0c;且稳定性比较好。 Linux版的可以处理超大工程的文件&#xff0c;不会出现其它同类工具在处理超大文件时崩溃的问题。 用法简单&#xff0c;学习成本低&…

前端代码统计工具cloc的安装与使用

怎么来衡量一个web端项目的大小&#xff0c;一是看页面多少&#xff0c;二是看源代码行数。页面多少比较好统计&#xff0c;通过 Router 的配置大概就能知道。但是源代码行数&#xff0c;如果要一个文件一个文件去计算&#xff0c;那就费了劲了。有问题有需求&#xff0c;就会有…

“无法启动mysql服务,错误1053”解决办法

启动MySQL服务时&#xff0c;报错如下&#xff1a; 1.“CTRLR”打开运行窗口&#xff0c;输入regedit点击确定打开注册表编辑器 2.找到HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\MySQL目录 3.修改ImagePath路径为mysqlld.exe路径&#xff0c;重启服务即可

解决Windows服务1053错误方法

WCF使用MSMQ绑定寄宿在Windows服务上&#xff0c;但启动服务时出现1053错误 在网上搜索了N多解决方案&#xff0c;都是比较高深的扯到原理和系统bug等问题 看了看到最后也没有解决&#xff0c;最终我决定使用一个比较山寨的但很有用的方式查看问题所在&#xff0c;所以就在代码…

windows无法启动MySQL服务出现1053错误原因之一

今天去使用mysql的时候出现了这样一个问题 我以为是mysql服务没开&#xff0c;去开启服务的时候出现 错误原因 服务的路径与安装的路径不一致&#xff0c;之前安装过mysql后卸载重装容易出现这样的问题&#xff0c;要么就是你可能从计算机上面卸载什么&#xff0c;组件程序&a…

无法启动OracleDBConsoleorcl服务,错误:1053 的解决办法 .

在控制面板里启动oracle的服务&#xff0c;其中启动OracleDBConsoleorcl服务时出错&#xff0c;提示&#xff1a;在本地计算机无法启动OracleDBConsoleorcl服务。错误&#xff1a;1053&#xff1a;服务没有及时相应启动或控制请求。重启电脑之后依然如故。请问怎么解决呢&#…

系统错误1058的解决办法

今天打算写个关闭windows系统服务的程序&#xff0c;发现了个错误 错误描述&#xff1a;无法启动Themes服务 错误详细步骤&#xff1a; 1.我以前禁用了服务themes 2.现在手动修改注册表HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/Themes中的"Start"为3…

Win11安装mysql 服务无法启动 1053错误之解决方法

手动启动的时候报错1053. 网上查询解决方法 MySQL启动1053错误之解决方法_小智解说的博客-CSDN博客_mysql1053 方法一&#xff1a; 重装mysql&#xff0c;重装后发现还是启动失败 方法二 为提高网络服务权限&#xff0c;需将网络服务添加到管理员组 计算机->管理->本地…

计算机Auto服务错误1053,服务启动1053错误解决步骤

我们在使用电脑是由于各种原因&#xff0c;需要对一些服务进行启动&#xff0c;可是有些用户却遇到了一些问题&#xff0c;比如有用户在启动某服务的时候&#xff0c;却发现启动不了&#xff0c;提示服务启动1053错误&#xff0c;那么遇到这样的问题该如何处理呢&#xff1f;下…

sqlserver2000代理无法启动 报错 1053,已解决。

背景&#xff1a; win7 64位系统&#xff0c;上安装着sqlserver2000&#xff0c;打补丁失败后&#xff0c;代理启动失败。 出现2个问题&#xff1a; 1、 点击代理&#xff0c;报错&#xff0c;提示 “发生错误 缺少xpstar.dll” 2、下载 xpstar.dll,下载,简介,描述,修复,等…

Windows无法启动MongoDB Server服务(错误1053)

Windows无法启动MongoDB Server服务&#xff08;错误1053&#xff09; 问题描述问题解决尚存在疑问 问题描述 作为一个代码界的小白&#xff0c;今天在准备使用MongoDB的过程中&#xff0c;出现了代码为1053的错误。为了避免下次再遇到该错误时忘记如何处理&#xff0c;在这里…