Lifecycle


https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html
https://maven.apache.org/guides/mini/guide-configuring-plugins.html

1. Important Points#

lifecycle:
    Maven 内置构建流程

phase:
    lifecycle 中的阶段,例如 test / package / deploy

goal:
    plugin 提供的具体动作,例如 surefire:test

2. Default Lifecycle#

Phase Purpose
validate 检查项目是否可构建
compile 编译 main code
test 运行 unit tests
package 打包 jar/war
verify 运行集成检查
install 安装到本地仓库
deploy 发布到远程仓库
./mvnw test
./mvnw package
./mvnw verify
./mvnw deploy
running a phase:
    Maven runs all previous phases in order

3. Bind Plugin Goal#

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-antrun-plugin</artifactId>
  <version>3.1.0</version>
  <executions>
    <execution>
      <id>print-build-info</id>
      <phase>validate</phase>
      <goals>
        <goal>run</goal>
      </goals>
      <configuration>
        <target>
          <echo message="building ${project.artifactId}"/>
        </target>
      </configuration>
    </execution>
  </executions>
</plugin>
execution:
    把一个 plugin goal 绑定到某个 phase

4. Unit Test And Integration Test#

unit test:
    maven-surefire-plugin
    default phase: test

integration test:
    maven-failsafe-plugin
    phases: integration-test and verify
./mvnw test
./mvnw verify

5. Plugin Prefix#

./mvnw dependency:tree
./mvnw help:effective-pom
dependency:tree:
    shorthand for maven-dependency-plugin tree goal

help:effective-pom:
    shorthand for maven-help-plugin effective-pom goal

6. Common Rules#

do:
    bind quality gates to verify
    bind deploy only in CI release jobs
    keep expensive scans out of local default build if they are too slow

avoid:
    running production deployment from package phase
    hiding network calls inside compile phase