使用Maven打包jar时获取org / springframework / context / support / ClassPathXmlApplicationContext的NoClassDefFoundError

时间:2012-11-16 17:40:15

标签: spring pom.xml

我确信此问题也在之前被问过。我已经尝试了其他选项,但没有成功:(

BackGround:使用maven打包包含所有依赖项的jar。

的pom.xml

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
        <execution>
            <id>assembly:package</id>
            <phase>package</phase>
            <goals>
                <!-- Work around for http://jira.codehaus.org/browse/MASSEMBLY-97 
                    as the goal should be attached. -->
                <goal>single</goal>
            </goals>
            <configuration>
                <descriptors>
                    <descriptor>src/main/assembly/assembly.xml</descriptor>
                </descriptors>
            </configuration>
        </execution>
    </executions>
</plugin>

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <mainClass>com.Application</mainClass>
            </manifest>
            <manifestEntries>
                <Class-Path>config/</Class-Path>
            </manifestEntries>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
            </manifest>
        </archive>
    </configuration>
</plugin> 

类路径

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src/main/java" />
    <classpathentry kind="src" path="src/main/resources" />
    <classpathentry exported="true" kind="con"
        path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER" />
    <classpathentry kind="con"
        path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jre7" />
</classpath>

当我使用

运行导出的jar时
java -cp Scheduler-0.0.1-SNAPSHOT-exe.jar com.Application

我的异常是

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/c
ontext/support/ClassPathXmlApplicationContext
        at com.Application.main(Application.java:13)
Caused by: java.lang.ClassNotFoundException: org.springframework.context.support
.ClassPathXmlApplicationContext
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)

有关于此的任何想法吗?我使用的是Spring版本3.0.6,适用于所有相关的罐子。

由于 Gendaful

2 个答案:

答案 0 :(得分:1)

您提到的类路径是eclipse项目的类路径。使用构建目标准备JAR时,maven构建工具不会考虑它。您应该在pom.xml中将spring jar指定为依赖项,如下所示,以便Maven在打包应用程序时考虑它们。

<dependencies>
    ...

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>3.1.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>3.1.1.RELEASE</version>
    </dependency>

    ...
</dependencies>

答案 1 :(得分:0)

尝试在pom.xml插件中添加此插件。

      <!-- Adding depenencies to jar file -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.1</version>
        <configuration>
         <configuration>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                  <resource>META-INF/spring.handlers</resource>
                </transformer>
                <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                  <resource>META-INF/spring.schemas</resource>
                </transformer>
              </transformers>
            </configuration>
        </configuration>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
相关问题