Build Systems for SpringBoot

Learn via video courses
Topics Covered

Overview

Any software system needs a build system to generate a deployable artefact. For example, the artefact for java based applications can be JAR, WAR, or EAR. The build tool helps in building the application and creating the artefact. Though many build tools exist in the market, maven and Gradle are popular.

Introduction to Build Systems in SpringBoot

Maven and Gradle are the two most popular build tools to build java based applications. Both support dependency management and consume artifactory from maven central.

Maven

This section will cover all dependencies and plugins required to build the spring boot application.

Inheriting the starter Parent

Every spring boot application must inherit the starter-parent dependency to use spring boot defaults.

The starter parent inherits from spring-boot-dependencies, which contains all dependency versions for all the framework that works with spring boot and are compatible with each other.

Starter parent provides:

  • Provide the default compiler level as Java 1.8.
  • Provides a default configuration for maven-surefire-plugin, maven-jar-plugin, and maven-failsafe-plugin.

Using Spring Boot without the Parent POM

There can be a situation in that you can not inherit from starter-parent dependency because your corporate has its parent pom, which must be inherited. In such cases, spring boot provides an alternate way to take advantage of dependency management.

Changing the Java Version

Spring boot by default assumes the application is using java 8, but it can be overridden using the maven property java.version

Using the Spring Boot Maven plugin

Spring Boot includes a Maven plugin that can package the project as an executable jar. Add the plugin to your <plugins> section.

The final pom will look like the below:

Gradle

The same configuration in Gradle will look like this.

build.gradle

Dependency Management Advantages

A set of JARs is required for a Java application to function properly. If we’ve spent time developing software, we’re probably familiar with the headaches of dependency management.

Any feature we want to introduce in a Java-based application will require several dependencies. For example, a set of dependencies must be added for our application to expose a simple REST endpoint.

This problem multiplies when we consider versions. One version of a particular dependency can work with only a specific version of other dependencies.

What if we happen to upgrade a library to a newer version? It will cascade on all the dependent libraries, eventually leading to a game of dependency “Whack-a-Mole.

Spring boot starters solve the solution. Spring Boot starters are Bills of Material (BOM), which provide packaged dependency of JARs compatible with each other.

Whenever we include any spring-boot-starter. dependencies, all the necessary transitive dependencies that have been thoroughly tested and promised to work together are brought up.

With this one dependency, our application is web-enabled and ready to expose the endpoint. This is without the need for manual configuration of the dispatcher servlet, content negotiator, serializer, deserializer, etc.

Conclusions

  • Build tools are required to build the application and create artefacts.
  • Two most popular build systems for java based applications are Maven and Gradle.
  • Maven and Gradle are the default spring boot build systems for spring boot-based applications as they offer dependency management.
  • Spring boot simplifies dependency management using the bill of materials (BOM) stater dependencies.