使用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
最后一行“行”作为行分隔符.似乎MANIFEST.MF文件看起来有点腐败,不是吗?
解决方法:
不知道发生了什么,但这就是我的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>