post:增加Maven常用插件文章。

This commit is contained in:
徐涛 2021-06-10 17:32:41 +08:00
parent 93e694ea74
commit fee2a6f933

View File

@ -0,0 +1,325 @@
---
title: 常用的Maven插件记录
tags:
- JVM
- Java
- Maven
- Plugins
categories:
- - JVM
- 构建工具
keywords: 'maven,plugins,插件'
date: 2021-06-10 17:32:15
---
Maven是一个依赖插件执行的框架每个任务实际上都是由插件完成的。<!-- more -->插件通常提供的是一个目标goal的集合也就是命令行格式`mvn [options] [goals] [phases]`中的`[goals]`。要执行一个插件的目标,可以使用命令格式`mvn [plugin-name]:[goal-name]`,例如`mvn jetty:run`。
在Maven中有Build和Report两种类型的插件其中Build用于在构建时执行Report用于在网站生成过程中执行。
每个插件可以有多个目标,插件可以使用`phase`元素来定义执行时机。要执行的任务则需要绑定到插件的目标上来执行。以下给出一个示例。
```xml
<build>
<plugins>
<plugin>
<!--引入要使用的插件-->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.1</version>
<!--定义要执行的动作-->
<executions>
<execution>
<id>on.clean</id>
<!--激活的阶段-->
<phase>clean</phase>
<!--激活的目标-->
<goals>
<goal>run</goal>
</goals>
<configuration>
<!--绑定到目标的任务-->
<tasks>
<echo>Now in clean phase.</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
```
## maven-compiler-plugin
Compiler插件主要用来编译项目源码。在POM中一般用来配置编译所接受的源码版本和编译后能够适应的Java运行时版本。代码版本的配置有两种方式一种是通过配置项指定一种是通过配置插件指定。
以下是通过配置项指定的示例。
```xml
<project>
<properties>
<!--源代码所使用的语言版本-->
<maven.compiler.source>1.8</maven.compiler.source>
<!--编译后要使用的 Java 运行时版本-->
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
```
或者可以直接配置插件。
```xml
<project>
<build>
<plugins>
<plugin>
<!--groupId 是 org.apache.maven.plugins 的插件,可以省略书写 groupId-->
<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-antrun-plugin
Antrun插件用来在一些指定阶段中运行Ant任务并且对书写在POM中的Ant任务提供支持。Antrun插件在前面一些章节已经有所接触其具体使用格式如下。
```xml
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase><!--指定激活阶段--></phase>
<configuration>
<target><!--编写要执行的 Ant 任务--></target>
</configuration>
<goals>
<!-- Antrun 插件只提供了一个目标-->
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
```
## maven-jar-plugin
Jar插件主要用来增强构建Jar包的功能为Jar包中增加`MANIFEST.MF`文件,或者通过提供`MANIFEST.MF`中所需的内容来自动构建`MANIFEST.MF`文件。
以下示例可以打包一个可执行的Jar包。
```xml
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.example.MainApp</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
```
配置好的项目可以通过命令`mvn jar:jar`来完成打包。
## maven-shade-plugin
Shade插件是用来建立Uber-JarFatJar的。Uber-Jar将所有的依赖都包含在了Jar包中在运行时可以不受外界运行时环境的影响。Shade插件提供了两个目标但其中只有`shade:shade`是用来打包的。
使用以下最简单的配置就可以打出一个Uber-Jar包。
```xml
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
```
要创建一个可执行的Jar包就需要指定主类主类是在`execution`元素中的`configuration`元素中定义的。
```xml
<execution>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resources.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>com.example.MainApp</Main-Class>
</manifestEntries>
</transformer>
</transformers>
</configuration>
</execution>
```
常用的`Transformer`类都定义在`org.apache.maven.shade.resources`包中,主要有以下这些。
* `AppendingTransformer`,向资源中添加一个内容。
* `IncludeResourcesTransformer`,向项目中添加文件。
* `ManifestResourcesTransformer`,向`MANIFEST.MF`中设定内容。
* `XmlAppendingTransformer`向XML资源中添加一个XML文件。
除此之外,`configuration`元素中还可以使用以下子元素来实现相应的功能。
* `filters/filter/[includes|excludes]`过滤Jar包中的类或者资源。
* `artifactSet/[includes|excludes]`指定需要打包的Jar。
* `minimizedJar`,自动排除不使用的类。
## maven-assembly-plugin
Assembly插件是用来执行定制化打包操作的。Assembly插件提供了两种打包方式第一种是通过`descriptorRefs`元素来指定使用官方统一定制的打包方式,常用的主要有`bin`、`jar-with-dependencies`、`src`等。
官方统一定制的打包方式可以使用以下方式配置。
```xml
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
```
另一种定制打包方式是使用`descriptors`元素来指定打包配置文件,通过配置文件来定制打包操作。这种方式的用法如下例所示。
```xml
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<descriptors>
<descriptor>src/assembly/src.xml</descriptor>
</descriptors>
</configuration>
</plugin>
```
打包配置文件也是一个XML文件以下是其中可以使用的示例格式以及各个元素的含义更详细的配置文件格式需要参考[Assembly插件](https://maven.apache.org/plugins/maven-assembly-plugin/assembly.html)的文档。
```xml
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
<!--配置文件的标识-->
<id></id>
<!--输出的打包格式,可以同时指定多种-->
<!--可选格式有 zip、tar、tar.bz、jar、dir、war 等-->
<formats>
<format></format>
</formats>
<!--定制项目依赖的打包方式-->
<dependencySets>
<dependencySet>
<!--相对于根目录的包依赖目录-->
<outputDirectory></outputDirectory>
</dependencySet>
</dependencySets>
<!--指定一组文件的存放位置-->
<fileSets>
<fileSet>
<outputDirectory></outputDirectory>
<!--设定要包含的文件-->
<includes>
<include></include>
</includes>
<!--设定要排除的文件-->
<excludes>
<exclude></exclude>
</excludes>
<fileMode>0755</fileMode>
</fileSet>
</fileSets>
<!--包含指定文件到指定目录-->
<files>
<file>
<!--相对于根目录的源文件路径-->
<source></source>
<outputDirectory></outputDirectory>
<!--在指定目录中的名称-->
<destName></destName>
<fileMode></fileMode>
</file>
</files>
</assembly>
```
## exec-maven-plugin
Exec插件主要用来支持Maven快速启动Java程序可以在不打包为Jar文件时快捷的启动程序而无需等待漫长的打包时间。Exec插件提供了两个目标可供使用`exec:java`和`exec:exec`。其中`exec:java`可以用来启动比较简单不需要复杂命令行配置的Java应用而`exec:exec`则更适合于启动需要复杂配置的应用。
以下配置了`exec:java`目标来启动Java应用。
```xml
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<verion>1.6.0</verion>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.example.MainApp</mainClass>
<!--命令行参数-->
<arguements>
<arguement>...</arguement>
</arguements>
<!--配置文件参数,相当于使用 -D 参数-->
<systemProperties>
<systemProperty>
<key></key>
<value></value>
</systemProperty>
</systemProperties>
</configuration>
</plugin>
```
使用`exec:exec`目标就更加复杂,但可以更加灵活。
```xml
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<verion>1.6.0</verion>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>java</executable>
<workingDirectory><!--工作目录--></workingDirectory>
<arguements>
<arguement><!--命令行参数--></arguement>
</arguements>
</configuration>
</plugin>
```