全面详解Maven的配置文件pom

发布时间 2024-01-09 09:27:02作者: Mountain_SY

全面详解Maven的配置文件pom.xml(含常用plugin)

img

​ 上一次,我们讲解了Maven的安装与配置,那么这次我们就讲讲pom.xml,在Maven中,pom.xml是必不可少的配置文件,它定义了项目的各种属性和依赖关系。接下来,我们将全面解析pom.xml,了解其结构和属性,以及如何使用它来管理项目

一、什么是pom.xml

pom.xml是Maven项目的核心配置文件,它是 项目对象模型 - Project Object Model(POM)的缩写。POM定义了项目的所有属性,包括项目的名称、版本、依赖关系、构建配置等。使用pom.xml,我们可以轻松地管理项目的构建和依赖关系,让我们能够更专注于业务逻辑的开发。

二、pom.xml的结构

我们先看一个简单pom.xml的结构,首先标签主要针对的是本pom.xml文件的格式,如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>com.example</groupId>
  <artifactId>example-proj</artifactId>
  <version>1.0.0</version>

  <name>Example Project</name>
  <description>This is an example Maven project.</description>
  <url>http://www.example.com/</url>

  <licenses>   <!-- 许可证 -->
    <license>
      <name>The Apache Software License, Version 2.0</name>
      <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
      <distribution>repo</distribution>
    </license>
  </licenses>

  <developers>     <!-- 开发者信息 -->
    <developer>
      <id>developer1</id>
      <name>Developer One</name>
      <email>developer1@example.com</email>
      <organization>Example Organizations Inc.</organization>
      <organizationUrl>http://www.example-organizations.com/</organizationUrl>
      <roles>
        <role>developer</role>
      </roles>
      <timezone>-5</timezone>
    </developer>
  </developers>

  <dependencies>  <!-- 依赖项 -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>5.2.1.RELEASE</version>
    </dependency>
  </dependencies>

  <build>   <!-- 项目构建 -->
    <plugins>   <!-- 插件配置 -->
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.2.0</version>
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <mainClass>com.example.App</mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

其中<modelVersion>指定了pom.xml文件使用的XML schema版本,目前,其最新的版本是4.0.0。其他部分我们会在下面进行详解

三、项目的基本信息

与项目的基本信息相关的标签有很多,以下算必填项:

  • groupId:项目的组名,通常是反转的域名,比如com.example。

  • artifactId:项目的唯一标识符,通常是项目的名称。

  • version:项目的版本号。

  • packaging:项目的打包方式,通常是jarwarpom,如果没有指定packaging,默认值是jar。

除了上面的几个标签,还有一些项目相关,但非必填的内容:

  • name:项目名,可选项,提供项目的简短名称

  • description:项目描述,可选项,提供项目的详细描述。

  • version:项目主页,可选项,提供项目的网址。

  • licenses: 许可证声明,可选项,声明项目所使用的一种或多种许可证

  • developers:开发者信息,可选项,列出项目的开发人员。

  • url:项目主页,可选项,提供项目的网址

当然,还有一些在我们示例中没有出现的标签,比如说 modulesparent

1、modules

modules 标签用于声明当前 Maven 项目包含的模块子项目,每个子项目都是一个独立的 Maven 项目,具有自己的 pom.xml 文件,可以进行独立构建和测试。在父项目的 pom.xml 文件中,使用 标签来列出所有子项目的名称,如下所示:

<project>
  <groupId>com.example.parent</groupId>
  <artifactId>parent-project</artifactId>
  <version>1.0.0</version>
  <packaging>pom</packaging>

  <modules>
    <module>child1</module>
    <module>child2</module>
    <module>child3</module>
  </modules>
</project>

上述代码表示当前项目是一个 Maven 的多模块项目:它包含了三个子项目 child1、child2 和 child3,这三个子项目与 parent-project 有相同的 groupId 和 version,但是 artifactId 不同,它们的 pom.xml 都位于 parent-project 的根目录下。当使用 Maven 命令在 parent-project 下执行构建时,Maven 会对每个子模块执行构建,最终生成子项目的构件并复制到 parent-project 的 target 目录下。

2、parent

parent 标签用于声明当前 Maven 项目的父项目,它可以将若干个 Maven 项目组织成一个整体,指定版本号,插件版本号等,便于管理和维护,在一个 Maven 项目中,使用标签来引用父项目,如下所示:

<project>
  <groupId>com.example.child</groupId>
  <artifactId>child-project</artifactId>
  <version>1.0.0</version>
  <packaging>jar</packaging>

  <parent>
    <groupId>com.example.parent</groupId>
    <artifactId>parent-project</artifactId>
    <version>1.0.0</version>
    <relativePath>../parent-project/pom.xml</relativePath>
  </parent>
</project>

上述代码表示当前项目 child-project 是 parent-project 的子项目,它的 groupId 和 version 都继承自 parent-project。元素是一个可选项,它的值是父项目 pom.xml 文件到子项目 pom.xml 文件的相对路径,如果子项目 pom.xml 和父项目 pom.xml 在同一目录下,则可以省略此元素。

3、scm

scm 又叫 Software Configuration Management,即软件配置管理, 与我们以前提到过的版本控制有关,是Maven中用于指定源代码版本控制信息的标签。它可以帮助Maven获取源代码并将构建生成的二进制文件提交到版本控制系统中。scm标签通常用于指定源代码管理系统的类型、URL、开发者连接等详细信息。示例如下:

<scm>
    <!-- 指定连接到SCM的URL,可以使用HTTP或者SSH协议 -->
    <connection>scm:git:git://github.com/username/repo.git</connection>  
    <!-- 指定开发者连接到SCM的URL,通常使用SSH协议 -->
    <developerConnection>scm:git:ssh://github.com/username/repo.git</developerConnection> 
    <!-- 指定SCM的web页面URL,方便开发者查看SCM信息 -->
    <url>http://github.com/username/repo/tree/master</url> 
    <!-- 当前Maven构件的版本在SCM中对应的标记 -->
    <tag>1.0.0</tag> 
</scm>

developerConnection 与 connection 有些许不同,connection 可以指向一个本地文件系统路径,也可以是一个远程代码仓库的URL;developerConnection 则是开发者使用的版本控制系统的连接URL,例如connection指向只读代码仓库,而developerConnection则指向可写代码仓库。

通过在POM文件中添加scm标签,Maven可以获取源代码并构建项目,同时还可以自动将构建生成的文件提交到版本控制系统中,方便管理代码版本和协同开发

4、properties

properties 严格来说,并不一定是项目本身的信息,而是人为设置的属性或者说宏,这个标签用来定义和管理项目中所需要的属性,其作用有以下几个:

  • 统一管理项目中的常用属性,比如版本号、路径、插件版本等,方便统一修改和管理。
  • 可以在配置过程中使用 ${…}占位符引用这些属性,使得配置更加灵活和便捷。
  • 避免硬编码,提高代码的可维护性和可读性

比如说我们可以这么配:

<properties>
    <project.name>demo-project</project.name>
    <project.version>1.0.0</project.version>
    <jdk.version>1.8</jdk.version>
</properties>
....省略其余部分
<dependency>
    <groupId>com.example.demo</groupId>
    <artifactId>${project.name}-api</artifactId>
    <version>${project.version}</version>
</dependency>

四、项目的依赖列表

1、dependency

与项目的依赖列表相关的标签最外层由 <dependencies>来囊括,内部包含了各种具体的依赖<dependency>,该标签用于指定一个依赖项,它包含以下几个子标签

  • <groupId>:指定依赖项的groupId,项目的组名
  • <artifactId>:指定依赖项的artifactId,项目的唯一标识符
  • <version>:指定依赖项的版本号。
  • <scope>:指定依赖项在项目中的使用范围。

其中的 <scope>一般包含以下几种范围:常用的有compiletestprovidedruntime

  • compile:依赖库默认的 scope,表示该依赖库在编译、测试、运行时均需要使用。
  • provided:表示该依赖库只在编译和测试时需要使用,而在运行时已经被系统或者容器提供,所以不需要打包到最终的应用程序中。
  • runtime:表示该依赖库只在运行时需要使用,而在编译和测试时则不需要。
  • test:表示该依赖库只在测试时需要使用,而在编译和运行时则不需要。

比如说我们引入了 junit 包,但显然这个包我们不需要在打包时包含,只是用于测试,那么我们就可以将 junit 的 scope 设置为 test。

2、repository

当然,我们还能在pom文件中支持指定Maven仓库,即使用 <repositories><repository>标签,<repository>用于指定一个Maven仓库,它包含以下几个子标签:

  • <id>:指定Maven仓库的ID。
  • <name>:指定Maven仓库的名称。
  • <url>:指定Maven仓库的URL

五、 项目的构建配置

项目的构建配置信息,包括编译器版本、插件列表、源代码目录等,接下来我们慢慢来讲

1、build

build用于定义项目的构建配置,包括源代码目录、资源目录、插件等,其中代码部分的设置如下

<project>
  .... 省略其他部分
  <build>
    <sourceDirectory>src/main/java</sourceDirectory>
    <resources>
      <resource>
        <directory>src/main/resources</directory>
      </resource>
    </resources>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

需要注意的是,像资源目录这种路径是没有默认值的,但根据Maven的约定优于配置原则(Convention over Configuration),Maven会使用默认的目录结构去查找源代码和测试代码。默认的目录结构如下:

|--src
   |--main
      |--java  // Java主源代码目录
      |--resources // 资源文件目录
   |--test
      |--java // 测试主代码目录
      |--resources // 测试资源文件目录

因此,在一个标准的Maven项目中,sourceDirectory默认值应该是src/main/java。如果有自定义的代码目录结构,需要显式地设置sourceDirectory元素的值。例如,如果有一个名为“core”的子目录作为项目的主源代码目录,可以按以下方式进行配置:

<build>
  <sourceDirectory>core</sourceDirectory>
  ...
</build>

2、plugins

plugins的作用是定义 Maven 插件, plugins 主要用于扩展 Maven 的功能,帮助开发人员更方便地构建、打包、发布项目。插件可以通过 Maven 的插件中心或者自己构建的私有仓库来使用,能在构建过程中执行特定的任务,比如编译、打包、测试等。

插件的配置可以分为两种方式:全局配置项目配置。全局配置是在 Maven 安装目录下的 conf/settings.xml 文件中进行配置,可以被所有的项目使用。项目配置则是在项目的 pom.xml 文件中进行配置,只对当前项目生效。

插件的使用主要分为以下几个步骤:

  1. 在 pom.xml 中声明插件依赖
  2. 配置插件的参数
  3. 运行插件命令

而常用的Maven Plugin有不少,我们一一来说一下:

maven-compiler-plugin

比方说,最常用的编译功能,我们可以在pom里面这么写

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.8.1</version>
      <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <encoding>UTF-8</encoding>
        <showWarnings>true</showWarnings>
        <compilerArgs>
          <arg>-Xlint:unchecked</arg>
          <arg>-Xlint:deprecation</arg>
        </compilerArgs>
      </configuration>
    </plugin>
  </plugins>
</build>

各子标签的作用如下:

  • <source>:指定Java源代码的版本,例如1.8表示Java 8。
  • <target>:指定编译后的字节码版本,例如1.8表示Java 8。
  • <encoding>:指定源代码的编码格式。
  • <showWarnings>:是否显示编译警告信息,true表示显示,false表示不显示。
  • <compilerArgs>:可选参数,可以添加多个编译器参数,例如-Xlint选项用来启用编译器警告检查。

如果我们按示例中配置,我们就指定了编译器的源和目标版本为1.8,当我们使用 mvn compile 命令的时候,这个插件将会编译我们的 Java 代码,并将编译后的 class 文件放置在 target 目录下

maven-surefire-plugin

maven-surefire-plugin插件是Maven中的一个测试框架,用于执行Java单元测试和集成测试。它的主要作用是在构建过程中运行测试,并生成测试报告,在pom.xml中的配置如下:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M4</version>
            <configuration>
                <skipTests>true</skipTests>
                <forkCount>2</forkCount>
                <useSystemClassLoader>false</useSystemClassLoader>
                <reuseForks>true</reuseForks>
                <includes>
                    <include>**/*Test.java</include>
                    <include>**/*Tests.java</include>
                </includes>
                <systemProperties>
                    <property>
                        <name>testProp1</name>
                        <value>value1</value>
                    </property>
                    <property>
                        <name>testProp2</name>
                        <value>value2</value>
                    </property>
                </systemProperties>
            </configuration>
        </plugin>
    </plugins>
</build>

其中几个子标签的作用分别如下:

  • <skipTests>:设置是否跳过测试,默认值为false。

  • <forkCount>:设置并行运行测试的JVM进程数。

  • <useSystemClassLoader>:设置是否使用系统类加载器加载测试类。

  • <reuseForks>:设置是否重用已经启动的JVM进程。

  • <includes>:设置测试文件的过滤规则,支持通配符。

  • <systemProperties>:设置传递给测试环境的系统属性,可以在测试代码中通过System.getProperty()方法获取

maven-jar-plugin

maven-jar-plugin 用于将项目打包为JAR文件,在这个例子中,我们告诉Maven将com.example.MyApp作为JAR文件的主类,那么在pom.xml中的配置如下:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <version>3.1.0</version>
      <configuration>
        <archive>
          <manifest>
            <addClasspath>true</addClasspath>
            <mainClass>com.example.MyApp</mainClass>
          </manifest>
        </archive>
      </configuration>
    </plugin>
  </plugins>
</build>
  • <archive>:JAR文件的归档配置信息
  • <manifest>:MANIFEST.MF文件的配置信息
  • <addClasspath>:是否将依赖项添加到Class-Path条目中
  • <mainClass>:定义可执行JAR文件的入口类

maven-install-plugin

当执行mvn instal命令时,maven-install-plugin 用于将一个特定的文件安装到本地Maven仓库中,以便其他项目可以使用它,例如在pom.xml中的配置如下:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-install-plugin</artifactId>
    <version>3.0.0-M1</version>
    <configuration>
        <file>${project.build.directory}/example.jar</file>
        <groupId>com.example</groupId>
        <artifactId>example</artifactId>
        <version>1.0.0</version>
        <packaging>jar</packaging>
    </configuration>
</plugin>
  • <file>:用来指定要安装到本地Maven仓库中的文件的路径。该标签的值应该是一个文件的绝对或相对路径。

  • <groupId>:通过该标签设置所安装文件的groupId,通常表示项目的组织或组织部门的标识符。

  • <artifactId>:同样是通过该标签设置所安装文件的artifactId,通常是指该文件的名称。

  • <version>:通过该标签设置所安装文件的版本号,通常采用三级版本号的格式,例如"1.0.0"。

  • <packaging>:通过该标签来指定所安装文件的打包类型,通常是jar或war。

需要注意的是,<file>标签必须与<groupId><artifactId><version>标签一起使用,才能正确将该文件安装到本地Maven仓库中,并在其他项目中使用,除了以上的配置,还有一些可选的配置项:

:通过该标签指定所安装文件的分类器,例如"sources"或"javadoc"等,默认为null。
:通过该标签指定本地仓库的路径,默认为Maven默认的本地仓库路径。
:是否在安装文件时创建SHA-1校验和,默认为true。
:是否跳过该插件的运行,默认为false,即不跳过。

maven-clean-plugin

maven-clean-plugin 用于清理Maven项目中的目标文件和构建临时文件,以便重新构建项目。它通常被用于在构建之前清理项目,以确保在构建时使用最新的代码和资源ar文件,在pom.xml中的配置如下:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-clean-plugin</artifactId>
      <version>3.1.0</version>
      <executions>
        <execution>
          <id>clean-all</id>
          <phase>clean</phase>
          <goals>
            <goal>clean</goal>
          </goals>
          <configuration>
            <excludeDefaultDirectories>true</excludeDefaultDirectories>
            <filesets>
              <fileset>
                <directory>target</directory>
                <includes>
                  <include>**/*</include>
                </includes>
              </fileset>
            </filesets>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

该配置中,maven-clean-plugin的版本号是3.1.0,它在clean阶段(phase标签指定)执行,使用的目标是clean。下面是各个子标签的作用:

  • <excludeDefaultDirectories>:默认值为false,如果设置为true,则禁用清理操作中默认清理的目录(如target、bin等)。

  • <filesets>:文件集合,可以指定多个文件或文件夹需要被清理。

  • <fileset>:单个的文件或文件夹。

  • <directory>:需要清理的文件夹路径。

  • <includes>:需要包含的文件或文件夹,支持通配符。

  • <excludes>:需要排除的文件或文件夹,支持通配符

至于通配符,使用规则如下:

*  匹配零个或多个字符
** 匹配零个或多个目录

需要注意的是,Maven的通配符仅支持*和**,不支持其他通配符,例如?。同时,通配符匹配的范围是相对于构建目录的,也就是默认情况下是相对于pom.xml文件的目录

maven-release-plugin

maven-release-plugin 可以帮助我们在代码库中创建一个稳定的发布版本,并将其发布到Maven仓库中,同时更新开发版本号,以便于下次开发版本的迭代,它可以做如下配置

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-release-plugin</artifactId>
      <version>2.5.2</version>
      <configuration>
        <tagNameFormat>v@{project.version}</tagNameFormat>
        <tagBase>http://svn.example.com/tags</tagBase>
        <autoVersionSubmodules>true</autoVersionSubmodules>
        <releaseProfiles>release</releaseProfiles>
        <branchBase>http://svn.example.com/branches</branchBase>
      </configuration>
    </plugin>
  </plugins>
</build>
  • <tagNameFormat>: 指定发布版本的标签格式,@{project.version}会被替换为项目的版本号。在上面的配置中,标签格式为v@{project.version}。

  • <tagBase>: 用于指定打标签的位置,默认值为 ${project.scm.url},即和项目的 SCM 地址相同。

  • <autoVersionSubmodules>: 是否自动更新子模块的版本号。如果设置为true,则子模块的版本号会自动更新为父模块的版本号。

  • <releaseProfiles>: 指定触发发布的Maven profile。只有在激活该profile后才会触发发布操作。在上面的配置中,只有当profile名称为release时,才会触发发布操作,关于profile,我们下面会讲

  • <branchBase>:用于指定创建分支的位置,默认值同 tagBase,即和项目的 SCM 地址相同。

3、profiles

profiles用于定义 Maven 运行时的不同配置环境,比如开发环境、测试环境、生产环境等,可以在不同的环境中使用不同的配置,比如我们做了如下配置

<profiles>
  <profile>
    <id>prod</id>
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>
    <build>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.1</version>
          <configuration>
            <release>11</release>
          </configuration>
        </plugin>
      </plugins>
    </build>
  </profile>
</profiles>
  • <id> 标签指定profile的唯一标识符。

  • <activation> 标签指定何时使用该profile。在示例中,activeByDefault设置为true表示默认启用该profile。0

  • <build> 标签包含一组构建配置,这些配置将在激活profile时覆盖默认配置。在示例中,它定义了maven-compiler-plugin插件的版本和为Java 11设置编译器版本。

在Maven中,使用以下命令激活特定的profile:

mvn clean install -Pprod

这将激活prod profile,覆盖默认构建配置。

六、pom.xml的使用

经过了上面的学习,不难发现,使用pom.xml可以轻松地管理项目的构建和依赖关系,其主要用法其实有三种:

  1. 添加依赖:在dependencies标签下添加依赖,包括groupId、artifactId、version、scope等信息。
  2. 修改打包方式:在packaging标签下修改项目的打包方式,通常是jar、war或pom。
  3. 配置插件:在build标签下配置插件,包括groupId、artifactId、version等信息。插件可以帮助我们处理各种构建任务,比如编译代码、生成文档、打包文件等。

七、Maven 生命周期和插件

Maven 的构建过程是由一系列的生命周期和插件来管理的。了解Maven的生命周期和插件,有助于理解项目的构建过程,以及在构建过程中可以执行哪些任务。

Maven 生命周期

Maven生命周期是一系列阶段的集合,定义了项目的构建过程。常用的生命周期包括:

  • clean: 清理项目,删除 target 目录。

  • validate: 验证项目是否正确。

  • compile: 编译项目的源代码。

  • test: 使用单元测试框架运行测试。

  • package: 将编译好的代码打包成可分发的格式,比如JAR。

  • verify: 对集成测试的结果进行验证,以保证质量。

  • install: 将打包好的项目发布到本地仓库。

  • deploy: 将项目发布到远程仓库。

这些生命周期是顺序执行的,你可以在某一个生命周期的阶段执行自定义的插件任务。

 <build>
    <!--扫描文件资源-->
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.xml</include>
        </includes>
      </resource>
      <resource>
        <directory>src/main/resources</directory>
        <includes>
          <include>**/*.xml</include>
        </includes>
      </resource>
    </resources>

 <!--插件-->
 <pluginManagement><!-- lock down plugins versions to avoid 
 using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current
        /maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see 
        https://maven.apache.org/ref/current/maven-core/default-
        bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/
        maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

Maven 插件

Maven 插件是执行构建任务的工具,它们与生命周期和阶段相关联。常见的插件包括:

  • maven-compiler-plugin: 用于编译Java源代码。

  • maven-surefire-plugin: 用于执行单元测试。

  • maven-jar-plugin: 用于打包JAR文件。

  • maven-failsafe-plugin: 用于执行集成测试。

pom.xml 文件中,可以通过配置插件来定制项目的构建过程。以下是一个例子,使用 maven-compiler-plugin 插件配置Java编译器版本:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

这里,我们配置了 maven-compiler-plugin 插件,指定了Java源代码和目标字节码的版本。

八、Maven 多模块项目

Maven支持多模块项目,通过模块化的方式组织代码和资源。多模块项目的结构类似于单模块项目,但包含了额外的父模块和子模块。

创建父模块

首先,创建一个父模块的Maven项目,用于管理子模块。在命令行中执行:

mvn archetype:generate -DgroupId=com.example -DartifactId=my-parent-module -DarchetypeArtifactId=maven-archetype-pom -DinteractiveMode=false

这个命令使用了 maven-archetype-pom 模板,生成了一个空的父模块项目。

创建子模块

在父模块的目录下,创建两个子模块的Maven项目:

mkdir my-child-module1
cd my-child-module1
mvn archetype:generate -DgroupId=com.example -DartifactId=my-child-module1 -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
cd ..

mkdir my-child-module2
cd my-child-module2
mvn archetype:generate -DgroupId=com.example -DartifactId=my-child-module2 -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
cd ..

这样,我们得到了一个父模块和两个子模块的多模块项目结构。

父模块的 pom.xml

在父模块的 pom.xml 文件中,使用 <modules> 元素列出所有子模块:

<modules>
    <module>my-child-module1</module>
    <module>my-child-module2</module>
</modules>

这个配置告诉Maven父模块下有哪些子模块。

子模块的 pom.xml

在每个子模块的 pom.xml 文件中,需要指定父模块的信息。例如:

<parent>
    <groupId>com.example</groupId>
    <artifactId>my-parent-module</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>

<artifactId>my-child-module1</artifactId>

这样,每个子模块都知道它们的父模块是谁,以及从父模块继承的信息。

构建多模块项目

在父模块的目录下执行Maven命令:

mvn clean install

这个命令将递归构建所有子模块,并将它们安装到本地仓库。你可以在每个子模块中执行单独的Maven命令,或者在父模块中执行命令。

cd my-parent-module
mvn clean install

这样,就完成了一个简单的多模块Maven项目的创建和构建。

九、Maven 高级特性

Maven 插件配置

在Maven中,插件配置是非常灵活的。你可以为插件定义各种配置参数,以满足项目的需求。

<build>
    <plugins>
        <!-- Maven Compiler 插件配置 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>

        <!-- Maven Surefire 插件配置 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.2</version>
            <configuration>
                <skipTests>false</skipTests>
                <testFailureIgnore>true</testFailureIgnore>
            </configuration>
        </plugin>
    </plugins>
</build>

在这个例子中,我们为 maven-compiler-pluginmaven-surefire-plugin 插件配置了一些参数,比如Java版本和测试配置。

Maven Profiles

Maven允许使用profiles来定义一组构建配置,以便根据不同的环境或需求执行不同的构建。在 pom.xml 文件中,可以通过 <profiles> 元素定义不同的profile。

<profiles>
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <environment>dev</environment>
        </properties>
    </profile>
    <profile>
        <id>prod</id>
        <properties>
            <environment>prod</environment>
        </properties>
    </profile>
</profiles>

在这个例子中,我们定义了两个profile,一个是dev,一个是prod。在执行Maven命令时,可以通过 -P 参数指定要激活的profile。

mvn clean install -P prod

这样,可以根据不同的profile执行不同的构建逻辑。

Maven 插件编写

Maven插件是Maven项目的基础。如果你有特定的构建需求,而现有的插件无法满足,你可以考虑编写自己的Maven插件。

插件编写涉及到Java编程和Maven插件的结构。通常,一个Maven插件项目包含以下几个部分:

  • Mojo(目标): 插件的基本执行单元,定义了插件的一个具体任务。

  • Plugin: 插件的配置和描述,定义了插件的名称、目标等。

  • PluginDescriptor: 插件的描述信息,包括插件的目标、参数等。

  • PluginManager: 插件的管理器,用于加载和执行插件。

这里只是简单提及插件编写的主要部分,具体的插件编写涉及到更多的细节和实践。

推荐几个好的 Maven 常用仓库网址:

http://mvnrepository.com/
http://search.maven.org/
http://repository.sonatype.org/content/groups/public/
http://people.apache.org/repo/m2-snapshot-repository/
http://people.apache.org/repo/m2-incubating-repository/