Are you tired of writing tests which have a lot of boilerplate code? If so, get started with Spock Framework >>

Spring Batch Tutorial: Getting the Required Dependencies With Gradle

The second part of my Spring Batch tutorial described how we can get the required dependencies with Maven.

However, Gradle has got a lot of traction during the last few years, and the odds are that we might want to use it instead of Maven. That is why I wanted to write a blog post that describes how we can get the required dependencies with Gradle.

After we have read this blog post, we:

  • Can identify the modules provided by Spring Batch.
  • Can list the required dependencies when we are using Spring Framework or Spring Boot.
  • Know how we can get the required dependencies with Gradle.

Let’s start by taking a look at the modules provided by Spring Batch.

Introduction to the Modules of Spring Batch

Spring Batch provides the following modules:

  • The spring-batch-infrastructure module contains the common readers and writers, and provides services for application developers and the core module.
  • The spring-batch-core module contains the classes which are required to launch and control Spring Batch jobs.
  • The spring-batch-test module provides support for writing automated tests for Spring Batch jobs.
  • The spring-batch-integration module helps us to integrate Spring Batch with Spring Integration.

Next, we will find out how we can get the required dependencies when we are writing a “legacy” Spring application.

Getting the Dependencies of a “Legacy” Spring Application

When we are writing Spring Batch jobs by using Spring Framework, we have to get the following dependencies:

  • Because Spring Batch writes the job and step execution metadata (aka logs) to the database, we need a JDBC driver which allows our application to communicate with the used database. We use the H2 in-memory database because it makes our example application easy to run. Also, because we need this dependency only when our application is run, we have to add this dependency to the runtimeOnly dependency configuration.
  • Liquibase. We integrate Liquibase with Spring Framework and ensure that it creates the database tables which contain the job and step execution metadata when the Spring container is started. We need this dependency when our application is compiled, and that’s why we have to add this dependency to the implementation dependency configuration.
  • The datasource provides database connections to our application. We use the HikariCP datasource because it’s the fastest datasource on this planet. Because we need this dependency when our application is compiled, we have to add this dependency to the implementation dependency configuration.
  • Spring Batch Core contains the classes that are required to launch and control Spring Batch jobs. Also, it includes the spring-batch-infrastructure module. We need this dependency when our application is compiled, and that’s why we have to add this dependency to the implementation dependency configuration.
Spring applications require other dependencies as well. I didn’t include those dependencies in the previous list simply because every application is different and requires different dependencies.

When we are writing a “legacy” Spring application, we can get the required dependencies by using one of these two options:

  1. We can manage the dependency versions by using the dependency management of Spring Boot.
  2. We can use the traditional way and manage the dependency versions manually.

Let’s find out how we can get the required dependencies when we use the dependency management of Spring Boot.

Using the Dependency Management of Spring Boot

We can get the required dependencies by following these steps:

First, we have to add the Spring Boot Gradle plugin as the dependency of our project and ensure that it isn’t applied to our project. We can do this by adding the following the plugins block to our build.gradle file:

plugins {
    id 'org.springframework.boot' version '2.3.1.RELEASE' apply false
}

Second, we have apply the Spring dependency management Gradle plugin to our Gradle project. Because this plugin is the dependency of the Spring Boot Gradle plugin, we don’t have to declare it as a dependency of our Gradle project. Also, this means that we don’t have to worry about the version of the Spring dependency management Gradle plugin because it’s specified by the Spring Boot Gradle plugin.

We can apply the Spring dependency management Gradle plugin to our Gradle project by adding the following line to our build.gradle file:

apply plugin: 'io.spring.dependency-management'

Third, we have to ensure that the Spring dependency management Gradle plugin imports the Spring Boot’s BOM. We can do this by adding the following dependencyManagement block to our build.gradle file:

dependencyManagement {
  imports {
      mavenBom org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES
  }
}

Fourth, we have to add the required dependencies to our Gradle project. We don’t have to worry about dependency versions because they are declared in the imported BOM. This means that we can get the required dependencies by adding the following dependencies block to our build.gradle file:

dependencies {
    implementation(
            'org.liquibase:liquibase-core',
            'com.zaxxer:HikariCP',
            'org.springframework.batch:spring-batch-core'
    )
    runtimeOnly(
            'com.h2database:h2'
    )
}

Next, we will find out how we can manage our dependency versions manually.

Using the Traditional Way

If we want to use the traditional way, we have to manage our dependency versions “manually”. In other words, we must specify the versions of all dependencies. We can do this by adding the following dependencies block to our build.gradle file:

dependencies {
    implementation(
            'org.liquibase:liquibase-core:3.8.9',
            'com.zaxxer:HikariCP:3.4.5',
            'org.springframework.batch:spring-batch-core:4.2.4.RELEASE'
    )
    runtimeOnly(
            'com.h2database:h2:1.4.200'
    )
}

We can now get the required dependencies when we are working with a “legacy” Spring application. Let’s move on and find out how we can get the required dependencies when we are using Spring Boot.

Getting the Dependencies of a Spring Boot Application

We can get the required dependencies by following these steps:

First, we have to apply the Spring Boot Gradle plugin to our Gradle project. We can do this by adding the following plugins block to our build.gradle file:

plugins {
    id 'org.springframework.boot' version '2.3.1.RELEASE'
}

Second, we have to apply the Spring dependency management Gradle plugin to our Gradle project. Because this plugin is the dependency of the Spring Boot Gradle plugin, we don’t have to declare it as a dependency of our Gradle project. Also, this means that both Spring Boot and our Gradle project will automatically use the same version of the Spring dependency management Gradle plugin.

We can apply the Spring dependency management Gradle plugin to our Gradle project by adding the following line to our build.gradle file:

apply plugin: 'io.spring.dependency-management'

Third, we have to configure the required dependencies. When we configure these dependencies, we can ignore the dependency versions because they are managed by the Spring Boot Gradle plugin. Before we can use Spring Batch in a Spring Boot application, we must get the following dependencies:

  • Because Spring Batch writes the job and step execution metadata (aka logs) to the database, we need a JDBC driver which allows our application to communicate with the used database. We use the H2 in-memory database because it makes our example application easy to run. Also, because we need this dependency only when our application is run, we have to add this dependency to the runtimeOnly dependency configuration.
  • Liquibase. We integrate Liquibase with Spring Boot and ensure that it creates the database tables which contain the job and step execution metadata when the Spring container is started. We need this dependency only when our application is run, and that’s why we have to add this dependency to the runtimeOnly dependency configuration.
  • The spring-boot-starter-batch dependency provides the dependencies which are required by Spring Batch. Because we need this dependency when our application is compiled, we have to add this dependency to the implementation dependency configuration.
  • The spring-boot-starter-jdbc dependency is a starter that provides the dependencies which allow us to use JDBC and HikariCP datasource in our Spring Boot application. We need this dependency when our application is compiled, and that’s why we have to add this dependency to the implementation dependency configuration.
Spring Boot applications require other dependencies as well. I didn’t include those dependencies in the previous list simply because every application is different and requires different dependencies.

Additional Reading:

We can get these dependencies by adding the following dependencies block to our build.gradle file:

dependencies {
    implementation(
            'org.springframework.boot:spring-boot-starter-batch',
            'org.springframework.boot:spring-boot-starter-jdbc'
    )
    runtimeOnly(
            'com.h2database:h2',
            'org.liquibase:liquibase-core'
    )
}

We can now get the required dependencies when we are using Spring Boot. Let’s summarize what we learned from this blog post.

Summary

This blog post has taught us five things:

  • Because Spring Batch writes the job and step execution metadata to a database, we need a JDBC driver which allows us to communicate with the used database.
  • Because we want to communicate with a relational database, we need a datasource which allows us to create database connections.
  • Because Spring Batch writes the job and step execution metadata to a database, we need a way to create the required database tables before Spring Batch tries to insert data into these tables. We can use Liquibase (or Flyway) for this purpose.
  • If we are working with a “legacy” Spring application, we have to declare the spring-batch-core dependency in our build.gradle file.
  • If we are using Spring Boot, we have to declare the spring-boot-starter-batch dependency in our build.gradle file.

P.S. You can get the example applications of this blog post from Github.

1 comment… add one

    Leave a Comment