解决运行maven打的Avro项目jar包,报错第三方库的类找不到

背景

今日运行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 clash 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作业

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值