部署具有maven程序集的spring项目的异常NoClassDefFoundError

时间:2011-10-02 15:25:03

标签: 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">
    <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文件。

5 个答案:

答案 0 :(得分:1)

在玩了两天之后,解决方案是: 因为我将所有依赖jar都放在lib文件中(在汇编文件中)

<dependencySets>
    <dependencySet>
        <unpack>false</unpack>
        <scope>runtime</scope>
        <outputDirectory>lib</outputDirectory>
    </dependencySet>
</dependencySets>

我需要在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>

答案 1 :(得分:0)

添加spring-context依赖项:

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

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

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

然后使用:

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

答案 2 :(得分:0)

Adi,

您缺少org.springframework.context.support jar

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

答案 3 :(得分:0)

从您提供的汇编描述符中,您明确定义它,它应该生成所有依赖到lib /文件夹。

但是,将库jar放入lib /文件夹是一种非常常见的方法。通常在这种情况下,您将提供一个简单的脚本来启动程序,其中包括lib / *。jar下的所有jar作为类路径。

还有一个替代方案,您可以使用内部定义的类路径生成MANIFEST。

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

答案 4 :(得分:0)

ClassNotFoundException通常意味着有问题的类不在CLASSPATH中。类加载器找不到它。你假设某些东西在CLASSPATH中,但事实并非如此。解决方案是检查您的假设并找出如何在运行时在CLASSPATH中提供所有必需的类。