Sub topic 1
Building with Maven or Gradle and the Spring Boot plugin
This chapter covers the first step of every deployment: turning source code into one runnable file. You will see what the Spring Boot build plugin adds to a normal Maven or Gradle build, how to configure it, and how to check and debug the output.
Prerequisites
This chapter assumes you have:
- A working JDK (this chapter uses 21) and
javaon your path. - Built a simple Spring Boot application with Maven or Gradle at least once.
- Basic command-line skills.
You do not need to know how the executable JAR is laid out internally. The next chapter covers that.
What the plugin adds to a normal build
A plain Java build produces a JAR that holds only your compiled classes. It has no dependencies inside it and no way to start a server. You would have to assemble a classpath by hand.
The Spring Boot plugin changes the last step of the build. It takes the JAR your build produced and rewrites it as an executable JAR. That JAR contains your classes, every dependency, and a launcher that knows how to start your application.
The build tool still does the rest: compiling, running tests and resolving dependencies. The plugin handles packaging and a few related tasks.
The plugin does three jobs:
- Dependency management. It supplies a Bill of Materials (BOM). A BOM is a list of tested, compatible versions for hundreds of libraries. This is why you can write
spring-boot-starter-webwithout a version number. - Packaging. It repackages the JAR, as described above.
- Convenience tasks. It adds goals for running the app, generating build metadata and building container images.
Maven setup
The simplest Maven setup uses spring-boot-starter-parent as the parent POM. The parent imports the BOM, sets the Java compiler options and configures the plugin version.
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.5.0</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>myapp</artifactId>
<version>1.0.0</version>
<properties>
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>The plugin entry has no version because the parent manages it. The plugin binds its repackage goal to the package phase, so mvn package produces the executable JAR automatically.
If your organization requires its own parent POM, you can't also use the Boot parent. In that case, import the BOM in dependencyManagement and configure the plugin yourself:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.5.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>3.5.0</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</dependencies>
</build>Without the parent, you must state the plugin version and the repackage execution explicitly. Forgetting the execution is a common reason for getting a non-executable JAR.
Gradle setup
In Gradle, two plugins do the work. org.springframework.boot adds the packaging tasks. io.spring.dependency-management applies the BOM.
plugins {
id 'java'
id 'org.springframework.boot' version '3.5.0'
id 'io.spring.dependency-management' version '1.1.7'
}
group = 'com.example'
version = '1.0.0'
java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
tasks.named('test') {
useJUnitPlatform()
}The java toolchain block tells Gradle which JDK to compile with, and Gradle can download a matching one if it is missing. The Boot plugin registers a bootJar task that builds the executable JAR. It also registers bootRun for running from the build tool.
Build commands
Always use the wrapper scripts, mvnw and gradlew, instead of a globally installed tool. The wrapper downloads the exact tool version recorded in the repository. That keeps your laptop and your CI server on the same version.
| Task | Maven | Gradle |
|---|---|---|
| Build and test | `./mvnw clean package` | `./gradlew clean build` |
| Build without tests | `./mvnw clean package -DskipTests` | `./gradlew bootJar` |
| Run from the build tool | `./mvnw spring-boot:run` | `./gradlew bootRun` |
| Output location | `target/myapp-1.0.0.jar` | `build/libs/myapp-1.0.0.jar` |
Gradle's bootJar depends on compilation but not on test. Use build when you want tests to run before packaging.
Setting the main class
The plugin searches your compiled classes for one class with a main method. If it finds none, or more than one, the build fails or produces a broken JAR. This happens often in projects that keep demo or utility classes with their own main methods.
Name the class explicitly. In Maven:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.example.myapp.Application</mainClass>
</configuration>
</plugin>In Gradle:
springBoot {
mainClass = 'com.example.myapp.Application'
}Adding build information
Operations teams often need to know which version is running. The plugin can write a META-INF/build-info.properties file into the JAR. Actuator then exposes it at /actuator/info, a feature a later chapter covers.
In Maven, add an execution to the plugin:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>In Gradle, add one block:
springBoot {
buildInfo()
}Inspecting the result
Do not assume the output is correct. List the JAR contents and check the manifest:
./mvnw clean package
ls -lh target/*.jar
unzip -p target/myapp-1.0.0.jar META-INF/MANIFEST.MF
unzip -l target/myapp-1.0.0.jar | grep -E "BOOT-INF/(classes|lib)" | headA correct executable JAR has a Main-Class pointing at a Spring Boot launcher and a Start-Class naming your application class. Your classes sit under BOOT-INF/classes, and dependencies sit under BOOT-INF/lib. A JAR that is only a few kilobytes and has no BOOT-INF directory was not repackaged.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `no main manifest attribute, in app.jar` | The plain JAR was run, or `repackage` never ran | Run the repackaged file. In a non-parent Maven setup, add the `repackage` execution. |
| Two JARs in `build/libs`, one ending in `-plain` | Gradle also builds the plain JAR | Run the one without `-plain`. To drop it, set `tasks.named('jar') { enabled = false }`. |
| A `.jar.original` file in `target` | Maven keeps the pre-repackage JAR | Ignore it. Don't deploy it. |
| `Unable to find main class` | No class or several classes with `main` | Set `mainClass` as shown above. |
| `UnsupportedClassVersionError` | The runtime JDK is older than the compile target | Run on the same or a newer Java version than `java.version`. |
| `Could not find artifact ...:jar:` for a starter with no version | Boot BOM isn't applied | Use the parent POM or import the BOM. In Gradle, apply the dependency-management plugin. |
Keeping builds reproducible
A few habits prevent "works on my machine" failures:
- Commit the wrapper files (
mvnw,.mvn/,gradlew,gradle/wrapper/). - Pin the Spring Boot version in one place. Upgrade it deliberately, not through a version range.
- Keep the artifact version meaningful. A release pipeline should produce
1.0.0, not an ambiguouslatest. - Don't put environment-specific values such as database URLs in files that are packaged into the JAR. The next chapters show how to supply them at run time instead.
Exercise
You run java -jar build/libs/myapp-1.0.0-plain.jar and get "no main manifest attribute". Why does this happen, and which file should you run?
Show answer
Gradle builds two JARs. The -plain JAR is the ordinary JAR created by the jar task. It holds only your classes and has no launcher or dependencies. Run build/libs/myapp-1.0.0.jar, which bootJar produced.
Exercise
Your company requires a custom parent POM, so you can't use spring-boot-starter-parent. Name the two things you must add to your pom.xml so that starters need no version and mvn package produces an executable JAR.
Show answer
Import spring-boot-dependencies as a BOM in dependencyManagement with <scope>import</scope> and <type>pom</type>. Then declare spring-boot-maven-plugin with a version and a repackage execution.