1. Why?
通过 maven-shade-plugin 生成一个 uber-jar,它包含所有的依赖 jar 包。
2. Goals
| Goal | Description |
|---|---|
| shade:help | Display help information on maven-shade-plugin.Callmvn shade:help -Ddetail=true -Dgoal= |
| shade:shade | Mojo that performs shading delegating to the Shader component. |
3. Usage
- 配置 maven-shade-plugin
maven-shade-plugin 将 goal shade:shade 绑定到 phase package 上。
1 | <build> |
- 执行命令
1 | mvn clean package |
会在 target 文件生成一个 uber-jar,以 -shaded.jar 为后缀的 jar 包。
4. Examples
- Selecting Contents for Uber JAR
将该工程依赖的部分 Jar 包 include/exclude 掉。
1 | <build> |
将依赖的某个 Jar 包内部的类或者资源 include/exclude 掉。
1 | <build> |
maven-shade-plugin 自动将所有不使用的类全部排除掉,将 uber-jar 最小化。
1 | <build> |
- Attaching the Shaded Artifact
默认会生成一个Jar包和一个以 "-shaded"为结尾的uber-jar包,可以通过配置来指定uber-jar的后缀名。
1 | <build> |
- Executable JAR
通过设置 MainClass 创建一个可执行 Jar 包。
1 | <build> |
- Relocating Classes
Java 工程经常会遇到第三方 Jar 包冲突,使用 maven shade plugin 解决 jar 或类的多版本冲突。 maven-shade-plugin 在打包时,可以将项目中依赖的 jar 包中的一些类文件打包到项目构建生成的 jar 包中,在打包的时候把类重命名。下面的配置将 org.codehaus.plexus.util jar 包重命名为 org.shaded.plexus.util。
1 | <build> |
参考
maven 入门指南 maven 生命周期 Maven 默认插件以及功能 maven 依赖管理 maven-shade-plugin 入门指南 maven-assembly-plugin 入门指南