gpt4 book ai didi

java - 如何生成具有 Maven 和第 3 方依赖项的 JAR

转载 作者:行者123 更新时间:2023-12-02 09:44:50 24 4
gpt4 key购买 nike

我正在使用 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 类属于我必须在项目中使用的第 3 方 jar (GestorProblema1maquina.jar),所以我不知道这是否是问题的原因。

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

Screenshot

最佳答案

这取决于 GestorProblema1maquina.jar 的构建方式。 GestorProblema1maquina.jar 是否包含带有 dominio/Instancia 类的第 3 方 jar,例如,它是一个 fat jar/uber jar吗?
如果它是使用 Maven 程序集插件构建的,并且包含其所有依赖项,那么您不应在运行时遇到此问题。

有2个选择

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

此外,它还声明“正在解释的范围是 Maven 看到的范围,而不是 pom 中指定的范围”,因此您的插件配置可能需要显式包含第 3 方系统范围 jar 依赖项,以包含在最终的 jar 。

关于java - 如何生成具有 Maven 和第 3 方依赖项的 JAR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56741764/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com