java - 运行具有 Spring 依赖项的 Maven 项目时出现 ClassNotFoundException

标签 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>
  <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/

相关文章:

java - 如何从 bean 验证注释中获取 payload 属性

java - 如何创建不同数字之间的映射?

java - 列表转队列如何实现FIFO

java - 如何分析 .hprof 文件?

java - JSTL fn :length() Function with byte[]

java - 我的 Java 应用程序中的两个类具有相同的限定名称

java - 如何消费物联网设备产生的数据?

java - 为什么我的maven构建在执行时找不到Netty的EventLoopGroup类?

Spring Boot 应用程序 url 不能为空

java - 未在 distributionManagement 元素内的 POM 或 -DaltDep loymentRepository=id::layout::url 参数中指定存储库元素