【问题标题】:How to list dependencies with their relative path in class path of Manifest file of generated jar file using maven jar plugin?如何使用 maven jar 插件在生成的 jar 文件的清单文件的类路径中列出依赖项及其相对路径?
【发布时间】:2020-05-09 21:01:29
【问题描述】:

我正在使用 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

而生成的classpath是这样的:

 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

但我确实需要使用相对路径而不是通过libWEB-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 的许多关于在 jar 文件中使用它们的相对路径类路径的清单文件的问题,我已经看到了 gradle 的选项,但我正在寻找 maven 解决方案,如果我可以使用 maven jar 执行此操作会更好插件。

【问题讨论】:

    标签: maven jar maven-plugin manifest.mf maven-jar-plugin


    【解决方案1】:

    我找到了一种获取相对地址的方法,但深度是硬编码的,对于多模块项目来说,这不是一个完美的解决方案。

    <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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 2016-09-21
      • 2017-12-09
      • 1970-01-01
      • 2012-03-23
      • 2014-11-18
      相关资源
      最近更新 更多