[英]JavaFX with Maven works with exec:java but not in the jar at runtime
[英]Maven exec works, but java -jar does not
我有一个只有一个依赖项的简单Maven项目。 我可以安装它并通过Exec Maven插件在命令行中运行它:
mvn exec:java -D"exec.mainClass"="com.MyClass"
打包后,maven在我的目录中生成一个.jar文件。 在Maven JAR插件的帮助下,我明确了自己的清单,以了解我的main
方法类。 看起来像:
...
Created-By: Apache Maven 3.3.1
Build-Jdk: 1.8.0_66
Main-Class: com.MyClass
现在,我想使用java
命令像常规的Java可执行文件一样运行此.jar文件,但是请执行以下操作:
java -jar myFile.jar
它给出了关于我唯一的依赖关系的错误java.lang.NoClassDefFoundError
。
我如何使Maven将所有依赖项添加到我的可执行jar文件中?
您可以使用Apache Maven Assembly Plugin来创建具有所有依赖项的jar,因此pom.xml应该如下所示:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>mypackage.myclass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
希望对您有帮助,再见。
实现此目的的一种方法是使用Apache Maven Shade插件 :
该插件提供了将工件打包(包括其依赖项)并遮蔽(即重命名)某些依赖项的包的功能。
对于具有许多依赖性的大型项目,此插件具有一些优点。 此处对此进行了解释: Maven插件之间的区别(assembly-plugins,jar-plugins,shaded-plugins)
在我的项目中,我将其用于以下配置:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.xxx.tools.imagedump.ImageDumpLauncher</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>
</execution>
</executions>
</plugin>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.