Apache maven shade plugin使用说明

在解决java jar包冲突的时候须要用到shade插件,好比flink 已经自带了protobuf的包,可是咱们想使用另外一个版本的protobuf 包,这时候咱们就可使用shade插件打包,不然会报版本相关的错误。java

introduction

Apache maven shade plugin提供把工程的artifact及其依赖打包到一个uber-jar中并能隐藏起来(好比重命名),shade插件仅仅有一个功能就是建立一个shaded包。 
那什么是uber-jar呢,uber在德语中是above或over的意思,在这里表示是从单一的jar提高到“over-jar”,即把全部的依赖都定义到一个jar文件里。 
好了,如今咱们知道shade插件的基本做用了,如今从官网给出的几个例子看看实际的应用。apache

为uber-jar选择内容

这是官网直译的标题,用咱们容易理解的就是经过shade插件咱们能够为生成的那个jar包选择包含哪些依赖以及排除哪些依赖。 
1. 支持两种操做include和exclude 
2. 配置格式:groupId:artifactId[[:type]:classifier],至少包含groupid和artifactid,type和类名可选 
3. 支持’*’ 和 ‘?’执行通配符匹配 
好比,一个示例以下:
 api

project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.0.0</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>
  ...
</project>

类重定位

官网是“Relocating Classes”,若是一个uber-jar会被其余项目引用,uber-jar中依赖的类可能会致使类定位冲突(因为不一样版本的jar包引发),咱们能够经过shade插件来将被隐藏的类重定位以使该类只在该uber-jar中使用,这种方式也常常被用来解决jar包冲突问题。 
示例以下:
 maven

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.0.0</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>
  ...
</project>

这指示插件经过移动相应的JAR文件条目并重写受影响的字节码( JAR file entries and rewritting the affected bytecode),将org.codehaus.plexus.util包以及子包中的类从组件org.shaded.plexus.util中移到org.shaded.plexus.util包中。Xpp3Dom类和pull其余类将保留在原始包中。ui

Shaded Artifact附加名字

默认状况下,当执行installed/deployed时候,会默认生成两个jar包,一个以-shaded结尾,这个咱们能够配置更改,示例以下:spa

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.0.0</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <shadedArtifactAttached>true</shadedArtifactAttached>
              <shadedClassifierName>myName</shadedClassifierName> <!-- Any name that makes sense -->
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  ...
</project>

则会生成以-myName结尾的jar包。插件

可执行jar包

要建立一个可执行uber-jar包,也能够将入口添加进来,示例以下:code

<project>
   <groupId>shade.test</groupId>
    <artifactId>shade.test</artifactId>
    <version>1.0-SNAPSHOT</version>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.0.0</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <mainClass>Main</mainClass>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  ...
</project>
相关文章
相关标签/搜索