【发布时间】:2018-10-04 13:54:52
【问题描述】:
当我mvn clean package 项目并使用java -jar target/project.jar 运行它时,它会抛出一个错误,它无法从外部 jar 中找到某些类。
采取的步骤
所有外部 jar 都在我项目的文件夹中:
/jars或/jars/morejars/将 jars 添加到构建路径:在 Eclipse 中,我右键单击
project,转到build path并选择add external archives。我可以看到 Eclipse 在“Maven 依赖项”文件夹下创建了一个库文件夹“引用的库”。当我检查project->Properties->Java Build Path->Libraries时,我可以看到所有导入的罐子。
其中一些外部 jar 在pom.xml 中被描述为<dependency>(带有<systemPath),因此它们不会出现在Referenced Libraries 中,而是出现在Maven dependencies 中(有趣的是,当运行我打包的项目是在一个外部 jar 中,它不在 Referenced Libraries 中,而是在 Maven dependencies)
例如
<dependency>
<groupId>external.jar.groupid</groupId>
<artifactId>external.jar.artifactid</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}/jars/external-jar.jar</systemPath>
</dependency>
-
使用 maven 程序集插件(如 here 所述),这是我的完整
<build>配置:<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <artifactId>maven-assembly-plugin</artifactId> <executions> <execution> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin> </plugins> </build> 运行
mvn clean package->BUILD SUCCESSjava -jar target/project.jar-> 无法从external.jar找到some.class
感谢您的帮助
【问题讨论】:
-
您使用
system作用域依赖项是否有特定原因? “..它们通常用于告诉 Maven JDK 或 VM 提供的依赖项..” -
我还猜想系统范围可能是根本原因,因为它很有用,例如。当您有一些共享库时,例如。在应用程序服务器 libs 文件夹等中。但不适用于独立应用程序 - 检查 maven doc
Dependencies with the scope system are always available and are not looked up in repository. They are usually used to tell Maven about dependencies which are provided by the JDK or the VM.所以在包目标期间,它们不会被下载/添加到您的包中