簡體   English   中英

maven-jar-plugin 生成帶有錯誤 MANIFEST.MF 的 jar

[英]maven-jar-plugin generates jar with wrong MANIFEST.MF

我想生成一個 jar 文件,包括它的依賴項。 這是我的 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>

依賴org.vehicle.model我從自定義的關系存儲庫中獲取它。 運行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:

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. 我希望它應該調用 jar 文件,其名稱為:libs/vehicle.model-0.0.1-SNAPSHOT.jar,因為它是由 maven-dependency-plugin 復制到 libs 文件夾的文件。 我不明白版本-0.0.1-20210203.101620-3來自哪里。

誰能解釋一下這里有什么問題以及如何解決? 謝謝!

我們以前遇到過這樣的事情。 jar 插件報告了一個未修復的錯誤。 幸運的是,有一個解決方法。 調整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>

如果這還不夠, 依賴插件有一個類似的<useBaseVersion>屬性可以啟用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM