当用户应用于Spark本身依赖同一个库时可能会发生依赖冲突,导致程序奔溃。依赖冲突表现为在运行中出现NoSuchMethodError或者ClassNotFoundException的异常或者其他与类加载相关的JVM异常。
此时,若能确定classpath中存在这个包,则错误是因为classpath中存在2个不同版本的jar包了,比如常见的log4j,你在classpath中添加了log4j.jar,而spark的lib目录中也有log4j.jar,而且这2个jar包版本不一致的话,就会出现依赖冲突问题。
解决办法有2种:
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>
主要是