Spring Batch Tutorial: Getting the Required Dependencies With Gradle

The second part of my Spring Batch tutorial described how you 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 you might want to use it instead of Maven. That is why I wanted to write a blog post that describes how you can get the required dependencies with Gradle.

After you have read this blog post, you:

  • Can identify the modules provided by Spring Batch.
  • Can list the required dependencies when you are using Spring Framework or Spring Boot.
  • Know how you 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 you to integrate Spring Batch with Spring Integration.

Next, you will find out how you can get the required dependencies when you are writing a "legacy" Spring application.

Getting the Dependencies of a "Legacy" Spring Application

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

  • Because Spring Batch writes the job and step execution metadata (aka logs) to the database, you need a JDBC driver which allows your application to communicate with the used database. You should use the H2 in-memory database because it makes your example application easy to run. Also, because you need this dependency only when your application is run, you have to add this dependency to the runtimeOnly dependency configuration.
  • Liquibase. You should 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. You need this dependency when your application is compiled, and that's why you have to add this dependency to the implementation dependency configuration.
  • The datasource provides database connections to your application. You should use the HikariCP datasource because it's the fastest datasource on this planet. Because you need this dependency when your application is compiled, you 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. You need this dependency when your application is compiled, and that's why you 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 you are writing a "legacy" Spring application, you can get the required dependencies by using one of these two options:

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

Let's find out how you can get the required dependencies when you are using the dependency management of Spring Boot.

Using the Dependency Management of Spring Boot

You can get the required dependencies by following these steps:

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

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

Second, you have to apply the Spring dependency management Gradle plugin to your Gradle project. Because this plugin is the dependency of the Spring Boot Gradle plugin, you don't have to declare it as a dependency of your Gradle project. Also, this means that you 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.

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

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

Third, you have to ensure that the Spring dependency management Gradle plugin imports the Spring Boot's BOM. You can do this by adding the following dependencyManagement block to your build.gradle file:

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

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

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

Next, you will find out how you can manage your dependency versions manually.

Using the Traditional Way

If you want to use the traditional way, you have to manage your dependency versions “manually”. In other words, you must specify the versions of all dependencies. You can do this by adding the following dependencies block to your 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'
    )
}

You can now get the required dependencies when you are working with a "legacy" Spring application. Let's move on and find out how you can get the required dependencies when you are using Spring Boot.

Getting the Dependencies of a Spring Boot Application

You can get the required dependencies by following these steps:

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

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

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

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

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

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

  • Because Spring Batch writes the job and step execution metadata (aka logs) to the database, you need a JDBC driver which allows your application to communicate with the used database. You should use the H2 in-memory database because it makes your example application easy to run. Also, because you need this dependency only when your application is run, you have to add this dependency to the runtimeOnly dependency configuration.
  • Liquibase. You should 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. You need this dependency only when your application is run, and that's why you 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 you need this dependency when your application is compiled, you 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 you to use JDBC and HikariCP datasource in your Spring Boot application. You need this dependency when your application is compiled, and that's why you 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:

You can get these dependencies by adding the following dependencies block to your 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'
    )
}

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

Summary

This blog post has taught you five things:

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

The next part of my Spring Batch tutorial describes how you can read the input data of your batch job from a CSV file.

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

1 comment… add one

Leave a Reply