使用 Maven 程序集部署 Spring 项目时出现异常 NoClassDefFoundError
我正在尝试为 spring 项目构建一个具有依赖项的可执行 jar。
我的 pom:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.xxx</groupId>
<artifactId>xxx</artifactId>
<version>3.0.x-SNAPSHOT</version>
</parent>
<groupId>com.mindcite</groupId>
<artifactId>mindcite-rdfizer-tool</artifactId>
<version>3.0.4-SNAPSHOT</version>
<packaging>jar</packaging>
<name>compony :: project</name>
<dependencies>
<dependency>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.2</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>conf</directory>
</resource>
</resources>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.8</version>
<configuration>
<wtpversion>2.0</wtpversion>
<buildcommands>
<buildcommand>org.eclipse.jdt.core.javabuilder</buildcommand>
<buildCommand>
<name>org.eclipse.ajdt.core.ajbuilder</name>
<arguments>
<aspectPath>org.springframework.aspects</aspectPath>
</arguments>
</buildCommand>
<buildCommand>
<name>org.springframework.ide.eclipse.core.springbuilder</name>
</buildCommand>
</buildcommands>
<additionalProjectnatures>
<projectnature>org.eclipse.jdt.core.javanature</projectnature>
<projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
</additionalProjectnatures>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>attached</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptors>
<descriptor>assembly/package.xml</descriptor>
</descriptors>
</configuration>
</plugin>
</plugins>
</build>
</project>
我的程序集描述符:
<?xml version="1.0" encoding="UTF-8"?>
<assembly>
<id>full</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<unpack>false</unpack>
<scope>runtime</scope>
<outputDirectory>lib</outputDirectory>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>${project.build.outputDirectory}</directory>
<outputDirectory>/</outputDirectory>
<useDefaultExcludes>false</useDefaultExcludes>
</fileSet>
<fileSet>
<directory>icons</directory>
<outputDirectory>icons</outputDirectory>
<includes>
<include>**/*</include>
</includes>
</fileSet>
<fileSet>
<directory>conf</directory>
<outputDirectory>conf</outputDirectory>
<includes>
<include>**/*</include>
</includes>
</fileSet>
</fileSets>
</assembly>
当我运行时出现异常: 线程“main”中的异常 java.lang.NoClassDefFoundError: org/springframework/context/support/ClassPathXmlApplicationContext
我做错了什么?
我不希望 maven 创建一个包含 lib 文件夹之外的所有依赖项 jar 的 jar 文件。
i'm trying to build an executable jar with dependencies for a spring project.
my pom:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.xxx</groupId>
<artifactId>xxx</artifactId>
<version>3.0.x-SNAPSHOT</version>
</parent>
<groupId>com.mindcite</groupId>
<artifactId>mindcite-rdfizer-tool</artifactId>
<version>3.0.4-SNAPSHOT</version>
<packaging>jar</packaging>
<name>compony :: project</name>
<dependencies>
<dependency>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.2</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>conf</directory>
</resource>
</resources>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.8</version>
<configuration>
<wtpversion>2.0</wtpversion>
<buildcommands>
<buildcommand>org.eclipse.jdt.core.javabuilder</buildcommand>
<buildCommand>
<name>org.eclipse.ajdt.core.ajbuilder</name>
<arguments>
<aspectPath>org.springframework.aspects</aspectPath>
</arguments>
</buildCommand>
<buildCommand>
<name>org.springframework.ide.eclipse.core.springbuilder</name>
</buildCommand>
</buildcommands>
<additionalProjectnatures>
<projectnature>org.eclipse.jdt.core.javanature</projectnature>
<projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
</additionalProjectnatures>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>attached</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptors>
<descriptor>assembly/package.xml</descriptor>
</descriptors>
</configuration>
</plugin>
</plugins>
</build>
</project>
my assembly descriptor:
<?xml version="1.0" encoding="UTF-8"?>
<assembly>
<id>full</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<unpack>false</unpack>
<scope>runtime</scope>
<outputDirectory>lib</outputDirectory>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>${project.build.outputDirectory}</directory>
<outputDirectory>/</outputDirectory>
<useDefaultExcludes>false</useDefaultExcludes>
</fileSet>
<fileSet>
<directory>icons</directory>
<outputDirectory>icons</outputDirectory>
<includes>
<include>**/*</include>
</includes>
</fileSet>
<fileSet>
<directory>conf</directory>
<outputDirectory>conf</outputDirectory>
<includes>
<include>**/*</include>
</includes>
</fileSet>
</fileSets>
</assembly>
when i run i get exception:
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/context/support/ClassPathXmlApplicationContext
what am i doing wrong?
i don't want maven to create a jar file with all the dependencies jar outside the lib folder.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
玩了两天,解决办法是:
因为我将所有依赖项 jar 放在 lib 文件中(在程序集文件中),所以
我需要自己在 pom 文件中创建类路径:
after playing with that for two days, the solution was:
because i put all the dependencies jar in a lib file (in the assembly file)
i needed to create the class path myself, in the pom file:
添加 spring-context 依赖:
还可以在 pom.xml 之上的属性中声明 spring 版本并引用它,如:
然后使用:
Add spring-context dependency:
You can also declare the spring version in the properties on top of pom.xml and reference it, such as:
And then use:
阿迪,
你缺少 org.springframework.context.support jar
Adi,
You are missing org.springframework.context.support jar
根据您提供的程序集描述符,您明确定义了它,它应该将所有依赖项生成到 lib/ 文件夹中。
然而,将库 jar 放入 lib/ 文件夹中是一种非常常见的方法。通常在这种情况下,您将提供一个简单的脚本来启动程序,其中包含 lib/*.jar 下的所有 jar 作为类路径。
还有一种选择,您可以生成 MANIFEST 并在其中定义类路径。
确切的解决方案基本上取决于上下文,并且很可能取决于您希望如何交付二进制发行版。
From the assembly descriptor you provided, you explicitly defining that, it should generate all dependency into lib/ folder.
However, it is a very common way to put library jars into lib/ folder. And usually in that case, you will provide a simple script to start the program which include all the jars under lib/*.jar as classpath.
There is one more alternative, you can generate MANIFEST with classpath defined inside.
The exact solution is basically context dependent and most likely depends on how you would like to delivery the binary distribution.
ClassNotFoundException 通常意味着相关类不在 CLASSPATH 中。类加载器找不到它。您假设 CLASSPATH 中有某些内容,但事实并非如此。解决方案是检查您的假设并找出如何在运行时使所有必要的类在 CLASSPATH 中可用。
A
ClassNotFoundException
usually means that the class in question is not in the CLASSPATH. The class loader can't find it. You're assuming that something is in the CLASSPATH, but it's not. The solution is to check your assumptions and figure out how to make all the necessary classes available in the CLASSPATH at runtime.