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

Maven生成仅含编译类的Jar包,排除所有依赖问题求助

How to Generate a Maven Jar Without Including Local System Dependencies

Hey there! No worries at all—we all fumble through Maven's quirks when we're starting out 😊 Let's break down why your jar is including those dependencies and how to fix it.

The Root Cause

Looking at your pom.xml, the issue isn't actually with the system scope dependencies themselves—Maven doesn't package system-scope dependencies into your project jar by default. The problem is in your resources configuration:

Your resources are set to include everything under src/main/resources (and subdirectories) except for direct files in those root directories. But your local jars are stored in src/main/resources/Details/Lib/, which is a subdirectory that isn't being excluded. So Maven is treating those jars as resource files and packaging them into your final jar.

Fix 1: Adjust Your Resources Exclusions

Update your resources section to explicitly exclude the Lib directory (or just the jar files inside it) so they don't get packaged as resources:

<resources>
  <resource>
    <directory>src/main/resources</directory>
    <excludes>
      <exclude>*.*</exclude>
      <exclude>Details/Lib/**</exclude> <!-- Exclude all files/subdirs under Lib -->
    </excludes>
    <filtering>true</filtering>
  </resource>
  <resource>
    <directory>src/main/resources/Details</directory>
    <excludes>
      <exclude>*.*</exclude>
      <exclude>Lib/**</exclude> <!-- Also exclude Lib here since this directory is targeted -->
    </excludes>
    <filtering>true</filtering>
  </resource>
  <resource>
    <directory>src/main/resources/Details/config</directory>
    <excludes>
      <exclude>*.*</exclude>
    </excludes>
    <filtering>true</filtering>
  </resource>
</resources>

Or if you only want to exclude jar files specifically:

<exclude>Details/Lib/*.jar</exclude>

After making this change, run mvn clean install again—your jar should now only contain your compiled Java classes and the resources you want (like config files), no dependencies.

Bonus: A Better Alternative to System Scope

While system scope works, it's not the most maintainable approach for local jars. A cleaner solution is to install these jars into your local Maven repository using the install:install-file command. For each jar, run:

mvn install:install-file -Dfile=${project.basedir}/src/main/resources/Details/Lib/jar1Name.jar -DgroupId=jar1 -DartifactId=dependency1 -Dversion=0.9.5 -Dpackaging=jar

Once installed, you can update your dependency entries to remove the scope and systemPath lines, just like any regular Maven dependency:

<dependency>
  <groupId>jar1</groupId>
  <artifactId>dependency1</artifactId>
  <version>0.9.5</version>
</dependency>

This keeps your pom.xml cleaner, avoids resource directory clutter, and makes your project more portable (other developers won't have to manually set up system paths).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 06:34:42