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

IntelliJ构建可执行Jar忽略Class-Path,如何解决LIST.MF引发的问题?

解决可执行Jar忽略Class-Path且生成META-INF/LIST.MF的问题

这问题我之前帮人排查过好几次,LIST.MF大概率是maven-shade-plugin搞出来的——哪怕你pom里没直接写,也可能是父依赖、项目继承的pom,甚至IntelliJ默认的构建逻辑偷偷启用了它!下面给你一步步排查和解决的方案:

1. 先检查IntelliJ的Artifact构建配置

打开IntelliJ的 File > Project Structure > Artifacts,看看你创建的Jar artifact是不是用了以下模式:

  • 选择了「From modules with dependencies」
  • 并且勾选了「Extract to the target JAR」(这种就是Shade打包模式,会自动生成LIST.MF)

如果是,改成「Copy to the output directory and link via manifest」模式,这样打包时不会合并依赖,而是保留正常的Class-Path配置,也不会生成LIST.MF。

2. 排查Maven插件的隐性引入

如果Artifact配置没问题,那得确认Shade插件是不是被间接启用了:

  • 打开终端,在项目根目录执行命令:
    mvn help:describe -Dplugin=shade -Ddetail
    
    如果输出里显示插件是active状态,说明肯定有地方引入了它(比如父pom、依赖的第三方pom)。

这时候你可以在自己的pom里显式禁用Shade插件,或者修改它的配置避免生成LIST.MF:

<build>
  <plugins>
    <!-- 方案一:直接禁用默认的Shade执行 -->
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-shade-plugin</artifactId>
      <version>3.4.1</version>
      <executions>
        <execution>
          <id>default-shade</id>
          <phase>none</phase> <!-- 让插件不在打包阶段运行 -->
        </execution>
      </executions>
    </plugin>

    <!-- 方案二:如果必须用Shade,配置它不生成LIST.MF -->
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-shade-plugin</artifactId>
      <version>3.4.1</version>
      <configuration>
        <createDependencyReducedPom>false</createDependencyReducedPom> <!-- 关闭依赖精简Pom,避免生成LIST.MF -->
        <transformers>
          <!-- 维护你的主类和Class-Path配置 -->
          <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
            <mainClass>com.yourpackage.YourMainClass</mainClass>
            <manifestEntries>
              <Class-Path>your/dependency/path.jar</Class-Path>
            </manifestEntries>
          </transformer>
          <!-- 显式排除LIST.MF的生成 -->
          <transformer implementation="org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer">
            <resource>META-INF/LIST.MF</resource>
          </transformer>
        </transformers>
      </configuration>
    </plugin>
  </plugins>
</build>

3. 换用更稳妥的maven-assembly-plugin

如果不想折腾Shade插件,推荐用maven-assembly-plugin构建可执行Jar,它不会生成LIST.MF,还能自动维护Class-Path:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-assembly-plugin</artifactId>
      <version>3.6.0</version>
      <configuration>
        <archive>
          <manifest>
            <mainClass>com.yourpackage.YourMainClass</mainClass>
            <addClasspath>true</addClasspath> <!-- 自动添加依赖到Class-Path -->
            <classpathPrefix>lib/</classpathPrefix> <!-- 依赖会被放到Jar同目录的lib文件夹下 -->
          </manifest>
        </archive>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
      <executions>
        <execution>
          <id>make-assembly</id>
          <phase>package</phase> <!-- 在打包阶段执行 -->
          <goals>
            <goal>single</goal> <!-- 生成单个可执行Jar -->
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

4. 清理缓存和旧构建文件

最后别忘了做个彻底清理:

  • 执行 mvn clean 清空target目录
  • 打开IntelliJ的 File > Invalidate Caches... > Invalidate and Restart,清除IDE缓存
  • 重新执行构建命令

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 08:19:46