【cloudera】【scala】【maven】【maven-shade-plugin】如何实现java和scala混合程序的spark打包,解决jar包依赖问题
2017年5月10日
在【cloudera】【scala】让maven项目同时编译java和scala代码,支持多个源文件,中介绍使用net.alchim31.maven+org.codehaus.mojo实现对src下的java和scala代码文件夹同时编译的方法。但是使用过程中会出现一些问题。主要是:
- 依赖问题
由于spark分布式运算,你的程序jar包依赖要打进jar包内部或者手动为每个节点上传一份依赖。由于维护麻烦,推荐使用前者。
把依赖打进jar包的方法分两种:
A.把依赖的jar都压缩进你程序jar包根目录的lib文件夹下。可以使用maven-dependency-plugin插件单独列出依赖的jar。详情请参考网上教程。
B.直接诶把依赖的源码打进jar包。可以使用maven-assembly-plugin或maven-shade-plugin(推荐)把依赖源码直接打进jar包
- 依赖冲突问题
由于spark程序会用到许多组件,依赖较多,容易发生重复依赖某一个jar包的情况。大多情况下jar包能向下兼容,不会触发依赖冲突问题,但是有时候会出现新版依赖的中取消了旧版接口问题,程序就会报错找不到方法或类。
解决方法就是使用maven-shade-plugin插件,把新旧版本进行包重命名,实现多版本依赖同时引入
具体方法:
综合上面问题,决定采用maven-shade-plugin插件解决依赖打进jar包和依赖冲突的问题。
一下是插件的配置,注意放到pom.xml文件的<build>标签中,关于同时编译java和scala的方式还按照原来的方式。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<!--用插件把org.apache.http映射为org.shaded.apache.http解决兼容问题,org.apache.hadoop.hbase同理--> <plugins> <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>org.apache.http</pattern> <shadedPattern>org.shadedhttp.apache.http</shadedPattern> </relocation> <relocation> <pattern>org.apache.hadoop.hbase.*</pattern> <shadedPattern>org.shadedhbase.apache.hadoop.hbase.*</shadedPattern> </relocation> </relocations> <filters> <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> |
分类: 技术,让世界更美好。