1

我相信这个问题之前也被问过。我已经尝试过提到的其他选项,但没有成功:(

背景:使用 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)

对此有什么想法吗?我为所有依赖的 jar 使用 spring 版本 3.0.6。

谢谢 Gendaful

4

2 回答 2

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>
于 2012-11-17T03:13:55.323 回答
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>
于 2013-11-14T11:42:28.063 回答