📜  Maven-构建生命周期(1)

📅  最后修改于: 2023-12-03 15:17:35.850000             🧑  作者: Mango

Maven 构建生命周期

Maven 是 Java 程序员常用的项目管理工具。Maven 的构建过程中有三个基本的构建生命周期:Clean Lifecycle、Default Lifecycle 和 Site Lifecycle。本文将介绍每个生命周期的具体步骤和目标。

Clean Lifecycle

Clean Lifecycle 用于清理项目。它包含了以下几个阶段:

pre-clean

在 clean 目标执行之前执行的步骤。

<plugins>
  <plugin>
    <groupId>some-group</groupId>
    <artifactId>some-plugin</artifactId>
    <executions>
      <execution>
        <id>pre-clean-id</id>
        <phase>pre-clean</phase>
        <goals>
          <goal>pre-clean</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
</plugins>
clean

用于清理项目。

<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-clean-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
      <execution>
        <id>clean-id</id>
        <phase>clean</phase>
        <goals>
          <goal>clean</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
</plugins>
post-clean

在 clean 目标执行之后执行的步骤。

<plugins>
  <plugin>
    <groupId>some-group</groupId>
    <artifactId>some-plugin</artifactId>
    <executions>
      <execution>
        <id>post-clean-id</id>
        <phase>post-clean</phase>
        <goals>
          <goal>post-clean</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
</plugins>
Default Lifecycle

Default Lifecycle 是构建过程中的主要部分,它包括以下几个阶段:

validate

用于验证项目是否正确。

<plugins>
  <plugin>
    <groupId>some-group</groupId>
    <artifactId>some-plugin</artifactId>
    <executions>
      <execution>
        <id>validate-id</id>
        <phase>validate</phase>
        <goals>
          <goal>validate</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
</plugins>
compile

用于编译项目。

<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.1</version>
    <executions>
      <execution>
        <id>compile-id</id>
        <phase>compile</phase>
        <goals>
          <goal>compile</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
</plugins>
test

用于测试项目。

<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.2</version>
    <executions>
      <execution>
        <id>test-id</id>
        <phase>test</phase>
        <goals>
          <goal>test</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
</plugins>
package

用于打包项目。

<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.2.0</version>
    <executions>
      <execution>
        <id>package-id</id>
        <phase>package</phase>
        <goals>
          <goal>jar</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
</plugins>
install

用于将项目安装到本地 Maven 仓库。

<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-install-plugin</artifactId>
    <version>2.5.2</version>
    <executions>
      <execution>
        <id>install-id</id>
        <phase>install</phase>
        <goals>
          <goal>install</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
</plugins>
deploy

用于将项目安装到远程 Maven 仓库。

<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-deploy-plugin</artifactId>
    <version>3.0.0-M1</version>
    <executions>
      <execution>
        <id>deploy-id</id>
        <phase>deploy</phase>
        <goals>
          <goal>deploy</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
</plugins>
Site Lifecycle

Site Lifecycle 用于生成项目的 site 文档。

pre-site

在 site 目标执行之前执行的步骤。

<plugins>
  <plugin>
    <groupId>some-group</groupId>
    <artifactId>some-plugin</artifactId>
    <executions>
      <execution>
        <id>pre-site-id</id>
        <phase>pre-site</phase>
        <goals>
          <goal>pre-site</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
</plugins>
site

用于生成项目的 site 文档。

<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-site-plugin</artifactId>
    <version>3.9.0</version>
    <executions>
      <execution>
        <id>site-id</id>
        <phase>site</phase>
        <goals>
          <goal>site</goal>
        </goals>
        <configuration>
          <outputDirectory>target/site</outputDirectory>
        </configuration>
      </execution>
    </executions>
  </plugin>
</plugins>
post-site

在 site 目标执行之后执行的步骤。

<plugins>
  <plugin>
    <groupId>some-group</groupId>
    <artifactId>some-plugin</artifactId>
    <executions>
      <execution>
        <id>post-site-id</id>
        <phase>post-site</phase>
        <goals>
          <goal>post-site</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
</plugins>
site-deploy

用于将 site 文档安装到远程仓库。

<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-site-plugin</artifactId>
    <version>3.9.0</version>
    <executions>
      <execution>
        <id>site-deploy-id</id>
        <phase>site-deploy</phase>
        <goals>
          <goal>deploy</goal>
        </goals>
        <configuration>
          <outputDirectory>target/site</outputDirectory>
        </configuration>
      </execution>
    </executions>
  </plugin>
</plugins>
Conclusion

以上就是 Maven 的构建生命周期,每个生命周期都有具体的几个阶段和目标。通过配置这些阶段和目标,可以让 Maven 将项目顺利地从代码到可运行的程序构建出来。