2011-10-02 257 views
0

我试图建立一个可执行的jar与依赖的春天项目。异常NoClassDefFoundError从部署弹簧项目与maven程序集

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

当我跑我得到异常: 异常在线程 “主要” java.lang.NoClassDefFoundError:组织/ springframework的/ context/support/ClassPathXmlApplicationContext

我在做什么错?

我不希望maven创建一个jar文件,并在lib文件夹之外创建一个jar文件。

+0

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

回答

2

与玩了两天后,该解决方案是: 因为我把一个lib文件中的所有依赖的jar(汇编文件)

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

增加了Spring上下文依赖性:

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

您也可以在申报的pom.xml的顶部的性质春季版本,并引用它,比如:

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

然后用:

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

不需要添加spring上下文,它在spring本身内部(它工作,如果我不把它放到lib中) –

0

阿迪,

你缺少org.springframework.context.support罐子

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

从您提供的装配描述符,您明确界定,它应该产生的所有依赖到的lib /文件夹。

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

还有一个选择,你可以用里面定义的类路径生成MANIFEST。

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

+0

如何生成这样的清单 –

0

A ClassNotFoundException通常表示相关类不在CLASSPATH中。类加载器找不到它。你假设CLASSPATH中有东西,但事实并非如此。解决方案是检查您的假设,并找出如何在运行时在CLASSPATH中创建所有必需的类。