[英]maven-jar-plugin generates jar with wrong MANIFEST.MF
I want to generate an jar file including its dependencies.我想生成一个 jar 文件,包括它的依赖项。 Here is my pom.xml:这是我的 pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>vehicle</artifactId>
<groupId>org</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>vehicle.cli</artifactId>
<dependencies>
<dependency>
<groupId>org</groupId>
<artifactId>vehicle.model</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<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}/libs
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>libs/</classpathPrefix>
<mainClass>
org.vehicle.cli.Main
</mainClass>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
The dependency org.vehicle.model I take it from a custom nexus repository.依赖org.vehicle.model我从自定义的关系存储库中获取它。 After running mvn package
I get the target folder with the libs folder and the jar file in the same hiarachy:运行mvn package
后,我在同一层次结构中获得了包含 libs 文件夹和 jar 文件的目标文件夹:
- target
- libs
- vehicle.model-0.0.1-SNAPSHOT.jar
- commons-cli-1.4.jar
- vehicle.cli-0.0.1-SNAPSHOT.jar
When I execute the vehicle.cli-0.0.1-SNAPSHOT.jar ( java -jar vehicle.cli-0.0.1-SNAPSHOT.jar
) I get an exception java.lang.NoClassDefFoundError for the classes in the vehicle.model-0.0.1-SNAPSHOT.jar It's strange because I have all the dependencies jar already in the libs folder, so I unpack the vehicle.cli-0.0.1-SNAPSHOT.jar to check the MANIFEST.MF and see the following: When I execute the vehicle.cli -0.0.1-SNAPSHOT.jar ( java -jar vehicle.cli-0.0.1-SNAPSHOT.jar
) I get an exception java.lang.NoClassDefFoundError for the classes in the vehicle.model -0.0 .1-SNAPSHOT.jar It's strange because I have all the dependencies jar already in the libs folder, so I unpack the vehicle.cli-0.0.1-SNAPSHOT.jar to check the MANIFEST.MF and see the following:
Manifest-Version: 1.0
Created-By: Maven Jar Plugin 3.2.0
Build-Jdk-Spec: 16
Class-Path: . libs/vehicle.model-0.0.1-20210203.101620-3.jar libs/commons-cli-1.4.jar
Specification-Title: vehicle.cli
Specification-Version: 0.0
Implementation-Title: vehicle.cli
Implementation-Version: 0.0.1-SNAPSHOT
Main-Class: org.vehicle.cli.Main
What makes me confused is in the line for ClassPath, it's calling the jar file named libs/vehicle.model-0.0.1-20210203.101620-3.jar while this jar file is not existing. What makes me confused is in the line for ClassPath, it's calling the jar file named libs/vehicle.model-0.0.1-20210203.101620-3.jar while this jar file is not existing. I expect it should call the jar file with name: libs/vehicle.model-0.0.1-SNAPSHOT.jar, because it is the one that was copied to the libs folder by the maven-dependency-plugin.我希望它应该调用 jar 文件,其名称为:libs/vehicle.model-0.0.1-SNAPSHOT.jar,因为它是由 maven-dependency-plugin 复制到 libs 文件夹的文件。 I dont understand where is the version -0.0.1-20210203.101620-3 coming from.我不明白版本-0.0.1-20210203.101620-3来自哪里。
Can anyone explain me what is the problem here and how to fix it?谁能解释一下这里有什么问题以及如何解决? Thank you!谢谢!
We have run into something like this before.我们以前遇到过这样的事情。 There is a bug reported for the jar plugin that was not fixed. jar 插件报告了一个未修复的错误。 Luckily, there is a workaround.幸运的是,有一个解决方法。 Adjust the manifest in the jar
plugin like this:调整jar
插件中的清单,如下所示:
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>libs/</classpathPrefix>
<mainClass>org.vehicle.cli.Main</mainClass>
<useUniqueVersions>false</useUniqueVersions> <!-- important! -->
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
If that isn't enough, the dependency plugin has a similar <useBaseVersion>
property that may be enabled.如果这还不够, 依赖插件有一个类似的<useBaseVersion>
属性可以启用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.