使用maven-dependency-plugin使用maven 3.1创建可运行的jar不会创建可运行的jar

时间:2014-02-28 13:46:52

标签: java eclipse maven jar noclassdeffounderror

使用Maven 3.1
Eclipse Helios

商Aspekt:

尝试使用maven-jar / dependency-plugins创建一个可运行的jar文件。

问题: 创建jar文件和依赖项后,当我尝试使用命令

启动jar文件时,会出现NoCLassDefFoundErrors
  java -jar program.jar

但类/文件在./dedency]-jars文件夹中可用??? 我也尝试了以下命令:

  java -cp ./dependency-jars -jar program.jar

这也没有用。

问题:

你知道为什么jvm找不到这些课吗?有什么问题?

的pom.xml:

<build>
    <finalName>program</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-eclipse-plugin</artifactId>
            <version>2.9</version>
            <configuration>
                <downloadSources>true</downloadSources>
                <downloadJavadocs>true</downloadJavadocs>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>${jdk.version}</source>
                <target>${jdk.version}</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>

                    <manifest>

                        <addClasspath>true</addClasspath>
                        <classpathPrefix>dependency-jars/</classpathPrefix>
                        <mainClass>de.test.MainCLass</mainClass>
                    </manifest>
                </archive>
                <outputDirectory>${package-target-folder}</outputDirectory>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-javadoc-plugin</artifactId>
            <version>2.9.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>javadoc</goal>
                    </goals>
                    <phase>package</phase>
                    <id>create-javadoc</id>
                    <configuration>
                        <charset>UTF-8</charset>
                        <outputDirectory>${package-target-folder}/docs</outputDirectory>
                        <reportOutputDirectory>${package-target-folder}/docs</reportOutputDirectory>

                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.8</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${package-target-folder}/dependency-jars/</outputDirectory>
                    </configuration>
                </execution>
                <execution>


                </execution>
            </executions>
        </plugin>

    </plugins>
</build>

[UPDATE]

创建的MANIFEST.MF看起来像

Manifest-Version: 1.0
Built-By: ahausden
Build-Jdk: 1.7.0_25
Class-Path: dependency-jars/spring-core-4.0.0.RELEASE.jar dependency-j
ars/commons-logging-1.1.1.jar dependency-jars/spring-jdbc-4.0.0.RELEA
SE.jar dependency-jars/spring-beans-4.0.0.RELEASE.jar dependency-jars
/spring-tx-4.0.0.RELEASE.jar dependency-jars/spring-oxm-4.0.0.RELEASE
.jar dependency-jars/spring-batch-core-2.2.0.RELEASE.jar dependency-j
ars/xstream-1.3.jar dependency-jars/xpp3_min-1.1.4c.jar dependency-ja
rs/jettison-1.1.jar dependency-jars/spring-aop-3.2.0.RELEASE.jar depe
ndency-jars/spring-context-3.2.0.RELEASE.jar dependency-jars/spring-e
xpression-3.2.0.RELEASE.jar dependency-jars/spring-batch-infrastructu
re-2.2.0.RELEASE.jar dependency-jars/spring-retry-1.0.2.RELEASE.jar d
ependency-jars/spring-batch-test-2.2.0.RELEASE.jar dependency-jars/co
mmons-io-1.4.jar dependency-jars/commons-dbcp-1.2.2.jar dependency-ja
rs/commons-pool-1.3.jar dependency-jars/commons-collections-3.2.jar d
ependency-jars/spring-test-4.0.0.RELEASE.jar dependency-jars/javax.in
ject-1.jar dependency-jars/slf4j-log4j12-1.6.1.jar dependency-jars/sl
f4j-api-1.6.1.jar dependency-jars/log4j-1.2.16.jar dependency-jars/sp
ring-orm-4.0.0.RELEASE.jar dependency-jars/aopalliance-1.0.jar depend
ency-jars/mariadb-java-client-1.1.1.jar dependency-jars/hibernate-cor
e-4.3.1.Final.jar dependency-jars/jboss-logging-3.1.3.GA.jar dependen
cy-jars/jboss-logging-annotations-1.2.0.Beta1.jar dependency-jars/jbo
ss-transaction-api_1.2_spec-1.0.0.Final.jar dependency-jars/dom4j-1.6
.1.jar dependency-jars/xml-apis-1.0.b2.jar dependency-jars/hibernate-
commons-annotations-4.0.4.Final.jar dependency-jars/hibernate-jpa-2.1
-api-1.0.0.Final.jar dependency-jars/javassist-3.18.1-GA.jar dependen
cy-jars/antlr-2.7.7.jar dependency-jars/jandex-1.1.0.Final.jar depend
ency-jars/hibernate-annotations-3.5.6-Final.jar dependency-jars/hiber
nate-commons-annotations-3.2.0.Final.jar dependency-jars/hibernate-jp
a-2.0-api-1.0.0.Final.jar dependency-jars/javassist-3.12.1.GA.jar dep
endency-jars/commons-cli-1.3-20140221.042048-103.jar
Created-By: Apache Maven 3.1.0
Main-Class: de.test.MainClass
Archiver-Version: Plexus Archiver

最后一行&#34;&#34;是行分隔符。似乎MANIFEST.MF文件看起来有点腐败,不是吗?

7 个答案:

答案 0 :(得分:2)

不确定发生了什么,但这就是我的pom用于使用阴影构建可运行的jar的样子:

 <build>
        <resources>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
                <filtering>false</filtering>
                <includes>
                    <include>schema.xsd</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <shadedArtifactAttached>true</shadedArtifactAttached>
                    <shadedClassifierName>stand-alone</shadedClassifierName>
                    <artifactSet>
                        <excludes>
                            <exclude>org.slf4j:slf4j-api:jar:</exclude>
                            <exclude>org.slf4j:slf4j-log4j12:jar:</exclude>
                            <exclude>org.slf4j:jcl-over-slf4j:jar:</exclude>
                            <exclude>commons-logging:commons-logging:jar:</exclude>
                            <exclude>commons-logging:commons-logging-api:jar:</exclude>
                        </excludes>
                    </artifactSet>
                    <filters>
                        <filter>
                            <artifact>*:*</artifact>
                            <excludes>
                                <exclude>META-INF/*.SF</exclude>
                                <exclude>META-INF/*.DSA</exclude>
                                <exclude>META-INF/*.RSA</exclude>
                            </excludes>
                        </filter>
                    </filters>
                    <finalName>MyFinalRunnableJarName</finalName>
                    <transformers>
                        <transformer
                            implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                            <mainClass>class.with.main.Method</mainClass>
                        </transformer>
                        <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>
            </plugin>
        </plugins>
    </build>

答案 1 :(得分:1)

尝试提供清单文件的输入:

Main-Class: YourClassWithMainMethod

另见this post。所以你可以手动制作,看看差异。

如果dependency-jars是目录,请尝试:

java -cp ./dependency-jars/* -jar program.jar

我仍然无法发表评论,所以请说明这些NoCLassDefFoundErrors是什么。 你是否尝试过从Eclipse手动执行相同的jar文件并看到差异?

你的清单中有依赖依赖项-jars / commons-cli-1.3-20140221.042048-103.jar,但你写道你在目录中有SNAPSHOT版本。在maven中心只有commons-cli:commons-cli:20040117.000000。

@robermann请参阅this post(工作时应使用双引号)

答案 2 :(得分:1)

最简单的解决方案是使用maven-assembly-plugin,如下所示:

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <!-- NOTE: We don't need a groupId specification because the group is
             org.apache.maven.plugins ...which is assumed by default.
         -->
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
        [...]
</project>

答案 3 :(得分:0)

您确定在命令行启动的Java版本是否等于或大于编译时maven使用的版本?试试java -version

答案 4 :(得分:0)

通过命令行,转到“dependency-jars”的父目录并运行:java -jar program.jar

答案 5 :(得分:0)

好的,我解决了第二个/更新的问题:

我首先使用eclipse和maven插件来创建可运行的jar文件。这在所描述的问题中运行。

毕竟我试图从控制台应用程序使用maven 3.1,毕竟它的工作原理。 似乎eclipse maven插件有一些问题。

答案 6 :(得分:0)

使用此命令创建任何maven项目的jar文件。

mvn clean compile install