通过 maven pom.xml 在 java manifest 'Classpath' 中添加自定义字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7007561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:14:24  来源:igfitidea点击:

Adding custom string in java manifest 'Classpath' via maven pom.xml

javamavenexecutable-jarpom.xml

提问by Ashish Sharma

I am creating a manifest file for my java jar via following pom.xml directives:

我正在通过以下 pom.xml 指令为我的 java jar 创建一个清单文件:

    <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                         <mainClass>parser.BulkParser</mainClass>
                         <classpathPrefix>dependency/</classpathPrefix>
                    </manifest>
                </archive>
            </configuration>                
        </plugin>
    </plugins>
</build>

this results in following kind of manifest to be generated:

这会导致生成以下类型的清单:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: shaashis
Build-Jdk: 1.6.0_21
Main-Class: parser.BulkParser
Class-Path: dependency/commons-configuration-1.6.jar dependency/commons-collections-3.2.1.jar dependency/commons-lang-2.4.jar

Here I want to add a string of following type in the Class-Path:

在这里,我想在 Class-Path 中添加以下类型的字符串:

Class-Path: conf/ dependency/commons-configuration-1.6.jar dependency/commons-collections-3.2.1.jar dependency/commons-lang-2.4.jar

How can I do that via my pom.xml?

我怎样才能通过我的 pom.xml 做到这一点?

thanks

谢谢

Ashish

阿西什

回答by Michael-O

Altering The Classpath: Using a Custom Classpath Formatis the way to go.

改变类路径:使用自定义类路径格式是要走的路。

Edit: The above does not exactly what is desired. I have found a way to achive that by examining the Archiver source code. This will do (just verified in the shell):

编辑:以上并不完全符合要求。我找到了一种通过检查 Archiver 源代码来实现这一目标的方法。这将执行(仅在 shell 中验证):

<plugin>
  <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifestEntries>
                <Class-Path>conf/</Class-Path>
            </manifestEntries>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
            </manifest>
        </archive>
    </configuration>
</plugin>

回答by Ashish Sharma

Modified my pom.xml as follows to get the correct solution to my problem:

修改我的 pom.xml 如下以获得我的问题的正确解决方案:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>parser.BulkParser</mainClass>
                        <classpathPrefix>dependency/</classpathPrefix>
                    </manifest>
                    <manifestEntries>
                        <Class-Path>conf/</Class-Path>
                    </manifestEntries>
                </archive>
            </configuration>
        </plugin>

this resulted in generating the value of 'Class-Path' in the manifest file as I wanted.

这导致按照我的需要在清单文件中生成“Class-Path”的值。

References:

参考:

Manifest Entries

清单条目

Maven Archiver

Maven 存档器