如何使用 Maven 依赖项和 jar 插件将文件夹附加到每个类路径条目?

How to append folder to each classpath entry using maven dependency and jar plugin?

我正在尝试为清单文件中的所有类路径条目添加文件夹。

正如您在下面的 pom 文件中看到的,我正在使用 jar 插件将类路径条目添加到清单中。

使用依赖插件,我将依赖项复制到 ${project.build.directory}/${finalName}-lib 文件夹。

我想将这些依赖项保留在该文件夹中并更改类路径条目以匹配 ${finalName}-lib 文件夹路径。

pom:

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.2.0</version>
            <configuration>
                <finalName>${jarpluginoutput}</finalName>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>desktop.core.Main</mainClass>
                    </manifest>
                </archive>
                <excludes>
                    <exclude>log4j2-test.properties</exclude>
                    <exclude>**/.keep</exclude>
                </excludes>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>3.2.0</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <includeScope>compile</includeScope>
                        <outputDirectory>${project.build.directory}/${finalName}-lib</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>

解决方案是通过添加 <classPathPrefix>.

         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.2.0</version>
            <configuration>
                <finalName>${jarpluginoutput}</finalName>
                <archive>
                    <manifest>
                        <classpathPrefix>${finalName}-lib</classpathPrefix>
                        <addClasspath>true</addClasspath>
                        <mainClass>desktop.core.Main</mainClass>
                    </manifest>
                </archive>
                <excludes>
                    <exclude>log4j2-test.properties</exclude>
                    <exclude>**/.keep</exclude>
                </excludes>
            </configuration>
        </plugin>