有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Maven exec可以工作,但java jar不能

我有一个简单的maven项目,只有一个依赖项。我可以通过Exec Maven Plugin安装并运行它的命令行:

mvn exec:java -D"exec.mainClass"="com.MyClass"

打包后,maven生成一个。jar文件在我的目录中。在Maven JAR Plugin的帮助下,我清楚地知道了我的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文件中


共 (2) 个答案

  1. # 1 楼答案

    实现这一点的一种方法是使用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>
    
  2. # 2 楼答案

    您可以使用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>
    

    希望对你有帮助,再见