【03 Maven生命周期和插件】
九月九日忆山东兄弟何为生命周期生命周期详解clean生命周期deault生命周期site生命周期命令行与生命周期插件内置插件自定义插件绑定插件配置插件解析笔记王维独在异乡为异客每逢佳节倍思亲。遥知兄弟登高处遍插茱萸少一人。除了坐标、依赖以及仓库之外Maven另外两个核心概念是生命周期和插件。何为生命周期在Maven出现之前项目构建的生命周期就已经存在软件开发人员每天都在对项目进行清理、编译、测试及部署。Maven的生命周期就是为了对所有的构建过程进行抽象和统一。Maven从大量项目和构建工具中学习和反思然后总结了一套高度完善的、易扩展的生命周期。这个生命周期包含了项目的清理、初始化、编译、测试、打包、集成测试、验证、部署和站点生成等几乎所有构建步骤。也就是说几乎所有项目的构建都能映射到这样一个生命周期上。Maven的生命周期是抽象的这意味着生命周期本身不做任何实际的工作在Maven设计中实际的任务(如编译源代码)都交由插件来完成。这种思想与设计模式中的模板方法Template Method非常相似。生命周期抽象了构建的各个步骤定义了它们的次序但没有提供具体实现。那么谁来实现这些步骤呢每个构建步骤都可以绑定一个或者多个插件行为而且Maven为大多数构建步骤编写并绑定了默认插件。例如针对编译的插件有maven-compiler-plugin针对测试的插件有maven-surefire-plugin等虽然在大多数时间里用户几乎都不会察觉到插件的存在但实际上编译是由maven-compiler-plugin完成的而测试是由maven-surefire-plugin完成的。当用户有特殊需要的时候也可以配置插件定制构建行为甚至自己编写插件。生命周期和插件的关系Maven定义的生命周期和插件机制一方面保证了所有Maven项目有一致的构建标准另一方面又通过默认插件简化和稳定了实际项目的构建。此外该机制还提供了足够的扩展空间用户可以通过配置现有插件或者自行编写插件来自定义构建行为。生命周期详解初学者往往会以为Maven的生命周期是一个整体其实不然Maven拥有三套相互独立的生命周期即彼此没有依赖关系执行一个生命周期中的阶段不会自动触发其他生命周期中的任何阶段。他们分别为clean、default和site。生命周期说明clean清理项目删除target/目录等default也叫build lifecycle构建项目编译、测试、打包、部署(发布构件到仓库)等Introduction to the Build Lifecyclesite生成项目站点文档如JavaDoc、测试报告等clean生命周期clean生命周期的目的是清理项目它包含三个阶段阶段插件目标说明pre-clean没有默认的插件目标cleanmaven-clean-plugin:clean清理上一次构建生成的文件(/target)post-clean没有默认的插件目标在idea中点击cleancmd中输出mvnw.cmd ^-Dmaven.ext.class.pathC:\Users\Administrator\AppData\Roaming\JetBrains\IntelliJIdea2025.3\plugins\maven\lib\maven-event-listener.jar^-Djansi.passthroughtrue ^-Dstyle.coloralways ^-Dmaven.repo.localE:\repository ^ clean ^-fpom.xml项目解释mvnw.cmdmaven wrapper封装了maven-Didea.version2025.3.4这是IDEA自己传进去的一个标记表示“这次Maven构建是由 IDEA 2025.3.4 发起的”-Dmaven.ext.class.pathApache Maven官方文档说明maven.ext.class.path 可以把一个 JAR 作为 Maven Core Extension 加到Maven的核心类加载路径里。IDEA把自己的一个 Maven 扩展 JAR 注入到了Maven启动过程中。这个 JAR 一般用来做这些事1 监听Maven构建事件2 把日志、阶段、错误、下载进度回传给IDEA3 让IDEA的Maven工具窗口能实时显示构建状态-Djansi.passthroughtrue和控制台彩色输出有关的参数避免ANSI颜色转义在某些控制台环境下被错误处理。-Dstyle.coloralways始终输出彩色样式-Dmaven.repo.localE:\repository指定本地仓库目录-f pom.xml-f是 Maven 的--file选项表示明确指定要使用哪个POM文件。这里就是让Maven按当前目录下的pom.xml来构建。deault生命周期default生命周期定义了真正构建时所需要执行的所有步骤它是所有生命周期中最核心的部分default生命周期包含23个阶段核心的主要有7个# 查看生命周期默认绑定的插件目标下面是在查看validate阶段mvn help:describe-Dcmdvalidate-Ddetail阶段插件目标说明validate没有默认的插件目标initialize没有默认的插件目标generate-sources没有默认的插件目标process-sourcesresources:resources处理项目主资源文件。一般来说是对src/main/resources目录的内容进行变量替换等工作后复制到项目输出的主classpath目录中。generate-resources没有默认的插件目标process-resources没有默认的插件目标compilemaven-compiler-plugin:compilecompile the source code of the projectprocess-classes没有默认的插件目标generate-test-sources没有默认的插件目标process-test-sourcesresources:testResourcesgenerate-test-resources没有默认的插件目标process-test-resources没有默认的插件目标test-compilecompiler:testCompileprocess-test-classes没有默认的插件目标testmaven-surefire-plugin:testtest the compiled source code using a suitable unit testing framework.These tests should not require the code be packaged or deployedprepare-packagepackagemaven-jar-plugin:2.4:jartake the compiled code and package it in its distributable format, such as a JAR.pre-integration-test没有默认的插件目标integration-test没有默认的插件目标post-integration-test没有默认的插件目标verifyrun any checks on results of integration tests to ensure quality criteria are metinstallmaven-install-plugin:installinstall the package into the local repository, for use as a dependency in other projects locallydeploymaven-deploy-plugin:deploydone in the build environment, copies the final package to the remote repository for sharing with other developers and projects.site生命周期mvn help:describe-Dcmdsite-Ddetail#site is a phase within the site lifecycle, which has the following phases:#* pre-site: Not defined#* site: org.apache.maven.plugins:maven-site-plugin:3.3:site#* post-site: Not defined#* site-deploy: org.apache.maven.plugins:maven-site-plugin:3.3:deploy阶段插件目标说明pre-site没有默认的插件目标sitemaven-site-plugin:site生成项目站点文件post-site没有默认的插件目标site-deploymaven-site-plugin:deploy将生成的项目站点发布到服务器上built-in-lifecycle-bindings命令行与生命周期为了让用户几乎不用任何配置就能够构建Maven项目Maven在核心为一些主要的生命周期阶段绑定了很多插件的目标当用户通过命令行调用生命周期的阶段的时候对应的插件目标就会执行。需要注意的是各个生命周期是相互独立的而一个生命周期的阶段是前后依赖关系的。下面以一些常见的Maven命令为例解释其执行的生命周期阶段mvn clean该命令调用clean生命周期的clean阶段。实际执行的阶段为clean生命周期的pre-clean和clean阶段。maven-clean-plugin插件的clean目标也被触发。mvn test该命令调用default生命周期的test阶段。实际执行的阶段为default生命周期的validate、initialize等直到test的所有阶段。这也解释了为什么在执行测试的时候项目的代码能够自动得以编译。mvn clean install该命令调用clean生命周期的clean阶段和default生命周期的install阶段。实际执行的阶段为clean生命周期的pre-clean、clean阶段以及default生命周期的从validate至install的所有阶段。该命令结合了两个生命周期在执行真正的项目构建之前清理项目是一个很好的实践。mvn clean deploy site-deploy该命令调用clean生命周期的clean阶段、default生命周期的deploy阶段以及site生命周期的site-deploy阶段。实际执行的阶段为clean生命周期的pre-clean、clean阶段default生命周期的所有阶段以及site生命周期的所有阶段。该命令结合了Maven所有三个生命周期且deploy为default生命周期的最后一个阶段site-deploy为site生命周期的最后一个阶段。插件插件的目标被执行有几种情况被默认绑定到生命周期某个阶段当该生命周期被执行相应的目标也会被出发如命令mvn clean会触发插件maven-clean-plugin的clean目标将插件的某个目标显示的绑定到生命周期的某个阶段如将maven-source-plungin的jar-no-fork目标绑定到verify阶段那么该阶段被触发就会打包源代码手动触发如分析依赖的插件mvn dependency:dependency内置插件Maven内置了默认插件绑定不需要显式配置插件默认绑定的阶段maven-clean-plugincleanmaven-resources-pluginprocess-resources,process-test-resourcesmaven-compiler-plugincompile,test-compilemaven-surefire-plugintestmaven-jar-pluginpackagemaven-install-plugininstallmaven-deploy-plugindeploy自定义插件绑定查看插件的详细信息mvn help:describe “-Dpluginorg.apache.maven.plugins:maven-compiler-plugin”-DdetailPS D:\Program Files\apache-maven-3.9.8\lib\maven-model-builder-3.9.8\org\apache\maven\spring-test-demomvn help:describe “-Dpluginorg.apache.maven.plugins:maven-compiler-plugin”-Ddetail[INFO]Scanningforprojects...[INFO]------------------------------------------------------------------------[INFO]Reactor Build Order:[INFO][INFO]cloud-security-demo[pom][INFO]gateway-service[jar][INFO]common[jar][INFO]user-service[jar][INFO][INFO]------------------com.example:cloud-security-demo-------------------[INFO]Building cloud-security-demo1.0.0[1/4][INFO]--------------------------------[pom]---------------------------------[INFO][INFO]--- maven-help-plugin:3.2.0:describe(default-cli) cloud-security-demo ---[INFO]org.apache.maven.plugins:maven-compiler-plugin:3.10.1 Name: Apache Maven Compiler Plugin Description: The Compiler Plugin is used to compile the sources of your project. Group Id: org.apache.maven.plugins Artifact Id: maven-compiler-plugin Version:3.10.1 Goal Prefix: compiler This plugin has3goals: compiler:compile如果想查看某个插件目标的信息mvn help:describe “-Dpluginorg.apache.maven.plugins:maven-compiler-plugin”-Dgoalcompile-Ddetailmvn help:describe “-Dpluginorg.apache.maven.plugins:maven-compiler-plugin”-Dgoalcompile-Ddetail[INFO]Scanningforprojects...[INFO]------------------------------------------------------------------------[INFO]Reactor Build Order:[INFO][INFO]cloud-security-demo[pom][INFO]gateway-service[jar][INFO]common[jar][INFO]user-service[jar][INFO][INFO]------------------com.example:cloud-security-demo-------------------[INFO]Building cloud-security-demo1.0.0[1/4][INFO]--------------------------------[pom]---------------------------------[INFO][INFO]--- maven-help-plugin:3.2.0:describe(default-cli) cloud-security-demo ---[INFO]Mojo:compiler:compilecompiler:compile Description: Compiles application sources Implementation: org.apache.maven.plugin.compiler.CompilerMojo Language:javaBound to phase: compile Available parameters: annotationProcessorPaths Classpath elements to supply as annotation processor path. If specified, the compiler will detect annotation processors onlyinthose classpath elements. If omitted, the default classpath is used to detect annotation processors. The detection itself depends on the configuration of annotationProcessors. Each classpath element is specified using their Maven coordinates(groupId, artifactId, version, classifier,type). Transitive dependencies are added automatically. Example:configurationannotationProcessorPathspathgroupIdorg.sample/groupIdartifactIdsample-annotation-processor/artifactIdversion1.2.3/version/path!--...more... --/annotationProcessorPaths/configuration将maven-source-plungin的jar-no-fork目标当多个目标被绑定到同一个阶段他们的执行顺序有声明的向后顺序决定的。插件配置命令行配置插件参数POM中插件全局配置POM中插件任务配置插件解析依赖仓库和插件仓库是分开配置通常中央插件仓库就已经够用了所以不需要依赖只会沿着依赖仓库进行查找插件则只会沿着插件仓库查找。插件仓库配置如下就是在Maven内置的插件中央仓库如果的确需要配置其他的插件仓库则在pom.xml文件中配置pluginRepositoriespluginRepositoryidcentral/idnameMaven Plugin Repository/nameurlhttp://repol.maven.org/maven2/urllayoutdefault/layoutsnapshotsenabledfalse/enabled/snapshotsreleasesupdatePolicynever/updatePolicy/releases/pluginRepository/pluginRepositories插件版本解析针对内嵌的核心插件在超级POM中是指定了版本的对于非核心的插件Maven会在所有的插件仓库中寻找最新的release版本不过都是建议在pom.xml中锁定版本避免版本变换带来的不确定性。project. . . . . .buildresourcesresourcedirectorysrc/main/resources/directoryfilteringtrue/filteringincludesinclude**/*/include/includes/resource/resourcesplugins!-- Spring Boot Maven Plugin: 用于打包Spring Boot应用 --!-- 功能: 打包成可执行的JAR, 包含所有依赖, 支持spring-boot:run命令 即执行 mvn spring-boot:run 可以执行程序 --plugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build/project解析插件前缀插件前缀 artifactId去掉表示Maven插件的那部分后剩下的核心名字所以spring-boot-maven-plugin的前缀是spring-boot执行如下命令查看插件详细信息也能获取前缀mvn help:describe “-Dpluginorg.springframework.boot:spring-boot-maven-plugin”-Ddetail输出Name: Spring Boot Maven Plugin Description:(no description available)Group Id: org.springframework.boot Artifact Id: spring-boot-maven-plugin Version:4.1.0-M3 Goal Prefix: spring-boot This plugin has11goals: spring-boot:build-image可以看到Goal Prefix: spring-boot笔记查看完整pom.xmlmvn help:effective-pom
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2446350.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!