Java 使用maven程序集部署spring项目时出现异常NoClassDefFoundError

Java 使用maven程序集部署spring项目时出现异常NoClassDefFoundError,java,spring,maven,maven-assembly-plugin,Java,Spring,Maven,Maven Assembly Plugin,我正在尝试为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">

我正在尝试为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>

4.0.0
com.xxx
xxx
3.0.x快照
com.mindcite
mindcite rdfizer工具
3.0.4-快照
罐子
公司:项目
org.codehaus.mojo
execmaven插件
1.2
org.springframework
春天
2.5.2
src
形态
org.apache.maven.plugins
maven eclipse插件
2.8
2
org.eclipse.jdt.core.javabuilder
org.eclipse.ajdt.core.ajbuilder
org.springframework.aspects
org.springframework.ide.eclipse.core.springbuilder
org.eclipse.jdt.core.javanature
org.springframework.ide.eclipse.core.springnature
maven编译器插件
1.6
1.6
maven汇编插件
组装
包裹
附属的
assembly/package.xml
我的程序集描述符:

<?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>

满的
罐子
假的
假的
运行时
解放党
${project.build.outputDirectory}
/
假的
图标
图标
**/*
形态
形态
**/*
当我运行时,会出现异常: 线程“main”java.lang.NoClassDefFoundError中出现异常:org/springframework/context/support/ClassPathXmlApplicationContext

我做错了什么


我不希望maven创建一个jar文件,其中包含lib文件夹之外的所有依赖项jar。

添加spring上下文依赖项:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>2.5.2</version>
</dependency>

org.springframework
spring上下文
2.5.2
您还可以在pom.xml顶部的属性中声明spring版本并引用它,例如:

<properties>
    <spring.version>2.5.2</spring.version>
</properties>

2.5.2
然后使用:

<version>${spring.version}</version>
${spring.version}
Adi

您缺少org.springframework.context.support jar

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>2.5.2</version>
</dependency>

org.springframework
spring上下文支持
2.5.2

从您提供的程序集描述符中,您明确定义了它,它应该将所有依赖项生成到lib/folder中

然而,将库jar放入lib/folder是一种非常常见的方法。通常在这种情况下,您将提供一个简单的脚本来启动程序,该脚本将lib/*.jar下的所有jar作为类路径

还有一种选择,您可以使用内部定义的类路径生成清单


确切的解决方案基本上取决于上下文,很可能取决于您希望如何交付二进制分发。

A
ClassNotFoundException
通常意味着所讨论的类不在类路径中。类装入器找不到它。您假设类路径中存在某些内容,但事实并非如此。解决方案是检查您的假设,并找出如何在运行时使类路径中的所有必要类都可用。

在玩了两天之后,解决方案是: 因为我将所有依赖项jar放在一个lib文件中(在程序集文件中)


假的
运行时
解放党
我需要自己在pom文件中创建类路径:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>

   <configuration>
        <archive>
            <manifest>
                 <mainClass>myMainclass</mainClass>
                 <addClasspath>true</addClasspath>
                  <classpathPrefix>lib/</classpathPrefix>
            </manifest>
            <manifestEntries>
                <Class-Path>conf/</Class-Path>
            </manifestEntries>
         </archive>
     </configuration>
 </plugin>

org.apache.maven.plugins
maven jar插件
myMainclass
真的
解放党/
形态/

您有几个答案可以提供解决方案,但您从未说过如何运行应用程序以获得该异常。运行时是否将
lib
的内容添加到类路径中?无需添加spring上下文,它位于spring本身内部(如果我不将所有内容都放在lib中,它就会工作)
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>

   <configuration>
        <archive>
            <manifest>
                 <mainClass>myMainclass</mainClass>
                 <addClasspath>true</addClasspath>
                  <classpathPrefix>lib/</classpathPrefix>
            </manifest>
            <manifestEntries>
                <Class-Path>conf/</Class-Path>
            </manifestEntries>
         </archive>
     </configuration>
 </plugin>