如何通过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-pluginto generate a customINDEX.LISTcontaining 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>tellsmaven-jar-pluginnot to generate the defaultINDEX.LISTwith all compile dependencies. - Generating custom INDEX.LIST: The
maven-dependency-plugin'slistgoal withoutputFormat=index-listcreates a properly formattedINDEX.LISTfile. 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
相关产品推荐
相关产品推荐

