0%

maven插件

Maven 插件全解析:从基础到高级应用

Maven 作为一款强大的项目管理工具,其核心功能完全依赖插件实现。无论是编译代码、打包项目,还是生成报告,都需要通过插件完成。本文将系统介绍 Maven 插件的分类、常用插件配置及调用方式,帮助你灵活掌控项目构建流程。

Maven 插件基础

插件的本质

Maven 插件是实现特定功能的 Java 类,通过 “目标(Goal)” 定义具体操作(如编译、打包)。每个插件可包含多个目标,例如 maven-compiler-plugin 包含 compile(编译主代码)和 testCompile(编译测试代码)两个目标。

插件语法

调用插件的基本格式:

1
mvn [插件前缀]:[目标]
  • 示例:mvn compiler:compile(调用编译插件的 compile 目标)。

插件分类

  • 构建插件(Build Plugins):参与项目构建过程,如编译、打包、测试等,默认绑定到 Maven 生命周期的特定阶段。
    示例:maven-compiler-pluginmaven-jar-plugin
  • 报告插件(Reporting Plugins):生成项目报告(如测试覆盖率、依赖分析),默认绑定到 site 生命周期。
    示例:maven-javadoc-pluginmaven-surefire-report-plugin

常用核心插件详解

编译插件:maven-compiler-plugin

功能:指定 Java 编译版本,确保代码兼容性。

1
2
3
4
5
6
7
8
9
10
11
<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> <!-- 编译后 class 版本 -->
<encoding>UTF-8</encoding> <!-- 编码格式 -->
<fork>true</fork> <!-- 独立进程编译,避免冲突 -->
</configuration>
</plugin>

说明:若不配置,Maven 默认使用 Java 1.5 兼容模式,需显式指定高版本(如 1.8)。

项目骨架插件:maven-archetype-plugin

功能:快速生成标准化项目结构(如普通 Java 项目、Web 项目)。

常用命令

1
2
3
4
5
6
7
8
9
10
11
12
13
# 生成普通 Java 项目
mvn archetype:generate \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DgroupId=com.zhanghe \
-DartifactId=myproject \
-Dversion=1.0-SNAPSHOT

# 生成 Web 项目
mvn archetype:generate \
-DarchetypeArtifactId=maven-archetype-webapp \
-DgroupId=com.zhanghe \
-DartifactId=mywebapp \
-Dversion=1.0-SNAPSHOT

打包插件:maven-jar-pluginmaven-war-plugin

maven-jar-plugin(Java 项目打包)

功能:自定义 Jar 包内容,指定主类实现可执行 Jar。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<mainClass>com.zhanghe.Main</mainClass> <!-- 主类全路径 -->
<addClasspath>true</addClasspath> <!-- 自动添加类路径 -->
<classpathPrefix>lib/</classpathPrefix> <!-- 依赖存放目录 -->
</manifest>
</archive>
<includes> <!-- 仅打包指定文件 -->
<include>com/zhanghe/**/*.class</include>
</includes>
</configuration>
</plugin>
maven-war-plugin(Web 项目打包)

功能:定制 WAR 包结构(如排除冗余文件)。

1
2
3
4
5
6
7
8
9
10
11
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.2</version>
<configuration>
<warName>mywebapp</warName> <!-- 自定义 WAR 文件名 -->
<excludes> <!-- 排除不需要的文件 -->
<exclude>WEB-INF/classes/dev.properties</exclude>
</excludes>
</configuration>
</plugin>

依赖打包插件:maven-assembly-pluginmaven-shade-plugin

maven-assembly-plugin(灵活打包)

功能:将项目及依赖打包为 Jar、Zip、Tar 等格式,支持自定义目录结构。

配置示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<descriptors>
<!-- 自定义打包规则(需创建 src/main/assembly/assembly.xml) -->
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<phase>package</phase> <!-- 绑定到打包阶段 -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>

assembly.xml 示例(打包为包含依赖的 Tar 包):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<assembly>
<id>dist</id> <!-- 包名后缀 -->
<formats><format>tar.gz</format></formats> <!-- 打包格式 -->
<fileSets>
<!-- 项目 Jar 包 -->
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>/</outputDirectory>
<includes><include>*.jar</include></includes>
</fileSet>
<!-- 依赖包 -->
<fileSet>
<directory>${project.build.directory}/lib</directory>
<outputDirectory>/lib</outputDirectory>
</fileSet>
</fileSets>
</assembly>
maven-shade-plugin(解决依赖冲突)

功能:合并依赖类到 Jar 包,支持重命名类以避免冲突。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.4.1</version>
<executions>
<execution>
<phase>package</phase>
<goals><goal>shade</goal></goals>
<configuration>
<transformers>
<!-- 指定主类 -->
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.zhanghe.Main</mainClass>
</transformer>
</transformers>
<!-- 重命名冲突类(如将 com.google 改为 shaded.com.google) -->
<relocations>
<relocation>
<pattern>com.google</pattern>
<shadedPattern>shaded.com.google</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution>
</executions>
</plugin>

依赖管理插件:maven-dependency-plugin

功能:分析依赖、复制依赖到指定目录,解决依赖冲突。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<phase>prepare-package</phase> <!-- 打包前执行 -->
<goals><goal>copy-dependencies</goal></goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory> <!-- 依赖输出目录 -->
<excludeScope>test</excludeScope> <!-- 排除测试依赖 -->
</configuration>
</execution>
</executions>
</plugin>

常用命令

  • mvn dependency:tree:查看依赖树(分析冲突)。
  • mvn dependency:analyze:检查未使用的依赖。
  • dependency:list能够列出项目最终解析到的依赖列表;
  • dependency:copy-dependencies能将项目依赖从本地Maven仓库复制到某个特定的文件夹下面

资源处理插件:maven-resources-plugin

功能:处理资源文件(如过滤变量、指定资源目录)。默认的主资源文件目录是src/main/resources,很多用户会需要添加额外的资源文件目录。此外,资源文件过滤也是Maven的一大特性,可以在资源文件中使用${propertyName}形式的Maven属性,然后配置maven-resources-plugin开启对资源文件的过滤,之后就可以针对不同环境通过命令行或者Profile传入属性的值,以实现更为灵活的构建

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<encoding>UTF-8</encoding> <!-- 资源文件编码 -->
</configuration>
<executions>
<execution>
<id>copy-resources</id>
<phase>process-resources</phase>
<goals><goal>copy-resources</goal></goals>
<configuration>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<resources>
<!-- 主资源目录 -->
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering> <!-- 启用变量过滤(如 ${version}) -->
<includes>
<include>application.yml</include>
<include>application-${profileActive}.yml</include>
<include>mapper/**/*.xml</include>
<include>static/**</include>
<include>templates/**</include>
<include>*.xml</include>
<include>*.properties</include>
</includes>
</resource>
<!-- 额外资源目录 -->
<resource>
<directory>src/main/config</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>

其他实用插件

  • maven-surefire-plugin:执行单元测试(默认绑定到 test 阶段)。

  • maven-release-plugin:自动化版本发布(如从 SNAPSHOT 升级到 RELEASE)。

    • release:prepare用来准备版本发布,具体的工作包括检查是否有未提交代码、检查是否有SNAPSHOT依赖、升级项目的SNAPSHOT版本至RELEASE版本、为项目打标签等等。
    • release:perform是签出标签中的RELEASE源码,构建并发布。
  • maven-enforcer-plugin:强制检查规则(如禁止 SNAPSHOT 依赖、指定 Java 版本)。

  • maven-help-plugin:查看有效 POM、插件信息(如 mvn help:effective-pom)。

  • help:system可以打印所有可用的环境变量和Java系统属性
  • help:effective-pom 用来打印项目的有效POM,有效POM是指合并了所有父POM(包括Super POM)后的XML,当你不确定POM的某些信息从何而来时,就可以查看有效POM
  • help:effective-settings用来打印项目的有效settings
  • maven-help-plugin的describe目标可以帮助你描述任何一个Maven插件的信息
  • all-profiles目标和active-profiles目标帮助查看项目的Profile

插件的调用方式

绑定到生命周期阶段

将插件目标与 Maven 生命周期阶段(如 compilepackage)绑定,执行阶段时自动触发目标。

示例:将 Thrift 编译插件绑定到 generate-sources 阶段:

1
2
3
4
5
6
7
8
9
10
11
<plugin>
<groupId>org.apache.thrift.tools</groupId>
<artifactId>maven-thrift-plugin</artifactId>
<version>0.1.10</version>
<executions>
<execution>
<phase>generate-sources</phase> <!-- 绑定阶段 -->
<goals><goal>compile</goal></goals> <!-- 执行目标 -->
</execution>
</executions>
</plugin>

说明:部分插件目标有默认绑定阶段(如 compiler:compile 默认绑定到 compile 阶段),可省略 <phase> 配置。

插件的目标本身可能在编写时就已经定义了默认绑定阶段,使用maven-help-plugin来查看插件的详细信息

命令行直接调用

通过 插件前缀:目标 直接执行,无需绑定生命周期。

示例

1
2
3
mvn archetype:generate  # 生成项目骨架
mvn dependency:tree # 查看依赖树
mvn help:effective-pom # 查看有效 POM

插件版本管理

始终指定插件的具体版本,避免因自动升级导致的兼容性问题:

1
2
3
4
5
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version> <!-- 显式指定版本 -->
</plugin>

推荐:使用 Maven 中央仓库查询最新稳定版本(Maven Repository

欢迎关注我的其它发布渠道

表情 | 预览
快来做第一个评论的人吧~
Powered By Valine
v1.3.10