我的 pom.xml 中有这些 spring 依赖项:
[...]
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
[...]
但是当我运行 .jar 时,我收到由 org.springframework.core.io.Resource 引起的 ClassNotFoundException 我添加了 spring-core 依赖项,所以我不知道发生了什么
[已解决]将 maven-assemble-plugin 添加到 pom.xml 中,如下所示:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.project.MainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
并使用mvn编译程序集:single
进行编译最佳答案
如果您希望在 jar 文件中链接依赖项,一种选择是确保 classpath设置并将依赖项复制到您需要与打包应用程序捆绑在一起的文件夹。您可以使用 maven-jar-plugin 来做到这一点和 maven-dependency-plugin ,如下面的示例所示:
<build>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.something.AppMainClassName</mainClass>
<classpathPrefix>dependencies</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<includeScope>runtime</includeScope>
<outputDirectory>${project.build.directory}/dependencies/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</build>
这样,所有依赖项都应该存在于项目构建目录内的“dependency”目录中(例如:“target”)
关于java - 运行具有 Spring 依赖项的 Maven 项目时出现 ClassNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48331429/