问题
I am having some trouble running a Hadoop job that includes a newer version of Guava than the one that is included in the Hadoop distribution (CDH 5.2). This is a known problem. I try to solve it by shading the libraries using the Maven shade plugin. Therefore, I added the following lines to my 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>
Unfortunately, the shading seems not to work. When I extract the uber-JAR there is no folder thirdparty/com/google
but still the folder com/google
.
Does someone have an idea what is going wrong?
回答1:
This worked for me:
<relocations>
<relocation>
<pattern>com.google.</pattern>
<shadedPattern>thirdparty.com.google.</shadedPattern>
</relocation>
</relocations>
note the dot at the end of the pattern.
回答2:
You may need to specify explicit artifactSet::includes under your <configuration>
section:
<configuration>
<artifactSet>
<includes>
<include>com.google.guava:*</include>
...
</includes>
</artifactSet>
<relocations>
...
回答3:
Seems like you need to refer to the package name in the relocation rule, not the maven groupId, I'm using rx v1 in my lib and don't want to pollute the user's namespace with it, the pom.xml section below rewrites the bytecode, so the final uberjar will have rx, but renamed (shaded.rx).
shade relocation doc
<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>
来源:https://stackoverflow.com/questions/27535909/why-does-relocation-with-the-maven-shade-plugin-not-work