我正在使用Maven和Maven-jar-plugin开发Java项目我已经使用maven jar插件为MANIFEST.MF中的jar文件生成了依赖项的类路径。这是我的插件用法:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
<manifestEntries>
<Build-Time>${maven.build.timestamp}</Build-Time>
</manifestEntries>
<manifestSections>
<manifestSection>
<name>${project.name}</name>
<manifestEntries>
<git-branch>${git.branch}</git-branch>
<git-build-host>${git.build.host}</git-build-host>
<git-build-time>${git.build.time}</git-build-time>
<git-build-user-email>git.build.user.email</git-build-user-email>
<git-build-user-name>git.build.user.name</git-build-user-name>
<git-build-version>${git.build.version}</git-build-version>
</manifestEntries>
</manifestSection>
</manifestSections>
</archive>
</configuration>
</plugin
并且生成的类路径是这样的:
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Build-Time: 2020-01-23T13:34:14Z
Class-Path: org/apache/logging/log4j/log4j-api-2.6.2.jar
org/apache/logging/log4j/log4j-core-2.6.2.jar
org/apache/commons/commons-configuration2-2.2.jar org/apache/commons/commons-lang3-3.6.jar
commons-loggin g/commons-logging-1.2.jar
commons-beanutils/commons-beanutils-1.9.3.jar
commons-collections/commons-collections-3.2.2.jar
Created-By: Apache Maven 3.6.2
Build-Jdk: 1.8.0_181
Name: service_utils
git-commit-id-abbrev: d78e66c
但是我确实需要使用相对路径生成类路径,而不是通过lib或WEB-INF的路径。我希望有这样的东西:
Manifest-Version: 1.0
Class-Path:
../../../org/hibernate/hibernate-core/5.1.0.Final/hibernate-core-5.1.0.Final.jar ../artifacts/org/hibernate/hibernate-core/5.1.0.Final/hibernate-core-5.1.0.Final.jar ../../../org/hibernate/common/hibernate-commons-annotations/5.0.1.Final/hibernate-commons-annotations-.0.1.Final.jar
../artifacts/org/hibernate/common/hibernat e-commons-annotations/5.0.1.Final/hibernate-commons-annotations-5.0.1 .Final.jar
../../org/jboss/logging/jboss-logging/3.3.0.Final/jboss-logging-3.3.0.Final.jar
../artifacts/org/jboss/logging/jboss-logging/3.3.0.Final/jboss-logging-3.3.0.Final.jar
../../../com/mchange/c3p0/ 0.9.2.1/c3p0-0.9.2.1.jar
我已经搜索并研究了很多StackOverflow有关在清单文件中包含清单文件及其相对路径classpath的问题,我已经看到gradle的选项,但我正在寻找maven解决方案,如果可以使用maven jar插件执行此操作,那就更好了。
我找到了一种获取相对地址的方法,但是深度是经过硬编码的,对于多模块项目,它不是一个完美的解决方案。
<manifest>
<addClasspath>true</addClasspath>
<mainClass>My.Main</mainClass>
<classpathLayoutType>custom</classpathLayoutType>
<customClasspathLayout>../../../$${artifact.groupIdPath}/$${artifact.artifactId}-$${artifact.version}$${dashClassifier?}.$${artifact.extension} ../artifacts/$${artifact.groupIdPath}/$${artifact.artifactId}-$${artifact.version}$${dashClassifier?}.$${artifact.extension}</customClasspathLayout>
</manifest>