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

Maven构建的Jar未包含依赖包问题求助

Hey there! The problem you're hitting is really common—when you run mvn package by default, Maven only packages your project's own code into the JAR file, not the external dependencies like Gson or your IO libraries. JUnit is a test dependency, so you probably don't need it in your runtime JAR anyway, but let's fix those missing runtime dependencies first.

Here are two reliable ways to configure Maven to package all required dependencies into your JAR:

1. Use the Maven Assembly Plugin

This plugin creates a separate "fat JAR" that includes all your project code plus its dependencies. It's straightforward and great for simple projects.

Add this configuration to your pom.xml inside the <build><plugins> section:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.6.0</version>
    <configuration>
        <!-- This descriptor tells Maven to bundle all dependencies into the JAR -->
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <!-- Omit the archive/manifest section below since you can't export a runnable JAR -->
        <!-- <archive>
            <manifest>
                <mainClass>com.your.package.YourMainClass</mainClass>
            </manifest>
        </archive> -->
    </configuration>
    <executions>
        <!-- Bind the plugin to the package phase so it runs automatically with `mvn package` -->
        <execution>
            <id>assemble-all</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

How to use it:

  • Run mvn clean package (either via Eclipse's "Run as -> Maven build..." with target clean package, or from the command line).
  • Check your target folder—you'll see two JARs: your regular project JAR, and another one named [your-project-name]-jar-with-dependencies.jar.
  • Use this -jar-with-dependencies JAR when running your code, and it won't throw missing dependency errors.

2. Use the Maven Shade Plugin

This is a more powerful option that merges dependency classes into your main JAR (instead of creating a separate one) and can handle things like classpath conflicts. It's better for more complex projects.

Add this to your pom.xml's <build><plugins> section:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.5.1</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <!-- Omit the transformer section if you don't need an executable JAR -->
                <!-- <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>com.your.package.YourMainClass</mainClass>
                    </transformer>
                </transformers> -->
                <!-- Optional: Exclude JUnit and other test dependencies to keep the JAR small -->
                <filters>
                    <filter>
                        <artifact>*:*</artifact>
                        <excludes>
                            <exclude>junit:*</exclude>
                            <exclude>org.junit:*</exclude>
                        </excludes>
                    </filter>
                </filters>
            </configuration>
        </execution>
    </executions>
</plugin>

How to use it:

  • Run mvn clean package as before.
  • The main JAR in your target folder will now include all your runtime dependencies (minus test ones like JUnit, if you added the filter). Just run this JAR directly.

Quick Notes:

  • Double-check your dependency scopes: Gson and IO libraries should be set to compile (the default scope) so Maven recognizes them as runtime-required. JUnit should stay as test scope since it's only for testing.
  • Always run mvn clean before packaging to remove old build files and avoid conflicts.
  • In Eclipse, make sure your Maven project is updated after modifying the pom.xml (right-click project -> Maven -> Update Project) to apply the new plugin configurations.

内容的提问来源于stack exchange,提问作者Carlos López Marín

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 08:02:31