【问题标题】:How fix external dependency missing jar with maven?如何用maven修复外部依赖缺失的jar?
【发布时间】:2020-12-04 06:48:36
【问题描述】:

我是 maven 的菜鸟,我正在尝试将一个外部 jar 附加到我的项目中。 所以我的 ${project.basedir}\src\lib 文件夹中有一个 x.jar 依赖项,这是我的项目所必需的。

当我运行mvn clean install 时,它成功完成。 当我运行java -jar project-1.0-SNAPSHOT.jar 时,我得到了错误 线程“主”java.lang.NoClassDefFoundError 中的异常:

我的 pom 文件是:

<?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>org.example</groupId>
    <artifactId>project</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>x.Main</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>x</groupId>
            <artifactId>x</artifactId>
            <version>RELEASE</version>
            <systemPath>${project.basedir}/src/lib/x.jar</systemPath>
            <scope>system</scope>
        </dependency>
    </dependencies>


</project>

我应该如何运行 maven 或编辑我的 pom 来解决这个问题?

【问题讨论】:

    标签: maven build maven-2


    【解决方案1】:

    标准 jar 不包含依赖项,因此除非您指定完整的类路径,否则无法在命令行上运行。

    所以要么构建一个jar-with-dependencies(maven 程序集插件或 maven shade 插件),要么明确列出依赖关系。

    【讨论】:

    • 我尝试添加 maven 程序集插件,但仍然无法正常工作。也许我应该更改依赖项中的其他任何内容?注意:我也尝试过使用&lt;systemPath&gt; 上的完整路径,但还是不行。我正在运行mvn clean install,然后运行 ​​java -jar 来执行 jar,但仍然找不到类。有什么建议吗?
    • 我不确定是否默认添加了systemPath依赖项。 SystemPath 已弃用。
    猜你喜欢
    • 1970-01-01
    • 2016-07-04
    • 1970-01-01
    • 1970-01-01
    • 2015-05-12
    • 1970-01-01
    • 2018-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多