Sub topic 1
Building with Maven or Gradle and the Spring Boot plugin
Prerequisites
This chapter assumes you have a JDK installed (17 or later), have built a simple Spring Boot application with Maven or Gradle, and are comfortable running commands in a terminal. You should also know that a JAR is a ZIP archive of compiled Java classes and resources.
What the plugin adds
Maven and Gradle compile your code and package it into a plain JAR. A plain JAR contains only your own classes. It does not contain the libraries your application depends on, so java -jar cannot start it.
The Spring Boot plugin changes the packaging step. It takes the output of the normal build and repackages it into an executable JAR. This JAR also holds your dependencies and a small launcher that knows how to load them. The next chapter looks inside that file. Here the goal is to get the build configured and producing it reliably.
The plugin also does two other jobs. It imports a curated set of dependency versions, so you can omit version numbers for Spring libraries and their common dependencies. It also adds tasks for running the application and building container images, which later chapters use.
Configuring the build
In Maven, the project inherits from spring-boot-starter-parent and declares the plugin. The parent supplies the version management and plugin defaults.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.5</version>
</parent>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>In Gradle, you apply the org.springframework.boot plugin. The io.spring.dependency-management plugin supplies the version management.
plugins {
id 'java'
id 'org.springframework.boot' version '3.3.5'
id 'io.spring.dependency-management' version '1.1.6'
}
java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}Building the artifact
Use the wrapper scripts (mvnw and gradlew) so every developer and the CI server use the same build tool version. Run clean first so stale files from earlier builds cannot end up in the package.
# Maven
./mvnw clean package
# Gradle
./gradlew clean bootJar
# Run the result
java -jar target/myapp-1.0.0.jar --spring.profiles.active=prodMaven writes the JAR to target/. Gradle writes it to build/libs/. In both cases the last command starts the application. The --spring.profiles.active=prod argument selects a configuration profile, a topic covered in a later chapter.
How the pieces fit
Common errors
- "no main manifest attribute": you ran the plain JAR. Make sure the Spring Boot plugin is applied and that you built with
packageorbootJar. - Version conflicts: you pinned a dependency version that differs from the one Spring Boot manages. Remove your version unless you have a reason to override it.
- Wrong Java version: the JAR was compiled for a newer Java than the runtime. Set the Java version in the build and use the same major version to run it.
- Tests skipped or failing silently:
packageruns tests. Skipping them with-DskipTestshides problems, so use it only for local experiments, never in your release build.
Exercise
You build a Gradle project and find two JAR files in build/libs, one ending in -plain.jar. Which one do you run with java -jar, and why?
Show answer
Run the JAR without the -plain suffix. It is the executable JAR produced by bootJar, which includes dependencies and the launcher. The -plain JAR holds only your own classes, so it fails with a missing main manifest or missing class errors.
Exercise
Why does the Spring Boot build declare no version for most Spring dependencies?
Show answer
The plugin setup (the starter parent in Maven, or the dependency-management plugin in Gradle) imports a curated set of dependency versions that are tested to work together, so the build takes the versions from that set.