本文最后更新于:2023-05-28T15:42:14+08:00
                  
                  
                
              
            
            
              
                
                Maven Shade Plugin
简介
maven-shade-plugin是一个maven打包插件,主要用于辅助完成maven项目打包,同时还提供了许多增强功能,包括依赖添加与排除、依赖重命名等。
maven-shade-plugin打包生成的jar,其中包含了项目所需要的全部依赖,所以jar包的文件大小相对来说会更大。此时生成的jar包往往是为了单独运行,而不是为了作为其他项目的依赖。
使用方式
jar打包
使用该插件进行打包,只需要在pom.xml文件中指定相关插件与版本,并增加execution选项,为其绑定在pacakge阶段:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 
 | <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>
 </execution>
 </executions>
 </plugin>
 
 | 
可运行jar包
如果要生成一个可指定jar包,那么还需要指定main方法的入口,可以在configuration标签中指定对应的mainClass:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 
 | <plugin><groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-shade-plugin</artifactId>
 <version>3.4.1</version>
 <configuration>
 <transformers>
 <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
 <mainClass>{ mainClass 全路径}</mainClass>
 </transformer>
 </transformers>
 </configuration>
 <executions>
 <execution>
 <phase>package</phase>
 <goals>
 <goal>shade</goal>
 </goals>
 </execution>
 </executions>
 </plugin>
 
 | 
依赖添加与排除
在configuration标签中可以增加artifactSet标签,其中可以配置需要添加的第三方依赖或者排除不需要打包的依赖。
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 
 | <configuration><artifactSet>
 <includes>
 <include>*:*</include>
 </includes>
 <excludes>
 <exclude>org.projectlombok:lombok</exclude>
 </excludes>
 </artifactSet>
 ...
 </configuration>
 
 | 
也可以通过配置filter来决定在第三方依赖中哪些文件进行打包,哪些文件不进行打包。
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 
 | <configuration><filters>
 <filter>
 <artifact>javax.servlet.jsp:jsp-api</artifact>
 <includes>
 <include>javax/servlet/jsp/**</include>
 </includes>
 </filter>
 </filters>
 ...
 </configuration>
 
 | 
依赖重命名
有时候我们的jar包在某些环境或者平台上运行,而jar包与平台之间存在依赖冲突,此时就需要利用到依赖重命名。
或者可以考虑一个更加形象的例子。现在有一个A项目,它依赖于B项目的1.0版本。然后我们需要开发一个C项目,C项目既需要引入A项目的依赖,又需要引入B项目的2.0版本。问题在于B项目的1.0版本与2.0版本之间是不兼容的,例如有API修改。那么对于C项目来说就会存在冲突,即C项目到底需要使用B:1.0还是B:2.0。使用B:1.0,那么直接依赖B:2.0的C项目无法工作;如果使用B:2.0,那么直接依赖B:1.0的A项目无法工作,从而依赖于A项目的C项目依旧无法工作。
利用maven-shade-plugin提供的依赖重命名功能,我们就可以解决上面的问题,达到隔离第三方包、解决包冲突等效果。例如在上面的例子中,就可以将C项目依赖的B:2.0进行重命名,使之与B:1.0不再冲突。
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 
 | <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>
 <relocations>
 <relocation>
 <pattern>com.B</pattern>
 <shadedPattern>com.shaded.B</shadedPattern>
 </relocation>
 </relocations>
 </configuration>
 </execution>
 </executions>
 </plugin>
 
 
 
 | 
这里的relation就是完成重命名的工作。其中pattern表示匹配模式,shadedPattern表示需要重命名成什么名称。
参考文章
- maven-shade-plugin
介绍
- What
is the maven-shade-plugin used for - StackOverflow
- Apache Maven
Shade Plugin