[英]How to generate a JAR with Maven and 3rd party dependencies
I'm working on a Java project with maven and I'm trying to generate the jar file. 我正在使用maven进行Java项目,并且正在尝试生成jar文件。 I'm using mvn-jar-plugin and mvn-dependency-plugin to try to include all the required libraries. 我正在使用mvn-jar-plugin和mvn-dependency-plugin尝试包括所有必需的库。 Here's my pom.xml: 这是我的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>
The problem is that when I try to run my jar file I get this error: 问题是,当我尝试运行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
Here is the MANIFEST.MF file of the generated jar: 这是生成的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
The dominio/Instancia class belongs to a 3rd party jar I have to use in my project (GestorProblema1maquina.jar), so I don't know if that's the cause of the problem. dominio / Instancia类属于我在项目中必须使用的第三方jar(GestorProblema1maquina.jar),所以我不知道这是否是问题的原因。
EDIT: Added screenshot with the contents of GestorProblema1maquina.jar (MANIFEST.MF is empty) 编辑:添加了带有GestorProblema1maquina.jar内容的屏幕截图(MANIFEST.MF为空)
It depends on how GestorProblema1maquina.jar was built. 这取决于GestorProblema1maquina.jar的构建方式。 Does GestorProblema1maquina.jar contain the 3rd party jar with dominio/Instancia class, as in, is it a fat jar / uber jar? GestorProblema1maquina.jar是否包含具有dominio / Instancia类的3rd party jar,例如,它是肥罐还是uber jar?
If it is built using maven assembly plugin in a way that it includes all of its dependencies, you should not run into this at runtime. 如果使用maven程序集插件以包含所有依赖项的方式构建它,则不应在运行时遇到此问题。
There are 2 choices 有2个选择
Also it states "The scopes being interpreted are the scopes as Maven sees them, not as specified in the pom" , so your configuration for the plugin may need explicit includes for the 3rd party system scope jar dependency to be included in the eventual jar. 它还指出“正在解释的范围是Maven看到的范围,而不是pom中指定的范围”,因此您的插件配置可能需要显式包含,以便最终的jar中包含第三方系统范围jar依赖项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.