如何使用 Maven 和 3rd 方依赖项生成 JAR

How to generate a JAR with Maven and 3rd party dependencies

我正在使用 Maven 开发一个 Java 项目,我正在尝试生成 jar 文件。我正在使用 mvn-jar-plugin 和 mvn-dependency-plugin 来尝试包含所有必需的库。这是我的 pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>TFG</groupId>
    <artifactId>TFG</artifactId>
    <version>1.0-RELEASE</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>8</java.version>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
    </properties>

    <build>
        <sourceDirectory>src/main/java</sourceDirectory>
        <testSourceDirectory>src/test/java</testSourceDirectory>
        <plugins>

            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-project-info-reports-plugin</artifactId>
                <version>2.7</version>
                <configuration>
                    <dependencyLocationsEnabled>false</dependencyLocationsEnabled>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.2</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>gui.ApplicationWindow</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
                <configuration>
                    <argLine>${surefireArgLine}</argLine>
                    <skipTests>${skip.unit.tests}</skipTests>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.4</version>
                <executions>
                    <execution>
                        <id>pre-unit-test</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                        <configuration>
                            <destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
                            <propertyName>surefireArgLine</propertyName>
                                <excludes>
                                    <exclude></exclude>
                                </excludes>
                        </configuration>
                    </execution>
                    <execution>
                        <id>post-unit-test</id>
                        <phase>test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                        <configuration>
                            <dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
                            <outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

    <dependencies>

        <dependency>
            <groupId>org.jfree</groupId>
            <artifactId>jfreechart</artifactId>
            <version>1.0.19</version>
        </dependency>

        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.0.6</version>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.4.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.4.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.0</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.sun.woodstock.dependlibs</groupId>
            <artifactId>jhall</artifactId>
            <version>2.0</version>
        </dependency>

        <dependency>
            <groupId>com.gestor</groupId>
            <artifactId>GestorProblema1maquina</artifactId>
            <version>1.0</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/GestorProblema1maquina.jar</systemPath>
        </dependency>

    </dependencies>

</project>

问题是当我尝试 运行 我的 jar 文件时出现此错误:

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: dominio/Instancia
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
        at java.lang.Class.privateGetMethodRecursive(Unknown Source)
        at java.lang.Class.getMethod0(Unknown Source)
        at java.lang.Class.getMethod(Unknown Source)
        at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: dominio.Instancia
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 7 more

这里是生成的 jar 的 MANIFEST.MF 文件:

Manifest-Version: 1.0
Class-Path: lib/jfreechart-1.0.19.jar lib/jcommon-1.0.23.jar lib/itext
 pdf-5.0.6.jar lib/bcmail-jdk14-1.38.jar lib/bcprov-jdk14-1.38.jar lib
 /bctsp-jdk14-1.38.jar lib/poi-4.1.0.jar lib/commons-codec-1.12.jar li
 b/commons-collections4-4.3.jar lib/commons-math3-3.6.1.jar lib/jhall-
 2.0.jar
Build-Jdk-Spec: 1.8
Created-By: Maven Archiver 3.4.0
Main-Class: gui.ApplicationWindow

dominio/Instancia class 属于我必须在我的项目中使用的第 3 方 jar (GestorProblema1maquina.jar),所以我不知道这是否是问题的原因.

编辑:添加了包含 GestorProblema1maquina.jar 内容的屏幕截图(MANIFEST.MF 为空)

这取决于 GestorProblema1maquina.jar 是如何构建的。 GestorProblema1maquina.jar 是否包含带有 dominio/Instancia class 的第 3 方 jar,例如,它是 fat jar / uber jar 吗?
如果它是使用 maven 程序集插件以包含所有依赖项的方式构建的,那么你不应该 运行 在 运行 时加入它。

有2个选择

  1. 使用 mvn install 并使 GestorProblema1maquina.jar 在存储库中可用。
  2. [link] https://maven.apache.org/plugins/maven-dependency-plugin/copy-dependencies-mojo.html 表示依赖项是从存储库中复制的。

它还声明 "The scopes being interpreted are the scopes as Maven sees them, not as specified in the pom" ,因此您的插件配置可能需要显式包含第 3 方系统范围 jar 依赖项以包含在最终的 jar 中。