Java 运行带有Spring依赖项的Maven项目时发生ClassNotFoundException
我的pom.xml中有以下spring依赖项:Java 运行带有Spring依赖项的Maven项目时发生ClassNotFoundException,java,spring,maven,spring-mvc,dependency-injection,Java,Spring,Maven,Spring Mvc,Dependency Injection,我的pom.xml中有以下spring依赖项: [...] <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.0.2.RELEASE</version> </dependency> <dependency> <grou
[...]
<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>
[...]
[…]
org.springframework
弹簧芯
5.0.2.发布
org.springframework
春豆
5.0.2.发布
org.springframework
spring上下文
5.0.2.发布
[...]
但是当我运行.jar时,我得到一个由org.springframework.core.io.Resource引起的ClassNotFoundException
我添加了spring核心依赖项,所以我不知道发生了什么
[SOLVED]将maven assembly插件添加到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>
maven汇编插件
com.project.MainClass
带有依赖项的jar
并使用mvn编译程序集编译:single如果您希望依赖项链接到jar文件中,一个选项是确保已设置并将依赖项复制到需要与打包应用程序捆绑的文件夹中。您可以通过使用和来实现这一点,如下面的示例所示:
<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>
org.apache.maven.plugins
maven jar插件
3.0.2
真的
com.something.AppMainClassName
依赖关系
org.apache.maven.plugins
maven依赖插件
3.0.2
复制依赖项
包裹
复制依赖项
运行时
${project.build.directory}/dependencies/
通过这种方式,所有依赖项都应该存在于项目构建目录(例如:“target”)中的“dependency”目录中。您将应用程序打包为.jar,但是这些依赖项是否存在于包含依赖项的某个文件夹中?(例如使用maven依赖插件)。默认情况下,依赖项不打包在jar中file@Azanx它们不是,所以我必须添加那个插件,下一步是什么?它是如何工作的?谢谢你的评论,我是使用maven和所有这些东西的新手。你需要使用maven Shade插件。最后,我使用了maven assembly插件并可以工作,但您的解决方案也是正确的。