当你使用 Maven 对项目打包时,你需要了解以下 3 个打包 plugin,它们分别是:
plugin | function |
---|---|
maven-jar-plugin | maven 默认打包插件,用来创建 project jar |
maven-shade-plugin | 用来打可执行包,executable(fat) jar |
maven-assembly-plugin | 支持定制化打包方式,例如 apache 项目的打包方式 |
将依赖的某个 Jar 包内部的类或者资源 include/exclude 掉。
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>junit:junit</artifact>
<includes>
<include>junit/framework/**</include>
<include>org/junit/**</include>
</includes>
<excludes>
<exclude>org/junit/experimental/**</exclude>
<exclude>org/junit/runners/**</exclude>
</excludes>
</filter>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/LICENSE</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/NOTICE</exclude>
<exclude>META-INF/services/*</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
maven-shade-plugin 自动将所有不使用的类全部排除掉,将 uber-jar 最小化
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<minimizeJar>true</minimizeJar>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>