0
0
SpringbootHow-ToBeginner · 3 min read

How to Create a JAR File in Spring Boot Easily

To create a jar file for a Spring Boot application, use the build tool like Maven or Gradle with the Spring Boot plugin configured. Running mvn clean package or ./gradlew bootJar will generate an executable jar file in the target or build/libs folder.
📐

Syntax

Use the Spring Boot plugin in your build tool configuration to package your app as a runnable jar. For Maven, the command is mvn clean package. For Gradle, use ./gradlew bootJar.

The generated jar includes all dependencies and can be run with java -jar yourapp.jar.

bash
mvn clean package

# or for Gradle
./gradlew bootJar
💻

Example

This example shows a simple Spring Boot project using Maven. The pom.xml includes the Spring Boot starter parent and plugin. Running mvn clean package creates target/demo-0.0.1-SNAPSHOT.jar which you can run directly.

xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
Output
Building jar: target/demo-0.0.1-SNAPSHOT.jar Run with: java -jar target/demo-0.0.1-SNAPSHOT.jar
⚠️

Common Pitfalls

  • Not including the Spring Boot Maven or Gradle plugin will create a plain jar without dependencies, causing runtime errors.
  • Running mvn package without clean may keep old files; use mvn clean package to avoid this.
  • For Gradle, using jar task instead of bootJar creates a non-executable jar.
xml
Wrong (Maven without plugin):

<build>
  <plugins>
    <!-- Missing spring-boot-maven-plugin -->
  </plugins>
</build>

Right (Maven with plugin):

<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
  </plugins>
</build>
📊

Quick Reference

Maven commands:

  • mvn clean package - Build executable jar
  • java -jar target/yourapp.jar - Run jar

Gradle commands:

  • ./gradlew bootJar - Build executable jar
  • java -jar build/libs/yourapp.jar - Run jar

Key Takeaways

Use the Spring Boot Maven or Gradle plugin to create an executable jar with dependencies included.
Run 'mvn clean package' or './gradlew bootJar' to build the jar file.
Always run the jar with 'java -jar yourapp.jar' to start your Spring Boot app.
Avoid missing the Spring Boot plugin or using the wrong build task to prevent runtime errors.
Clean your build before packaging to ensure no stale files remain.