You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Maven将指定依赖纳入INDEX.LIST文件

Great question! The workaround with maven-antrun-plugin works, but it's definitely clunky. Here's a clean, pure Maven solution that avoids unpacking/repackaging the JAR and doesn't require Ant tools:

Core Idea

Instead of letting maven-jar-plugin auto-generate the INDEX.LIST with all compile-scope dependencies, we'll:

  • Disable automatic index generation in maven-jar-plugin
  • Use maven-dependency-plugin to generate a custom INDEX.LIST containing only your target dependencies
  • Let the JAR plugin include this pre-generated file in the final artifact

Full Configuration Example

First, update your maven-jar-plugin to turn off auto-indexing:

<plugin>
  <artifactId>maven-jar-plugin</artifactId>
  <configuration>
    <archive>
      <addMavenDescriptor>false</addMavenDescriptor>
      <index>false</index> <!-- Disable auto-generated INDEX.LIST -->
      <manifest>
        <addClasspath>true</addClasspath>
      </manifest>
      <manifestEntries>
        <!-- Your existing manifest entries -->
      </manifestEntries>
    </archive>
    <excludes>
      <!-- Your existing excludes -->
    </excludes>
  </configuration>
</plugin>

Next, add maven-dependency-plugin to generate your custom INDEX.LIST at the right phase:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <version>3.6.0</version>
  <executions>
    <execution>
      <id>generate-custom-index-list</id>
      <phase>prepare-package</phase> <!-- Runs right before JAR packaging -->
      <goals>
        <goal>list</goal>
      </goals>
      <configuration>
        <!-- Output directly to the classes directory so it gets packaged -->
        <outputFile>${project.build.directory}/classes/META-INF/INDEX.LIST</outputFile>
        <!-- Filter dependencies: adjust these to match your target set -->
        <includeGroupIds>com.yourcompany,org.somelibrary</includeGroupIds>
        <includeArtifactIds>dependency-one,dependency-two,dependency-three</includeArtifactIds>
        <!-- Keep scope as compile to match your requirement -->
        <scope>compile</scope>
        <!-- Use the built-in format for INDEX.LIST -->
        <outputFormat>index-list</outputFormat>
        <!-- Optional: exclude specific dependencies if needed -->
        <!-- <excludeGroupIds>com.unwanted.group</excludeGroupIds> -->
        <!-- <excludeArtifactIds>unwanted-dependency</excludeArtifactIds> -->
      </configuration>
    </execution>
  </executions>
</plugin>

How This Works

  • Disabling auto-indexing: Setting <index>false</index> tells maven-jar-plugin not to generate the default INDEX.LIST with all compile dependencies.
  • Generating custom INDEX.LIST: The maven-dependency-plugin's list goal with outputFormat=index-list creates a properly formatted INDEX.LIST file. We output it directly to ${project.build.directory}/classes/META-INF/ so it gets included in the final JAR automatically.
  • Dependency filtering: Use <includeGroupIds>/<includeArtifactIds> to specify exactly which compile-scope dependencies you want in the index. You can also use exclusions if you need to omit specific dependencies from the allowed set.

This approach is cleaner, faster, and fully integrated with Maven's lifecycle—no messy unpacking/repackaging steps required.

内容的提问来源于stack exchange,提问作者Re Viki

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 18:32:31