Handbooks / Spring Boot / Chapter 1
Build Tools and Project Setup
7 pages · ~5 min✓ Reviewed
Start here. Next up: Core: IoC and Dependency Injection.
Spring Initializr and the Boot Mental Model
1. What Boot Is
- Boot = Spring + auto-configuration + starters + embedded server
- Beans are configured from the classpath: almost no XML or boilerplate
- Initializr generates a zipped skeleton: build file, main class, test class
- Also in the zip:
application.properties, wrapper scripts - Reach it at start.spring.io, an IDE wizard, or
curl
2. Initializr Choices
| Choice | Options |
|---|---|
| Project type | Maven or Gradle |
| Language | Java, Kotlin, Groovy |
| Boot version | pick a stable release |
| Group / artifact | project coordinates |
| Packaging | jar or war |
| Java version | e.g. 21 |
| Dependencies | starters like web, data-jpa |
- The same choices work from the command line via
-dflags
bash
curl https://start.spring.io/starter.zip \
-d dependencies=web,data-jpa \
-d type=maven-project \
-d javaVersion=21 \
-o demo.zipdownloads demo.zip, a ready Maven project
3. Convention over Configuration
| Default | Value |
|---|---|
| Port | 8080 |
| Server | embedded Tomcat |
| Config file | application.properties |
Override only when needed
Boot ships sensible defaults. Change a setting only when the default does not fit.
Maven vs Gradle
1. Two Build Tools
| Maven | Gradle | |
|---|---|---|
| Config | XML pom.xml | Groovy / Kotlin DSL build.gradle(.kts) |
| Model | fixed lifecycle | task graph |
| Style | declarative, convention-driven | flexible, scriptable |
| Speed | steady | incremental builds + build cache, usually faster on big projects |
| Trade-off | stricter, more uniform | faster, easier to over-customize |
Parent POM, Dependency Management and Starters
1. The parent POM and the BOM
spring-boot-starter-parentas<parent>gives a BOM of tested versions- Also sets plugin config, Java version, UTF-8, resource filtering
- Declare managed deps with no
<version> - Override: property
<spring-data-bom.version>or explicit<version>
xml
<parent> <artifactId>spring-boot-starter-parent</artifactId> </parent> <dependency> <artifactId>spring-boot-starter-web</artifactId> </dependency>
no version on the dependency
2. No parent allowed? Import the BOM
- Company parent POM: import
spring-boot-dependenciesin<dependencyManagement> - Gradle: the
io.spring.dependency-managementplugin
xml
<dependencyManagement><dependencies>
<dependency>
<artifactId>spring-boot-dependencies</artifactId>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies></dependencyManagement>Project Layout, Main Class and @SpringBootApplication
1. Standard Layout
| Path | Holds |
|---|---|
src/main/java | application code |
src/main/resources | application.properties or .yml, static/, templates/ |
src/test/java | tests |
2. The Main Class
SpringApplication.runcreates the ApplicationContext- Then runs auto-configuration
- Then starts the embedded server
java
package com.example.demo; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
Running and Packaging the App
1. Run and Build
- Dev run:
./mvnw spring-boot:runor./gradlew bootRun - Or the IDE run button on the main class
| Tool | Package command | Output |
|---|---|---|
| Maven | ./mvnw clean package | target/demo-0.0.1-SNAPSHOT.jar |
| Gradle | ./gradlew bootJar | build/libs/ |
2. Run the Packaged App
- Run the jar with
java -jar
Spring Boot Setup Cheat Sheet
1. Bootstrap
- Go to start.spring.io, or the IDE wizard
- Pick build tool, Boot version, Java version
- Packaging jar, then add only the starters you need
2. Commands
| Goal | Maven | Gradle |
|---|---|---|
| Run in dev | ./mvnw spring-boot:run | ./gradlew bootRun |
| Package | ./mvnw clean package | ./gradlew bootJar |
| Run jar | java -jar app.jar |
Check yourself
The main class is in com.example.demo. A @RestController sits in com.example.api. The app starts fine, but its endpoint returns 404. Why?
- Component scan covers only the main class's package and its sub-packages
com.example.apiis a sibling, so the controller bean is never registered- Fix: move it under
com.example.demo, or setscanBasePackages
A pom uses spring-boot-starter-parent but declares spring-boot-starter-web with <version>2.5.0</version>. What is the bug?
- The parent's BOM already manages that version, so the pinned one overrides it
- It can drift from the other Boot libraries and cause mismatches
- Remove the
<version>tag