是否可以在清单中更改(或添加)自定义键控 Class 路径条目?

Is it possible to change (or add) a custom keyed Class-path entry in the Manifest?

在使用 maven-jar-plugin 时:是否可以在清单中创建一个内容与 Class-Path 相同的自定义类路径键条目?例如在这种情况下 Cluster-Dependencies:

Manifest-Version: 1.0
Class-Path: scala-library-2.10.6.jar scalatest_2.10-3.0.0.jar
Cluster-Dependencies: scala-library-2.10.6.jar scalatest_2.10-3.0.0.jar

以及 pom.xml

中的相关部分
<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <version>3.0.2</version>
      <configuration>
        <archive>
          <index>true</index>
          <manifest>
            <addClasspath>true</addClasspath>
          </manifest>
          <manifestEntries>
            <Cluster-Dependencies>???</Cluster-Dependencies>
          </manifestEntries>
        </archive>
      </configuration>
    </plugin>
  </plugins>
</build>

我是这样实现的:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.10</version>
    <executions>
        <execution>
            <id>unpack</id>
            <phase>validate</phase>
            <goals>
                <goal>build-classpath</goal>
            </goals>
            <configuration>
                <outputProperty>classpath.entry</outputProperty>
                <pathSeparator>;</pathSeparator>
                <prefix>:</prefix>
                <fileSeparator>_</fileSeparator>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.10</version>
    <executions>
        <execution>
            <id>regex-property</id>
            <goals>
                <goal>regex-property</goal>
            </goals>
            <configuration>
                <name>classpath.entry.tmp</name>
                <value>${classpath.entry}</value>
                <regex>_</regex>
                <replacement xml:space="preserve"> </replacement>
                <failIfNoMatch>false</failIfNoMatch>
            </configuration>
        </execution>
        <execution>
            <id>regex-property2</id>
            <goals>
                <goal>regex-property</goal>
            </goals>
            <configuration>
                <name>classpath.entry.tmp2</name>
                <value>${classpath.entry.tmp}</value>
                <regex>;</regex>
                <replacement></replacement>
                <failIfNoMatch>false</failIfNoMatch>
            </configuration>
        </execution>
        <execution>
            <id>regex-property3</id>
            <goals>
                <goal>regex-property</goal>
            </goals>
            <configuration>
                <name>classpath.entry.final</name>
                <value>${classpath.entry.tmp2}</value>
                <regex>:</regex>
                <replacement></replacement>
                <failIfNoMatch>false</failIfNoMatch>
            </configuration>
        </execution>                    
    </executions>
</plugin>
<plugin>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.0.2</version>
    <configuration>
        <archive>
            <index>true</index>
            <manifest>
                <addClasspath>true</addClasspath>
            </manifest>
            <manifestEntries>
                <Cluster-Dependencies>${classpath.entry.final}</Cluster-Dependencies>
            </manifestEntries>
        </archive>
    </configuration>
</plugin>       

它的冗长主要是由于不可能为 build-classpath 目标定义 space 或空字符(或者至少我不能轻易做到),因此应用了几个正则表达式在最终的 ${classpath.entry.final} 属性 上实现它,它将包含项目类路径,然后可以用作清单条目的占位符。

还要注意正则表达式 replacementxml:space="preserve" 属性的用法,没有它就无法应用 space 作为文件分隔符。