使用Maven程序集部署Spring项目时出现异常NoClassDefFoundError [英] exception NoClassDefFoundError from deploy spring project with maven assembly

查看:101
本文介绍了使用Maven程序集部署Spring项目时出现异常NoClassDefFoundError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为Spring项目构建具有依赖性的可执行jar.

i'm trying to build an executable jar with dependencies for a spring project.

我的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

when i run i get exception: Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/context/support/ClassPathXmlApplicationContext

我在做什么错了?

我不希望maven使用lib文件夹之外的所有依赖项jar创建一个jar文件.

i don't want maven to create a jar file with all the dependencies jar outside the lib folder.

推荐答案

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

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)

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

我需要在pom文件中自己创建类路径:

i needed to create the class path myself, in the pom file:

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

这篇关于使用Maven程序集部署Spring项目时出现异常NoClassDefFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆