Featured

How to Set Up a Spring Boot Project

There are several ways to build new Spring Boot projects. A Spring Boot project is essentially a Java project that includes a few classes in its classpath.

 

Nowadays, dependencies aren’t manually added to the classpath; instead, build tools such as Maven or Gradle are commonly used to manage them. While building a Spring Boot project manually isn’t complicated, it’s advisable to use a project generator.

 

With the Spring Initializr, there is a web-based service that we can use directly in the browser. In most modern development environments, this is also directly integrated via dialogs. Let’s take a look at the Initializr at https://start.spring.io/.

 

Spring Initializr Website

 

We can choose whether a Maven or a Gradle project as desired; in this case, we choose Maven. Next, we can set the programming language. Language options include Java, Kotlin, and Groovy, with Spring’s reference documentation featuring examples in both Java and Kotlin, highlighting the importance of Kotlin in backend development. Java is the default language, and we'll keep it that way.

 

Next, we select the version number of Spring Boot (because there are updates every month, the options will be more current on the website than the screenshot here). Then, we can enter the usual project metadata, which is typical for Maven. This has nothing to do with the Spring project at first.

 

To build a project, we fill in the following input fields: enter “com.tutego” for Group and “date4u” for Artifact. The Name field is prefilled as date4u, and Description is prefilled as Demo project for Spring Boot. The Package name, which is automatically generated from the Group and the Artifact, fits so far.

 

Next, we look at Packaging: Should the application be packaged as a JAR (Java archive) (the default) or generated as a web application (WAR)? Web applications can be deployed later in a servlet container such as Tomcat. In addition, the Java version must be specified; Spring Boot 3 requires at least Java 17.

 

The Dependencies section on the right allows for the inclusion of Spring-supported projects, rather than any arbitrary Java library. For example, you could say “I want to do something with web development” or “something with a tool like Lombok” so that there are setters and getters automatically via a compiler hack. The details are later automatically entered into the project object model (POM) file (or in the case of Gradle, into a Gradle file) via the Initializr. Dependencies can be removed by using the minus sign.

 

Selecting Explore there shows a preview into the project as it would be generated.

 

A First Look at the Project

 

If you unfold the structure tree at src, you see the typical default directory layout of Maven: src/main/java, src/main/resources, and src/test/java, but no src/test/resources by default. This must be added manually later.

 

In addition, other files can be found: a main class with the main(…) method, an empty application.properties file for configuration, an automatically generated test class, and, of course, as already shown, the POM file. There is also a HELP.md file that contains compact documentation about the dependencies. In addition, .gitignore is generated because developers today usually use Git for version control.

 

On the web page with the file tree, there is a Download button at the bottom (see the previous figure). Clicking on it opens a ZIP file. On the main page, the button is called Generate (refer to the first figure). In other words, the Spring Initializr is essentially a special web service that provides a source code archive with the corresponding project.

 

Once you’ve downloaded and unpacked the ZIP archive, the result is a regular Maven project that can be opened in any modern development environment. (For example, if you’re using the free IntelliJ IDEA Community Edition, you’ll have to do so as a Maven project at this point because that edition has no support for Spring Boot projects.) However, the project can now also be built and executed on the command line because the Initializr uses the Maven wrapper to generate a kind of local Maven installation. The installation can be tested with mvnw -version:

 

$ ./mvnw -version

Apache Maven 3.8.6 (84538c9988a25aec085021c365c560670ad80f63)

Maven home: …\.m2\wrapper\dists\apache-maven-3.8.6-bin\

1ks0nkde5v1pk9vtc31i9d0lcd\apache-maven-3.

8.6

Java version: 17, vendor: Oracle Corporation, runtime: …

\jdk-17

Default locale: en_DE, platform encoding: Cp1252

OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

 

Tip: Maven requires a set JAVA_HOME environment variable. If the procedure described causes any problems, it’s probably due to the environment variable not being set. However, Maven also displays this in an error message.

 

A plug-in is already added to the POM file, which can also be used to run the Spring program. After entering the command mvnw spring-boot:run, all phases are processed. This means that the project is first compiled and then executed.

 

$ mvnw spring-boot:run

[INFO] Scanning for projects…

[INFO]

[INFO] -------------------------< com.tutego:date4u >-----------------------

[INFO] Building date4u 0.0.1-SNAPSHOT

[INFO] --------------------------------[ jar ]------------------------------

[INFO]

[INFO] >>> spring-boot-maven-plugin:3.0.0:run (default-cli) > testcompile

@ date4u >>>

[INFO]

[INFO] --- maven-resources-plugin:3.3.0:resources (default-resources) @ date4u -

--

[INFO] Copying 1 resource

[INFO] Copying 0 resource

[INFO]

[INFO] --- maven-compiler-plugin:3.10.1:compile (default-compile) @ date4u ---

[INFO] Changes detected - recompiling the module!

[INFO] Compiling 1 source file to …\target\classes

[INFO]

[INFO] --- maven-resources-plugin:3.3.0:testResources (defaulttestResources)

@ date4u ---

[INFO] skip non existing resourceDirectory …\src\test\resources

[INFO]

2475.book Seite 46 Donnerstag, 14. September 2023 9:48 21

47

1.2 Spring Boot

1

[INFO] --- maven-compiler-plugin:3.10.1:testCompile (defaulttestCompile)

@ date4u ---

[INFO] Changes detected - recompiling the module!

[INFO] Compiling 1 source file to …\target\test-classes

[INFO]

[INFO] <<< spring-boot-maven-plugin:3.0.0:run (default-cli) < testcompile

@ date4u <<<

[INFO]

[INFO]

[INFO] --- spring-boot-maven-plugin:3.0.0:run (default-cli) @ date4u ---

[INFO] Attaching agents: []

 

  .   ____          _            __ _ _

 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \

( ( )\___ | '_ | '_| | '_ \/  _` | \ \ \ \

 \\/  ___)| |_)| | | | | | | (_| |  ) ) ) )

  '  |____| .__|_| |_|_| |_ \__, | / / / /

========= |_|==============|____/=/_/_/_/

:: Spring Boot ::                  (v3.1.3)

 

2023-11-28T18:38:14.846+01:00 INFO 61404 --- [

       main] com.tutego.date4u.Date4uApplication        : Starting Date4uAppli

cation using Java 17 with PID 61404 (…\target\classes started by Christian in …)

2023-11-28T18:38:14.850+01:00 INFO 61404 --- [

       main] com.tutego.date4u.Date4uApplication        : No active profile se

t, falling back to 1 default profile: "default"

2023-11-28T18:38:15.252+01:00 INFO 61404 --- [

       main] com.tutego.date4u.Date4uApplication        : Started Date4uApplic

ation in 0.71 seconds (process running for 0.998)

[INFO] ---------------------------------------------------------------------

[INFO] BUILD SUCCESS

[INFO] ---------------------------------------------------------------------

[INFO] Total time: 3.103 s

[INFO] Finished at: 2023-11-28T18:38:15+01:00

[INFO] ---------------------------------------------------------------------

 

In the console, we see the result: the Spring application starts up and shuts down again. Because the application doesn’t contain a server part, there’s nothing else for the Spring application to do, and it exits automatically.

 

Using the Maven wrapper has two key benefits: First, project recipients can run the project immediately without needing their own local Maven installation. Second, the local version of Maven provided by the wrapper doesn’t conflict with any other global Maven installation. This can be advantageous in achieving a reliable build, where the software can be built not only today but also 10 years from now. When the software provides its own Maven, the build environment becomes a closed system that works without any external dependencies. The Maven wrapper downloads all the necessary components from the Maven Central server, and the only requirement is an installed JDK. Additional information is available on the official website at https://maven.apache.org/wrapper/index.html.

 

Editor’s note: This post has been adapted from a section of the book Spring Boot 3 and Spring Framework 6 by Christian Ullenboom.

Recommendation

Spring Boot 3 and Spring Framework 6
Spring Boot 3 and Spring Framework 6

Say goodbye to dependencies, bogged-down code, and inflexibility! With the Spring framework and Spring Boot, you’ll painlessly create Java applications that are production ready. Start with the basics: containers for Spring-managed beans, Spring framework modules, and proxies. Then learn to connect to relational databases, implement Jakarta Persistence, use Spring Data JPA, and work with NoSQL databases. Get the right know-how for modern software development with Spring and Java!

Learn More
Rheinwerk Computing
by Rheinwerk Computing

Rheinwerk Computing is an imprint of Rheinwerk Publishing and publishes books by leading experts in the fields of programming, administration, security, analytics, and more.

Comments