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

时间:2016-09-13 13:41:59

标签: xml maven maven-3 maven-jar-plugin

使用maven-jar-plugin时:是否可以在Manifest中创建一个与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>

1 个答案:

答案 0 :(得分:1)

这就是我实现它的方式:

<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目标定义空格或空字符(或者至少我不能轻易制作它),因此应用了几个正则表达式来实现它最终的$ {classpath.entry.final}属性,它将包含项目类路径,然后可以用作Manifest条目的占位符。

另请注意正则表达式xml:space="preserve"replacement属性的使用情况,如果没有它,则无法将空格应用为文件分隔符。