解决运行maven打的Avro项目jar包,报错第三方库的类找不到
程序员文章站
2022-06-05 19:38:57
...
背景
今日运行Avro项目时,碰到一个问题,用Maven把自己的项目打成jar包,然后运行之,报错找不到某个第三方库的类
解决方法
在pom.xml中,加入maven-assembly-plugin,代码如下
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>job</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>job.xml</descriptor>
</descriptors>
<finalName>szc_avro_test</finalName>
<outputDirectory>${project.build.directory}/../..</outputDirectory>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</execution>
</executions>
</plugin>
其中,<finalName>是生成的jar包的名字,<outputDirectory>是输出路径,而任务配置描述信息则保存在<descriptor>标签下的job.xml文件里,这个文件内容如下
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/2.2.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/2.2.1">
<id>job</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<useTransitiveFiltering>true</useTransitiveFiltering>
<unpack>true</unpack>
<unpackOptions>
<excludes>
<exclude>META-INF/LICENSE</exclude> <!-- Exclude to avoid * with directory of the same name -->
</excludes>
</unpackOptions>
<scope>runtime</scope>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>false</useProjectArtifact>
<excludes>
<exclude>org.apache.hadoop:*</exclude>
</excludes>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>target/classes</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
</assembly>
然后再打包运行,就正常了。输出的jar包在${project.build.directory}/../..路径下,比如项目的build目录是D:\develop\ideaWorkspace\szc_hadoop_reduce\out,那么输出目录就是D:\develop\ideaWorkspace。
而项目的build目录,可以在idea的ProjectStructure中查看
关于如何利用maven给项目打包,请参见文章命令行运行Hadoop作业