Maven - 如何在jar中添加任意类路径条目?

Ken*_*Liu 62 maven-2 jar build classpath

我有一个不寻常的情况,我需要在可执行jar的清单中添加一个任意的类路径条目(指向一个jar文件).(这适用于Swing桌面应用程序.)

maven-jar-plugin使用maven依赖项为jar清单生成"Class-Path"条目,并且似乎没有任何添加任意条目的方法.

我还看了使用"-classpath"参数将任意类路径条目硬编码到启动应用程序的批处理文件中,但我无法弄清楚如何让Maven将类路径过滤到批处理文件中.

小智 72

我发现这个问题有一个简单的解决方案.您可以<Class-Path><manifestEntries>元素添加元素,并设置<addClassPath>true</addClassPath><manifest>元素.因此,<Class-Path>元素的值会自动添加到类路径中.例:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <configuration>
    <archive>
      <manifest>
        <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
        <addClasspath>true</addClasspath>
        <mainClass>your.main.Class</mainClass>
      </manifest>
      <manifestEntries>
        <Class-Path>../conf/</Class-Path>
      </manifestEntries>
    </archive>
  </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

  • **当心**:这种方法与JAR索引不兼容!有关更多信息,请参见http://stackoverflow.com/a/10437177/14731. (8认同)

Ric*_*ler 18

更新:以下是如何将类路径过滤到自定义清单中.

可以将maven-dependency-plugin的build-classpath目标配置为以类型格式(即classpath = [classpath])将类路径输出到文件.然后,配置filters元素以使用生成的类路径文件,并配置要过滤的资源目录.

例如:

<build>
  <plugins>
    <plugin>
      <artifactId>maven-dependency-plugin</artifactId>
      <version>2.1</version>
      <executions>
        <execution>
          <phase>generate-resources</phase>
          <goals>
            <goal>build-classpath</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <outputFilterFile>true</outputFilterFile>
        <outputFile>${project.build.directory}/classpath.properties</outputFile>
      </configuration>
    </plugin>
    <plugin>
      <artifactId>maven-jar-plugin</artifactId>
      <configuration>
        <archive>
          <manifestFile>
            ${project.build.outputDirectory}/META-INF/MANIFEST.MF
          </manifestFile>
        </archive>
      </configuration>
    </plugin>
  </plugins>
  <filters>
    <filter>${project.build.directory}/classpath.properties</filter>
  </filters>
  <resources>
    <resource>
      <directory>src/main/resources</directory>
      <filtering>true</filtering>
    </resource>
  </resources>
</build>
Run Code Online (Sandbox Code Playgroud)

然后在src/main/resources/META-INF/Manifest.MF中指定以下内容:

Bundle-Version: 4.0.0
...
Classpath: ${classpath};[specify additional entries here]
Run Code Online (Sandbox Code Playgroud)

注意:使用标准窗口路径分隔符(\)时,此处理存在一个错误,生成路径被剥离了分隔符(注意它在Linux上运行正常).您可以通过<fileSeparator>\\\\</fileSeparator>build-classpath目标的配置中指定,为Windows正确生成类路径.


您可以在jar-plugin的配置中自定义清单.为此,你需要在你的pom上添加这样的东西.

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  ...
  <configuration>
    <archive>
      <index>true</index>
      <manifest>
        <addClasspath>true</addClasspath>
      </manifest>
      <manifestEntries>
        <mode>development</mode>
        <url>${pom.url}</url>
        <key>value</key>
      </manifestEntries>
    </archive>
  </configuration>
  ...
</plugin>
Run Code Online (Sandbox Code Playgroud)

完整的归档规范提供了很多选项.有关配置类路径的选项,请参阅示例页面.

如果这些都不适合您,您可以定义自己的Manifest,设置包含所需条目的属性,并使用过滤器使用这些属性填充清单


Foy*_*yta 5

尝试像在这个bug中那样做,即使用manifestEntries/Class-Path元素合并条目

https://issues.apache.org/jira/browse/MJAR-41