如何将类路径添加到maven

How to add classpath to maven

我想创建一个可执行 jar,其依赖项将位于文件夹 lib 中的存档之外。我发现我可以要求 Maven 将依赖项复制到文件夹中并修改我的可执行文件中的清单文件以了解在哪里寻找所需的 classes。问题是它没有向我的清单添加任何 class 路径。 庞:

<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.sav</groupId>
    <artifactId>console-app-01</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <project.build.sourceEncoding>utf-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>utf-8</project.reporting.outputEncoding>
    </properties>

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

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>org.sav.app.App</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>com.kenai.nbpwr</groupId>
            <artifactId>joda-time</artifactId>
            <version>1.6.2-201110292322</version>
        </dependency>
    </dependencies>

</project>

在 运行ning mvn install 之后,我的清单如下所示:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven 3.8.3
Built-By: abigo
Build-Jdk: 11.0.12
Main-Class: org.sav.app.App

但不会 运行。它告诉我找不到需要的 classes。一旦我手动添加 class-path 它 运行s 没有问题

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven 3.8.3
Built-By: abigo
Build-Jdk: 11.0.12
Main-Class: org.sav.app.App
Class-Path: lib/joda-time-1.6.2.jar lib/joda-time-1.6.2-201110292322.jar

申请:

package org.sav.app;

import org.joda.time.LocalTime;

public class App {

    public static void main(String[] args) {
        LocalTime currentTime = new LocalTime();
        System.out.printf("Current time is: %s%n", currentTime);
    }

}

使用旧版本的 maven,一切正常。

我现在遇到了同样的麻烦,除了降级maven版本我找不到其他解决方法。我用的是maven 3.8.3,我要下载3.6