maven-shade-plugin 入门指南

有时候,需要将复杂的项目, 包括各种xml文件属性文件, 类, jar等等打包到一个可以执行的jar包中, 然后用java -jar  xxx.jar 来运行项目, 这样简单方便, 特别是在编写一些测试工具时,尤为重要。

但是经常发现打包后的项目无法启动, 其中一大类是您项目有问题, 但是这个相对好解决, 毕竟自己的项目可以在windows下的ide中做各种调试,测试都测试好了, 在打包一般程序问题的概率就低得多了。

1. Why?

通过 maven-shade-plugin 生成一个 uber-jar,它包含所有的依赖 jar 包。

2. Goals

GoalDescription
shade:helpDisplay help information on maven-shade-plugin.Callmvn shade:help -Ddetail=true -Dgoal=<goal-name>to display parameter details.
shade:shadeMojo that performs shading delegating to the Shader component.

3. Usage

  • 配置 maven-shade-plugin

maven-shade-plugin 将 goal shade:shade 绑定到 phase package 上。

 <build>
     <plugins>
         <plugin>
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-shade-plugin</artifactId>
             <version>2.4.3</version>
             <configuration>
                <!-- put your configurations here -->
             </configuration>
             <executions>
                 <execution>
                     <phase>package</phase>
                     <goals>
                        <goal>shade</goal>
                     </goals>
                 </execution>
             </executions>
         </plugin>
     </plugins>
 </build>
  • 执行命令
mvn clean package

会在 target 文件生成一个 uber-jar,以 -shaded.jar 为后缀的 jar 包。

4. Examples

  • Selecting Contents for Uber JAR

将该工程依赖的部分 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>
              <artifactSet>
                <excludes>
                  <exclude>classworlds:classworlds</exclude>
                  <exclude>junit:junit</exclude>
                  <exclude>jmock:*</exclude>
                  <exclude>*:xml-apis</exclude>
                  <exclude>org.apache.maven:lib:tests</exclude>
                  <exclude>log4j:log4j:jar:</exclude>
                </excludes>
              </artifactSet>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
</build>

将依赖的某个 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/*.DSA</exclude>
                    <exclude>META-INF/*.RSA</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>
  • Attaching the Shaded Artifact

默认会生成一个Jar包和一个以 “-shaded”为结尾的uber-jar包,可以通过配置来指定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>
              <shadedArtifactAttached>true</shadedArtifactAttached>
              <shadedClassifierName>jackofall</shadedClassifierName> <!-- Any name that makes sense -->
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  • Executable JAR

通过设置 MainClass 创建一个可执行 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>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <mainClass>org.sonatype.haven.HavenCli</mainClass>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  • Relocating Classes

Java 工程经常会遇到第三方 Jar 包冲突,使用 maven shade plugin 解决 jar 或类的多版本冲突。 maven-shade-plugin 在打包时,可以将项目中依赖的 jar 包中的一些类文件打包到项目构建生成的 jar 包中,在打包的时候把类重命名。下面的配置将 org.codehaus.plexus.util jar 包重命名为 org.shaded.plexus.util。

  <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>
              <relocations>
                <relocation>
                  <pattern>org.codehaus.plexus.util</pattern>
                  <shadedPattern>org.shaded.plexus.util</shadedPattern>
                  <excludes>
                    <exclude>org.codehaus.plexus.util.xml.Xpp3Dom</exclude>
                    <exclude>org.codehaus.plexus.util.xml.pull.*</exclude>
                  </excludes>
                </relocation>
              </relocations>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

-----

下面的部分介绍的更为细致些, 特别是, jar包和类的剔除等

网上有一些maven-shade-plugin替代maven-assembly-plugin的文章,原因是代maven-assembly-plugin打出的jar包中要么是不能设置Main-Class,要么spring的META-INF/spring.*文件相互覆盖了。对于这两个问题,maven-assembly-plugin在当前的版本(3.1.0)中都可以解决了(方法见https://my.oschina.net/u/2377110/blog/1584205)。

实际上这两个插件所针对的用途其实是有差异的,而它们与maven默认的maven-jar-plugin都是打包插件,简单的区别如下:

|plugin|function| | — | — | |maven-jar-plugin|maven 默认打包插件,用来创建 project jar| |maven-shade-plugin|用来打可执行包,包含依赖,以及对依赖进行取舍过滤| |maven-assembly-plugin|支持定制化打包方式,更多是对项目目录的重新组装|

当你只想将项目打成一个可执行包时,maven-shade-plugin非常适合。一般情况下,pom文件中shade插件配置如下。

<build>  
    <plugins>  
        <plugin>  
            <groupId>org.apache.maven.plugins</groupId>  
            <artifactId>maven-shade-plugin</artifactId>  
            <version>1.4</version>  
            <executions>  
                <execution>  
                    <phase>package</phase>  
                    <goals>  
                        <goal>shade</goal>  
                    </goals>  
                    <configuration>  
                        <filters>  
                            <filter>  
                                <artifact>*:*</artifact>  
                                <excludes>  
                                    <exclude>META-INF/*.SF</exclude>  
                                    <exclude>META-INF/*.DSA</exclude>  
                                    <exclude>META-INF/*.RSA</exclude>  
                                </excludes>  
                            </filter>  
                        </filters>  
                        <transformers>  
                            <transformer  
                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">  
                                <mainClass>com.lcifn.Application</mainClass>  
                            </transformer>  
                            <transformer  
                                implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">  
                                <resource>META-INF/spring.handlers</resource>  
                            </transformer>  
                            <transformer  
                                implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">  
                                <resource>META-INF/spring.schemas</resource>  
                            </transformer>  
                        </transformers>  
                    </configuration>  
                </execution>  
            </executions>  
        </plugin>  
    </plugins>  
</build>  

shade插件绑定的是package生命周期目标,并设置com.lcifn.Application为Main-Class,以及将META-INF/spring.*文件合并(追加而非覆盖),并过滤掉所有依赖的META/INF中SF,DSA,RSA后缀文件。这里涉及到filter配置和transformer配置。

filters和artifactSet

Filter操作在打包时将jar包中的内容排除。它是以groupId:artifactId为标识,在filter内部可以使用<include>/<exclude>更细致地控制,既可以移除代码文件,也可以移除配置文件。

<!-- 按package过滤junit包 -->
<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>
    </filters>
</configuration>

如果想将整个jar包都过滤掉,可以使用<artifactSet>,也是指定groupId:artifactId的标识。

<configuration>
	<artifactSet>
		<excludes>
			<exclude>classworlds:classworlds</exclude>
			<exclude>junit:junit</exclude>
			<exclude>jmock:*</exclude>
			<exclude>*:xml-apis</exclude>
			<exclude>org.apache.maven:lib:tests</exclude>
			<exclude>log4j:log4j:jar:</exclude>
		</excludes>
	</artifactSet>
</configuration>

另外配置<minimizeJar>将项目中没有使用的依赖自动移除

<configuration>
    <minimizeJar>true</minimizeJar>
</configuration>

<minimizeJar>可以和<filters>共同使用

<configuration>
	<minimizeJar>true</minimizeJar>
	<filters>
		<filter>
			<artifact>log4j:log4j</artifact>
			<includes>
				<include>**</include>
			</includes>
		</filter>
		<filter>
			<artifact>commons-logging:commons-logging</artifact>
			<includes>
				<include>**</include>
			</includes>
		</filter>
	</filters>
</configuration>

资源转换

在打包时,存在将多个构件中的class文件或资源文件聚合的需求。shade插件提供了丰富的Transformer工具类。这里介绍一些常用的Transformer。

ManifestResourceTransformer

往MANIFEST文件中写入Main-Class是可执行包的必要条件。ManifestResourceTransformer可以轻松实现。

<configuration>
	<transformers>
		<transformer
			implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
			<mainClass>com.lcifn.Application</mainClass>
		</transformer>
	</transformers>
</configuration>

AppendingTransformer

用来处理多个jar包中存在重名的配置文件的合并,尤其是spring。

<configuration>
	<transformers>
		<transformer
			implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
			<resource>META-INF/spring.handlers</resource>
		</transformer>
		<transformer
			implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
			<resource>META-INF/spring.schemas</resource>
		</transformer>
	</transformers>
</configuration>

ServicesResourceTransformer

JDK的服务发现机制是基于META-INF/services/目录的,如果同一接口存在多个实现需要合并 ,则可以使用此Transformer。

<configuration>
  <transformers>
    <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
  </transformers>
</configuration>

更多的Transformer见http://maven.apache.org/plugins/maven-shade-plugin/examples/resource-transformers.html

原始构件与shade构件

默认情况下,shade插件会覆盖基于项目的jar包,而生成包含所有依赖的jar包。但有时需要原始的jar包和shade后的jar包同时被部署,可以配置如下。

<configuration>
  <shadedArtifactAttached>true</shadedArtifactAttached>
  <!-- 名称会作为后缀在shade构件jar包后 -->
  <shadedClassifierName>jackofall</shadedClassifierName> 
</configuration>
### 回答1: Maven Shade Plugin 是一个 Maven 构建工具的插件,可以将项目依赖的所有 jar 包合并成一个可执行的 jar 包,以便于在分发和部署时使用。这个插件的常见用法包括: 1. 打包可执行的 jar:将项目依赖的所有 jar 包和项目本身的代码打包成一个可执行的 jar 包,方便在生产环境中部署和执行。 2. 打包成一个不可执行的 jar:将项目依赖的所有 jar 包和项目本身的代码打包成一个不可执行的 jar 包,方便在其他项目中引用和使用。 3. 排除某些依赖:有些依赖可能会引起冲突或者不必要的依赖,可以使用 Maven Shade Plugin 的 exclude 配置项排除这些依赖。 4. 重新定位依赖:有些依赖可能会引用了其他库中的类或资源,导致在合并 jar 包时出现冲突,可以使用 Maven Shade Plugin 的 relocation 配置项将这些依赖的类或资源重新定位到新的包名或路径下。 总之,Maven Shade Plugin 可以让我们更方便地打包和分发项目,并且解决一些依赖冲突的问题,是一个非常实用的 Maven 插件。 ### 回答2: maven-shade-plugin是一个用于Maven项目构建的插件,主要用于将Java项目打包成可执行的jar包,解决了传统的jar包依赖冲突和包名冲突的问题。它的常见用法如下: 1. 打包可执行的jar包:通过配置maven-shade-plugin,在项目构建时会将所有依赖的jar包和项目代码打包到一个可执行的jar包中。这样,用户只需要执行生成的jar包即可运行整个项目。 2. 解决传统的jar包依赖冲突:在Java项目中,使用不同的依赖库时可能会引发依赖冲突。maven-shade-plugin可以帮助解决这个问题,他会将项目所依赖的库合并到一个jar包中,避免了传统方式下的依赖冲突。 3. 解决包名冲突:当我们引入了多个依赖库时,这些库中可能存在相同的类或包名,会导致编译错误或运行时的冲突。maven-shade-plugin可以通过重命名包名来解决这个问题,确保在打包后不会出现冲突。 4. 改变jar包中的资源路径:通过配置maven-shade-plugin,可以将项目中的一些资源文件移动到指定的位置,改变其相对路径。 5. 指定启动类:maven-shade-plugin可以通过配置指定启动类,当执行生成的可执行jar包时,会直接调用该类的main方法。 总结来说,maven-shade-plugin是一个非常实用的插件,能够解决传统的jar包依赖冲突和包名冲突问题,同时也提供了一些其他功能,使得项目的构建更加简洁和方便。 ### 回答3: maven-shade-pluginMaven生态系统中的一个插件,主要用于解决Java项目中的依赖冲突问题和打包问题。 常见的maven-shade-plugin用法有以下几种: 1. 打包包含所有依赖的可执行JAR文件:通过配置maven-shade-plugin插件,在打包时将项目的所有依赖打包到一个JAR文件中,并且可以配置主类,使得该JAR文件可以直接执行。 2. 重命名依赖冲突的类:当项目中存在不同版本的依赖库,并且这些依赖库中的某些类具有相同的包和类名时,maven-shade-plugin可以通过重命名类的方式来解决冲突。可以通过配置该插件来指定需要重命名的类。 3. 过滤不需要的依赖:有时候项目的依赖中可能会包含一些不需要的、或者与项目功能无关的依赖库,可以使用maven-shade-plugin来过滤这些不需要的依赖,减小项目的包大小。 4. 拷贝资源文件到生成的JAR包中:除了将Java类文件打包到JAR包中,还可以通过maven-shade-plugin来将项目中的资源文件(如配置文件、属性文件等)一同打包到生成的JAR包中。 5. 混淆代码:maven-shade-plugin还可以对项目的代码进行混淆,使得代码难以被反编译,提高代码的安全性。 通过以上常见的用法,maven-shade-plugin可以解决Java项目中的依赖冲突问题和打包问题,让项目能够更好地进行构建和部署。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值