为什么使用 maven 阴影插件重新定位不起作用



我在运行包含比Hadoop发行版(CDH 5.2(中更新版本的Guava的Hadoop作业时遇到了一些问题。这是一个已知问题。我尝试通过使用 Maven 阴影插件对库进行着色来解决这个问题。因此,我在pom.xml中添加了以下行:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.3</version>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
        <configuration>
          <relocations>
            <relocation>
              <pattern>com.google</pattern>
              <shadedPattern>thirdparty.com.google</shadedPattern>
            </relocation>
          </relocations>
        </configuration>
      </execution>
    </executions>
  </plugin>

不幸的是,阴影似乎不起作用。当我提取超级 JAR 时,没有文件夹thirdparty/com/google但文件夹仍然com/google

有人知道出了什么问题吗?

这对我有用:

<relocations>
   <relocation>
     <pattern>com.google.</pattern>
     <shadedPattern>thirdparty.com.google.</shadedPattern>
   </relocation>
 </relocations>

请注意模式末尾的点。

您可能需要在<configuration>部分下指定显式 artifactSet::include:

    <configuration>
        <artifactSet>
            <includes>
                <include>com.google.guava:*</include>
                ...
            </includes>
        </artifactSet>
        <relocations>
        ...

似乎您需要在重定位规则中引用包名称,而不是 maven groupId,我在我的库中使用 rx v1 并且不想用它污染用户的命名空间,下面的 pom.xml 部分重写了字节码,因此最终的 uberjar 将具有 rx,但重命名为 (shaded.rx(。

阴影搬迁文档

<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>rx</pattern>
              <shadedPattern>shaded.rx</shadedPattern>
            </relocation>
          </relocations>
        </configuration>
      </execution>
    </executions>
  </plugin>

最新更新