如何将Spring Boot项目打包为含依赖的Jar包并作为其他Spring Boot应用依赖?
Absolutely feasible! The problem you're hitting is that Spring Boot's default packaging creates an executable JAR—a special format where third-party dependencies are tucked inside BOOT-INF/lib, making them unreadable by other applications when used as a dependency. Plus, the default setup only includes your custom code in the "plain" JAR (if it even generates one). Let's break down the solutions step by step.
Solution: Package Your Custom Code + Dependencies into a Reusable JAR
We'll cover both Maven and Gradle setups since you didn't specify your build tool:
For Maven Projects
- Adjust Spring Boot Maven Plugin Configuration
By default, thespring-boot-maven-plugingenerates only an executable JAR. Configure it to also produce a standard, non-executable JAR, then use themaven-assembly-pluginto bundle dependencies into a single "fat JAR":
<!-- pom.xml --> <build> <plugins> <!-- Generate a standard, dependency-free JAR + an executable JAR --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>your-spring-boot-version</version> <executions> <execution> <goals> <goal>repackage</goal> </goals> <configuration> <!-- Marks the executable JAR with an "exec" suffix --> <classifier>exec</classifier> </configuration> </execution> </executions> </plugin> <!-- Bundle your code + all dependencies into one JAR --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.6.0</version> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <!-- Optional: Customize the output JAR name --> <finalName>${project.artifactId}-${project.version}-with-deps</finalName> </configuration> </execution> </executions> </plugin> </plugins> </build>
Run mvn clean package, and you'll get three JARs in your target folder:
your-project-VERSION.jar: Standard JAR with only your custom codeyour-project-VERSION-exec.jar: Executable Spring Boot JARyour-project-VERSION-with-deps.jar: The JAR you need—contains your code + all dependencies
- Key Notes
- Watch out for dependency conflicts if you use this fat JAR in another Spring Boot app. For example, mismatched Spring Boot versions can cause runtime errors. Keep versions consistent between projects, or use
<exclusions>in the target app'spom.xmlto resolve conflicts. - Avoid publishing this fat JAR to a shared Maven repository—it will bloat the repo with duplicate dependency files. Instead, publish the standard JAR and let dependencies resolve via Maven's transitive mechanism.
For Gradle Projects
- Add a Custom Task to Build a Fat JAR
Update yourbuild.gradleto generate a JAR that includes both your code and dependencies:
plugins { id 'java' id 'org.springframework.boot' version 'your-spring-boot-version' id 'io.spring.dependency-management' version '1.1.0' } // Task to build a JAR with all dependencies included task fatJar(type: Jar) { archiveClassifier = 'with-deps' from sourceSets.main.output // Bundle all runtime dependencies into the JAR from { configurations.runtimeClasspath.findAll { it.name.endsWith('jar') }.collect { zipTree(it) } } // Handle duplicate files (e.g., META-INF resources) duplicatesStrategy = DuplicatesStrategy.EXCLUDE } // Ensure the fatJar runs during the build phase build.dependsOn fatJar
Run gradle clean build, and you'll find your-project-VERSION-with-deps.jar in the build/libs folder.
How to Create a Reusable JAR for Other Spring Boot Apps (Standard Approach)
If you don't need to bundle dependencies (the recommended approach to avoid conflicts), follow these steps to generate a standard, dependency-transitive JAR:
Maven Projects
- Configure the Spring Boot plugin to generate a standard JAR:
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>your-spring-boot-version</version> <executions> <execution> <goals> <goal>repackage</goal> </goals> <configuration> <classifier>exec</classifier> </configuration> </execution> </executions> </plugin>
- Install the JAR to your local Maven repo with
mvn clean install, then reference it in another Spring Boot app:
<dependency> <groupId>your-group-id</groupId> <artifactId>your-artifact-id</artifactId> <version>your-version</version> </dependency>
Gradle Projects
- Add the
maven-publishplugin to publish the JAR to your local repo:
plugins { // Existing plugins... id 'maven-publish' } publishing { publications { mavenJava(MavenPublication) { from components.java } } }
- Run
gradle publishToMavenLocal, then add the dependency to another Spring Boot app:
implementation 'your-group-id:your-artifact-id:your-version'
Pro Tips
- If you're only reusing utility or business classes, consider extracting them into a plain Java project (not a Spring Boot project) instead. This avoids pulling in unnecessary Spring Boot dependencies and reduces conflict risks.
- If you must use a Spring Boot project as a dependency, ensure both projects use the same Spring Boot version, and use
dependencyManagementin the target app to unify dependency versions.
内容的提问来源于stack exchange,提问作者Bravo




