Spring Boot Build Tools
Spring Boot Build Tools

The following tutorial focuses on the topic of various build systems support for developing Spring Boot applications. Usually, Spring Boot Applications recommends using dependency management tools like Maven, Gradle over Ant.

In last tutorial we have discussed about Creating first Spring Boot Application, here in this tutorial, we will discuss build tools for spring boot.

There is no built-in support for dependency management in Ant and is not recommended generally. If you need to use Ant due to project restrictions, then it is better to go for Ant + Ivy because it supports similar dependency management like Maven, Gradle, etc.

Dependency Management

  • Each version of Spring boot supports a pre-defined set of dependencies. Based on the version, Spring boot automatically downloads the dependencies.
  • Also, all the other child dependencies do not need to declare the version dependencies in the build configuration file.
  • Spring Boot does not recommend to override the version number to another version as it may result in version conflicts and run time errors.
  • However, if a need arises to specify a different version for a particular dependency, then it can be defined in the build configuration file.
  • For instance, the following example uses different version of spring release train dependency:
<properties>
<spring-data-releasetrain.version>Fowler-SR2</spring-data-releasetrain.version>
</properties>

Here are the following build tools for spring boot

Using Maven

  • Maven users can inherit from parent starter (spring-boot-starter-parent) in order to manage the spring boot starter dependencies.
  • Provide the build version for the spring boot starter parent dependency as shown in the following snippet:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
</project>
  • For the other child starters ( for instance, web starters), there is no need to define the versions.
  • For instance, see the below example where the dependency version for the spring-boot-starter-web starter is not defined.
<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
</dependencies>
  • Spring boot downloads the version automatically based on the version defined in the parent starter.

Dependency Management without parent starter declaration

  • The parent starter takes care of dependency and plugin management in a Spring Boot application.
  • However, it is not always possible to inherit from the parent starter in pom.xml due to project constraint on design rules.
  • Also, there might be a need to inherit from a third-party starter parent.
  • In such cases also, you can still get all the advantages of Spring Boot dependency management.
  • This can be achieved by including “spring-boot-dependencies” artifact in pom.xml by adding “scope=import” as shown in the following snippet:
<dependencyManagement>
     <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.1.3.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
  • Also, include other spring starter dependencies as usual (for example, web starter) and get the advantage of spring boot features.
<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
</dependencies>
  • Also, explicitly add the “spring-boot-maven-plugin to get all the required features of plugin management (For example, packaging the application as JAR) as shown below:
<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
  </plugins>
</build>
  • To override any particular dependency, add an entry in the dependencyManagement section of pom.xml.
  • This entry needs to be added before the “spring-boot-dependencies” artifact as shown in the following snippet:
<dependencyManagement>
  <dependencies>
<!-- Override Spring Data release train provided by Spring Boot -->
    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-releasetrain</artifactId>
      <version>Fowler-SR2</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-dependencies</artifactId>
      <version>2.1.3.RELEASE</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

Using Gradle

  • Gradle is also a build tools for spring boot. Add spring boot support to the Gradle project (“build.gradle” file) by using “Spring boot gradle” plugin in the following way:
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath(
                'org.springframework.boot:spring-boot-gradle-plugin:2.1.3.RELEASE'
        )
    }
}
 
apply plugin: 'java'
apply plugin: 'spring-boot'
  • Also, specify the required starter dependencies to be imported into the project.
  • For instance, to import web and actuator starters, include the following lines in the configuration:
repositories {
    mavenCentral()
}

dependencies {
    compile(
            'org.springframework.boot:spring-boot-starter-actuator',
            'org.springframework.boot:spring-boot-starter-web’
    )
}

In the above example, notice the following starter dependencies:

spring-boot-starter-actuator: Provides production ready features by enabling the developers to monitor and collect the statistics of a Spring boot application.

spring-boot-starter-web : Enables to configure the application as a Spring web MVC application thereby handling web requests.

Using Ant

  • In addition to Maven and Gradle, it is possible to get the Spring boot application running with a combination of Apache Ant + Ivy.
  • There are 3 components required as follows for :
  1. ivysetting.xml – Tells ivy where the maven repository is located.
  2. ivy.xml – To download the project dependencies.
  3. Ant build.xml – To compile and build the application.
  • Refer the following sample XML to get an overview of the configuration files:

ivysetting.xml

  • The following code snippet contains a sample ivysetting.xml
<ivysettings>
 <!-- Overrides the default ivysetting.xml that is found inside the ivy.jar -->
 	<resolvers>
  		<chain name="public">
                    <ibiblio name="spring-milestones" m2compatible="true" root="http://repo.springsource.org/libs-milestone/"  />
  		    <ibiblio name="ibiblio" m2compatible="true" />
 		</chain>
 	</resolvers>
</ivysettings>

 

ivy.xml

  • Also, a sample ivy.xml is shown below:
<ivy-module version="2.0">
  <info organisation="org.springframework.boot" module="spring-boot-sample-ant" />
  <configurations>
    <conf name="compile" description="everything needed to compile this module" />
    <conf name="runtime" extends="compile" description=”needed to run this module" />
  </configurations>
  <dependencies>
    <dependency org="org.springframework.boot" name="spring-boot-starter-web"
      rev="2.1.3.RELEASE" conf="compile" />
  </dependencies>
</ivy-module>

Conclusion

In this tutorial, you have learned about the build systems supported in Spring Boot and configure the same through Maven, Gradle, and Ant. We discussed build tools for spring boot in this tutorial.

In the next tutorial, you will be learning about the various starters used in Spring Boot.

References

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-build-systems

LEAVE A REPLY

Please enter your comment!
Please enter your name here