Sub topic 6
Generating a Project with Spring Initializr
This chapter assumes you know basic Java, have used Maven or Gradle at a user level, understand HTTP at a basic level, and have a JDK and a terminal available. Here you will generate a Spring Boot project. The next chapters tour what it creates.
What Spring Initializr Is
Spring Initializr is a service that generates a starter project from a few choices. You pick a build tool, a language, a Spring Boot version, and the dependencies you need. It returns a ZIP file with a build file, a main class, a test class, and a build wrapper.
You can reach it in three ways:
- The website at start.spring.io.
- The Spring Initializr wizard built into IDEs such as IntelliJ IDEA Ultimate, Spring Tools for Eclipse, and VS Code with the Spring extensions.
- Its HTTP API, which you can call with
curl.
All three use the same service, so they produce the same project.
The Choices on the Form
| Field | What it controls | A sensible choice for this tutorial |
|---|---|---|
| Project | Build tool: Maven or Gradle | Maven |
| Language | Java, Kotlin, or Groovy | Java |
| Spring Boot | Boot version | The latest stable release, not a SNAPSHOT or milestone |
| Group | Your organization's identifier, used as the base package | `com.example` |
| Artifact | The project name, used for the JAR file name | `demo` |
| Packaging | JAR or WAR | JAR |
| Java | The Java version the project targets | A version your JDK supports |
| Dependencies | The starters added to the build file | Spring Web |
Group and Artifact together set the Maven coordinates. They also set the default Java package. With com.example and demo, the main class lands in com.example.demo.
Choosing Dependencies
Each dependency you add on the form becomes a starter in the build file. Selecting Spring Web adds spring-boot-starter-web. That starter brings in Spring MVC and an embedded Tomcat server. Every project also gets spring-boot-starter-test, which is why the test starter appears without your choosing it.
Add only what you need now. You can edit the build file later, so a missing dependency is easy to fix.
Generating the Project
- Open start.spring.io.
- Select Maven, Java, and the latest stable Spring Boot version.
- Enter Group
com.exampleand Artifactdemo. - Choose JAR packaging and your Java version.
- Click Add Dependencies and select Spring Web.
- Click Generate. Your browser downloads
demo.zip. - Unzip the file and open the
demofolder in your IDE.
You can do the same from a terminal. This command requests a Maven project with the Web dependency and unzips it:
curl https://start.spring.io/starter.zip \
-d type=maven-project \
-d language=java \
-d groupId=com.example \
-d artifactId=demo \
-d packaging=jar \
-d dependencies=web \
-o demo.zip
unzip demo.zip -d demo
cd demoWithout a bootVersion parameter, the service uses its current default version.
Checking That It Worked
From the project folder, run the build and the generated test:
./mvnw testA successful run ends with BUILD SUCCESS. The first run downloads dependencies, so it can take a minute.
Common Errors
- Java version mismatch. If you chose a Java version newer than your JDK, the build fails with an error such as "release version not supported". Run
java -version, then regenerate the project or change thejava.versionproperty inpom.xml. - Permission denied on
mvnw. ZIP extraction can drop the executable bit. Runchmod +x mvnw. - Wrong package. If your Group or Artifact contains hyphens or other invalid characters, Initializr adjusts the package name. Check that the package matches what you expect.
- Snapshot versions. Choosing a SNAPSHOT or milestone Boot version can fail to resolve or change without notice. Use a stable release while learning.
Exercise
You generate a project with Group org.acme and Artifact orders. Which package holds the main class, and which dependency did you pick if spring-boot-starter-web appears in the build file?
Show answer
The main class is in the package org.acme.orders. You selected Spring Web on the form, which adds spring-boot-starter-web.
Exercise
A teammate has no Maven installed. How can they build the generated project?
Show answer
They use the Maven wrapper included in the project, running ./mvnw (or mvnw.cmd on Windows) from the project folder.