Maven Lifecycle and Goals

本文介绍了Maven中生命周期阶段与插件目标之间的联系,解释了如何通过命令行调用特定阶段并查看绑定的插件及其版本。根据不同类型的项目,Maven会自动绑定不同的插件目标到相应的生命周期阶段。
The previous section covered plugin goals and how to run goals from command line. In very few situations we invoke plugin goals directly and more often than not, lifecycle phases are preferred. In this chapter, we describe the link between Maven Lifecycle and Goals.

Lifecycle Phases and Plugin Goals

When a lifecycle phase is run, depending on project type and packaging type, Maven binds plugin goals to lifecycle phases.
When we run mvn package in a Java Project, Maven binds plugin goals to lifecycle phases as shown in the next figure.
Maven Lifecycle and Goals - Package Goals
To process-resources phase, Maven binds resources goal of maven-resources-plugin and to test phase, it binds test goal of maven-surefire-plugin and so on.
What’s happens at package phase is bit interesting. In a Java Project, Maven binds jar goal of maven-jar-plugin. However, when we run the same command in a webapp project, up to test phase Maven binds same goals, but to the package phase Maven binds war goal of maven-war-plugin the war:war instead of jar:jar.
The command mvn help:describe -Dcmd=<phase> is a very useful command not only to list the lifecycle phases but also to know the binded goal and plugin version. Run the following command from the project directory that contains the pom.xml.
$ mvn help:describe -Dcmd=clean

[INFO] ‘clean’ is a lifecycle with the following phases: 

* pre-clean: Not defined
* clean: org.apache.maven.plugins:maven-clean-plugin:2.5:clean
* post-clean: Not defined

We pass lifecycle phase clean with argument -Dcmd. The help:describe validates the pom.xml and based on project type outputs the sequence of lifecycle phases that will be executed for the lifecycle and plugin goals bounded to each phase. As we can see in the screenshot, no goals are bounded to pre-clean and post-clean phases and they are indicated as not defined and for clean phase, clean:clean goal of maven-clean-plugin (version 2.5) is attached.
The following command outputs the sequence of phases and goals for default lifecycle The phase deploy belongs to default lifecycle so command output details of default lifecycle.
$ mvn help:describe -Dcmd=deploy

It is a part of the lifecycle for the POM packaging ‘jar’.
This lifecycle includes the following phases:

* validate: Not defined
* initialize: Not defined
* generate-sources: Not defined
* process-sources: Not defined
* generate-resources: Not defined
* process-resources: org.apache.maven.plugins:maven-resources-plugin:2.6:resources
* compile: org.apache.maven.plugins:maven-compiler-plugin:3.1:compile
* process-classes: Not defined
* generate-test-sources: Not defined
* process-test-sources: Not defined
* generate-test-resources: Not defined
* process-test-resources: org.apache.maven.plugins:maven-resources-plugin:2.6:testResources
* test-compile: org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile
* process-test-classes: Not defined
* test: org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test
* prepare-package: Not defined
* package: org.apache.maven.plugins:maven-jar-plugin:2.4:jar
* pre-integration-test: Not defined
* integration-test: Not defined
* post-integration-test: Not defined
* verify: Not defined
* install: org.apache.maven.plugins:maven-install-plugin:2.4:install
* deploy: org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy

Lifecycles, Lifecycle Phases, Plugins and Plugin Goals are the core of Maven. and we summarize the concepts learned so far:
  • Maven comes with three lifecycles – default, clean and site.
  • each lifecycle is made up of lifecycle phases and in all, there are 28 phases – default 21, clean 3 and site 4.
  • when a lifecycle phase is invoked using mvn command, all preceding phases are executed sequentially one after another.
  • lifecycle phases by themselves doesn’t have any capabilities to accomplish some task and they rely on plugins to carryout the task.
  • depending on project and packaging type, Maven binds various plugin goals to lifecycle phases and goals carryout the task entrusted to them.

Maven 中执行 **Lifecycle -> package**,是指通过 Maven 的生命周期命令将 Java 项目进行编译、测试、打包等操作,最终生成一个可部署的文件(如 `.jar` 或 `.war` 文件)。 --- ### ✅ 一、使用命令行执行 `mvn package` 这是最常见、最通用的方式。 #### 步骤: 1. 打开终端(Windows 上是 CMD 或 PowerShell,Mac/Linux 是 Terminal)。 2. 进入你的项目根目录(即包含 `pom.xml` 文件的目录)。 3. 执行以下命令: ```bash mvn package ``` #### 示例输出: ```bash [INFO] Scanning for projects... [INFO] Building your-project-name 1.0.0 [INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) --- [INFO] --- maven-surefire-plugin:2.22.0:test (default-test) --- [INFO] --- maven-jar-plugin:3.2.1:jar (default-jar) --- [INFO] Building jar: your-project/target/your-project-name.jar ``` #### 💡 附加选项: - 清理后打包: ```bash mvn clean package ``` - 跳过测试打包: ```bash mvn package -DskipTests ``` --- ### ✅ 二、在 IntelliJ IDEA 中执行 `package` 如果你使用的是 IntelliJ IDEA,可以通过图形界面操作执行 Maven 的 `package` 生命周期。 #### 步骤: 1. 打开项目,确保项目根目录下有 `pom.xml`。 2. 在右侧找到 **Maven** 工具窗口(如果没有,点击 `View -> Tool Windows -> Maven`)。 3. 展开当前项目的生命周期(Lifecycle)。 4. 右键点击 `package`,选择 **Run Maven Build**。 或者双击 `package` 直接执行。 #### 📌 注意: - 执行成功后,生成的 `.jar` 文件会在 `target/` 目录下。 - 如果你使用的是 Spring Boot,确保 `pom.xml` 中配置了 `spring-boot-maven-plugin` 插件,否则生成的 JAR 不可运行。 --- ### ✅ 三、在 Eclipse 中执行 `package` 如果你使用的是 Eclipse: #### 步骤: 1. 右键点击项目 → **Run As** → **Maven build...** 2. 在 Goals 输入框中输入: ``` package ``` 3. 点击 **Run** 开始执行。 --- ### ✅ 四、生成的 JAR 文件在哪? 执行完 `mvn package` 后,生成的 `.jar` 文件默认位于: ``` your-project/ └── target/ └── your-project-name.jar ``` 你可以通过以下命令运行它: ```bash java -jar your-project-name.jar ``` --- ### ✅ 五、`package` 阶段会执行哪些操作? 执行 `mvn package` 会依次运行以下阶段(默认): | 阶段 | 说明 | |------|------| | `validate` | 验证项目结构是否正确 | | `compile` | 编译源代码(`src/main/java`) | | `test` | 运行单元测试(不会打包进去) | | `package` | 打包成 JAR/WAR 文件 | --- ### ✅ 示例:Spring Boot 项目打包配置(`pom.xml`) 确保你的 `pom.xml` 中包含以下配置,才能生成可运行的 JAR 包: ```xml <packaging>jar</packaging> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ``` --- ### ✅ 总结 | 方式 | 描述 | |------|------| | 命令行执行 `mvn package` | 最常用,适合所有环境 | | IntelliJ IDEA 图形界面 | 快速可视化操作 | | Eclipse 图形界面 | 同样支持图形化执行 | | 生成的 `.jar` 文件 | 位于 `target/` 目录下,可直接运行 | ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值