Maven exec 有效,但 java -jar 无效
Maven exec works, but java -jar does not
我有一个简单的 Maven 项目,只有一个依赖项。我可以通过 Exec Maven Plugin:
安装它并 运行 它的命令行
mvn exec:java -D"exec.mainClass"="com.MyClass"
maven打包后在我的目录下生成了一个.jar文件。在 Maven JAR Plugin 的帮助下,我制作了清单以了解我的 main
方法 class。看起来像:
...
Created-By: Apache Maven 3.3.1
Build-Jdk: 1.8.0_66
Main-Class: com.MyClass
现在我想 运行 这个 .jar 文件就像使用 java
命令的常规 java 可执行文件一样,但是在执行以下操作之后:
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 Plugin:
This plugin provides the capability to package the artifact in an
uber-jar, including its dependencies and to shade - i.e. rename - the
packages of some of the dependencies.
这个插件对于有很多依赖项的大型项目有一些优势。这在这里解释:Difference between maven plugins ( 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>
我有一个简单的 Maven 项目,只有一个依赖项。我可以通过 Exec Maven Plugin:
安装它并 运行 它的命令行mvn exec:java -D"exec.mainClass"="com.MyClass"
maven打包后在我的目录下生成了一个.jar文件。在 Maven JAR Plugin 的帮助下,我制作了清单以了解我的 main
方法 class。看起来像:
...
Created-By: Apache Maven 3.3.1
Build-Jdk: 1.8.0_66
Main-Class: com.MyClass
现在我想 运行 这个 .jar 文件就像使用 java
命令的常规 java 可执行文件一样,但是在执行以下操作之后:
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 Plugin:
This plugin provides the capability to package the artifact in an uber-jar, including its dependencies and to shade - i.e. rename - the packages of some of the dependencies.
这个插件对于有很多依赖项的大型项目有一些优势。这在这里解释:Difference between maven plugins ( 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>