【问题标题】:exception NoClassDefFoundError from deploy spring project with maven assembly使用 Maven 程序集部署 spring 项目的异常 NoClassDefFoundError
【发布时间】:2011-11-29 10:42:17
【问题描述】:

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

【问题讨论】:

  • 您有几个可以提供解决方案的答案,但您从未说过您是如何运行应用程序来获得该异常的。运行时是否将lib 的内容添加到类路径中?

标签: java spring maven maven-assembly-plugin


【解决方案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>

【讨论】:

    【解决方案2】:

    添加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>
    

    【讨论】:

    • 不需要添加 spring 上下文,它在 spring 本身内部(如果我不这样做,它可以工作;不把所有东西都放到 lib 中)
    【解决方案3】:

    阿迪,

    你缺少 org.springframework.context.support jar

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

    【讨论】:

      【解决方案4】:

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

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

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

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

      【讨论】:

        【解决方案5】:

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

        【讨论】:

          猜你喜欢
          • 2014-08-12
          • 2014-05-27
          • 2012-04-16
          • 2016-12-07
          • 1970-01-01
          • 2023-03-29
          • 1970-01-01
          • 1970-01-01
          • 2018-10-22
          相关资源
          最近更新 更多