在Maven中添加外部JAR导致NoClassDefFoundError

时间:2018-04-25 12:38:36

标签: java maven jar classpath pom.xml

发现了一些有关此问题的其他问题,但似乎没有一个问题可以解决我的问题。

我有一个外部JAR需要包含在我生成的jar中。我目前正在这样做:

<dependency>
    <groupId>be.april.meter</groupId>
    <artifactId>JRPiCam</artifactId>
    <version>1.1.1</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/src/main/resources/jrpicam-1.1.1.jar</systemPath>
</dependency>

当在IDE中运行我的主类时,一切正常,因为它应该是。然而,当尝试运行Maven生成JAR时,我收到NoClassDefFoundError错误。我已经尝试过生成一个“胖子”和“阴影”。罐子和所有的东西,但没有一个帮助。

我还尝试使用Maven Install Plugin(https://eureka.ykyuen.info/2014/06/10/maven-include-system-scope-dependency-in-maven-assembly-plugin/)安装jar,但这会产生与我当前解决方案完全相同的错误。

可在此处找到完整的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>be.april.meter</groupId>
    <artifactId>April-Meter</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <!-- Build an executable JAR -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>be.april.meter.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.10.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.10.0</version>
        </dependency>
        <dependency>
            <groupId>be.april.meter</groupId>
            <artifactId>JRPiCam</artifactId>
            <version>1.1.1</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/resources/jrpicam-1.1.1.jar</systemPath>
        </dependency>
    </dependencies>

提前致谢。

- 编辑:似乎log4j也不会加载。我通过添加:

来修复此问题
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.5.1</version>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <includeScope>compile</includeScope>
                <outputDirectory>${project.build.directory}/lib/</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

这会导致JRPiCam jar与log4j depdendencies一起位于/ lib输出目录中,但JRPiCam仍然无法正常工作。

2 个答案:

答案 0 :(得分:1)

您可以尝试使用maven程序集插件。我刚刚验证过,它也适用于外部依赖项。运行时会创建一个可执行jar。

  

mvn clean install

 <modelVersion>4.0.0</modelVersion>
<groupId>com.spring</groupId>
<artifactId>maven-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
    <java.version>10</java.version>
</properties>


<dependencies>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>1.9.5</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.10.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.10.0</version>
    </dependency>
</dependencies>
<build>
<plugins>
<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <mainClass>com.test.MyMain</mainClass>
            </manifest>
        </archive>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>


    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>


</plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.6.1</version>
        <configuration>
            <source>10</source>
            <target>10</target>
        </configuration>
    </plugin>
</plugins>
</build>

答案 1 :(得分:0)

我通过将jar上传到私有Nexus Repo来修复此问题。真正解决的其他解决方案&#39;主要问题随时欢迎..