常用第三方插件应用
Maven 提供丰富的第三方插件扩展构建能力。
shade 插件
打包 Fat-JAR
XML
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.example.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
效果
将所有依赖打包到单个 JAR(Fat-JAR/Uber-JAR)。
重定位类
XML
<configuration>
<relocations>
<relocation>
<pattern>org.springframework</pattern>
<shadedPattern>shaded.org.springframework</shadedPattern>
</relocation>
</relocations>
</configuration>
过滤依赖
XML
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
assembly 插件
自定义打包
XML
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptorRefs>
<descriptorRef>bin</descriptorRef> <!-- 二进制包 -->
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
自定义描述符
XML
<configuration>
<descriptors>
<descriptor>src/assembly/dist.xml</descriptor>
</descriptors>
</configuration>
dist.xml 示例
XML
<assembly>
<id>dist</id>
<formats>
<format>tar.gz</format>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>target</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
<fileSet>
<directory>bin</directory>
<outputDirectory>/bin</outputDirectory>
</fileSet>
</fileSets>
</assembly>
source 插件
打包源码
XML
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
产物
XML
my-lib-1.0.0-sources.jar
my-lib-1.0.0-test-sources.jar
javadoc 插件
打包文档
XML
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.4.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
<configuration>
<encoding>UTF-8</encoding>
<charset>UTF-8</charset>
<docencoding>UTF-8</docencoding>
</configuration>
</plugin>
exec 插件
执行外部程序
XML
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>npm</executable>
<arguments>
<argument>install</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
dependency 插件
复制依赖
XML
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<includeScope>runtime</includeScope>
</configuration>
</execution>
</executions>
</plugin>
war 插件
WAR 打包配置
XML
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.2</version>
<configuration>
<warSourceDirectory>src/main/webapp</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
<webResources>
<resource>
<directory>src/main/webapp</directory>
<filtering>true</filtering>
</resource>
</webResources>
</configuration>
</plugin>
release 插件
自动化发布
Bash
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
<configuration>
<tagNameFormat>v@{project.version}</tagNameFormat>
<autoVersionSubmodules>true</autoVersionSubmodules>
</configuration>
</plugin>
执行发布
XML
mvn release:prepare
mvn release:perform
flyway 插件
数据库迁移
Bash
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>8.5.0</version>
<configuration>
<url>jdbc:mysql://localhost:3306/mydb</url>
<user>root</user>
<password>password</password>
</configuration>
</plugin>
执行迁移
XML
mvn flyway:migrate
spring-boot 插件
Spring Boot 打包
Bash
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
运行应用
text
mvn spring-boot:run
常用插件汇总
| 插件 | 功能 |
|---|---|
| shade | 打包 Fat-JAR |
| assembly | 自定义打包格式 |
| source | 打包源码 |
| javadoc | 打包文档 |
| exec | 执行外部程序 |
| dependency | 依赖操作 |
| release | 自动化发布 |
| flyway | 数据库迁移 |
| spring-boot | Spring Boot 支持 |
要点总结
- shade 打包 Fat-JAR,包含所有依赖
- assembly 自定义打包格式(tar.gz/zip)
- source 打包源码包
- javadoc 打包文档包
- exec 执行外部程序或 Java 类
- dependency:copy-dependencies 复制依赖
- release 自动化版本发布流程
📝 发现内容有误?点击此处直接编辑