The first part of my Spring Batch tutorial explained why we should use Spring Batch instead of writing our own batch jobs and identified the basic building blocks of a Spring Batch job. Before we can move on and see Spring Batch in action, we have to get the required dependencies.

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 Maven.

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

This blog post assumes that: You are familiar with 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.

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.

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.

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, we need this dependency only when our application is run, and that’s why we have to use the runtime scope when we declare this dependency.

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, we need this dependency only when our application is run, and that’s why we have to use the scope when we declare this dependency. 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 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. The datasource provides database connections to our application. We use the HikariCP datasource because it’s the fastest datasource on this planet.

provides database connections to our application. We use the HikariCP datasource because it’s the fastest datasource on this planet. Spring Batch Core contains the classes that are required to launch and control Spring Batch jobs. Also, it includes the spring-batch-infrastructure module.

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:

We can manage the dependency versions by using the dependency management of Spring Boot. 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 configure the parent POM of our Maven project. Spring Boot has starter called spring-boot-starter-parent which provides dependency and plugin management for Maven projects. When we want to use the dependency management of Spring Boot, we have to set this starter as the parent of our Maven project. We can do this by adding the following XML to our POM file:

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.1.RELEASE</version> </parent>

Second, after we have configured the parent POM of our Maven project, we don’t have to worry about dependency versions because they are inherited from the parent POM. This means that we can get the required dependencies by adding the following XML to the dependencies section of our POM file:

<!-- Database --> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <!-- Liquibase --> <dependency> <groupId>org.liquibase</groupId> <artifactId>liquibase-core</artifactId> </dependency> <!-- DataSource --> <dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> </dependency> <!-- Spring Batch --> <dependency> <groupId>org.springframework.batch</groupId> <artifactId>spring-batch-core</artifactId> </dependency>

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 dependency declarations to the dependencies section of our pom.xml file:

<!-- Database --> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>1.4.200</version> <scope>runtime</scope> </dependency> <!-- Liquibase --> <dependency> <groupId>org.liquibase</groupId> <artifactId>liquibase-core</artifactId> <version>3.8.9</version> </dependency> <!-- DataSource --> <dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version>3.4.5</version> </dependency> <!-- Spring Batch --> <dependency> <groupId>org.springframework.batch</groupId> <artifactId>spring-batch-core</artifactId> <version>4.2.4.RELEASE</version> </dependency>

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 configure the parent POM of our Maven project. When we are writing a Spring Boot application, we must set the spring-boot-starter-parent as the parent of our Maven project. We can do this by adding the following XML to our POM file:

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.1.RELEASE</version> </parent>

Second, we have to configure the required dependencies. When we configure these dependencies, we can ignore the dependency versions because they are inherited from the parent POM. 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, we need this dependency only when our application is run, and that’s why we have to use the runtime scope when we declare this dependency.

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, we need this dependency only when our application is run, and that’s why we have to use the scope when we declare this dependency. 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. Because we need this dependency only when our application is run, we have to use the runtime scope when we declare this dependency.

. 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. Because we need this dependency only when our application is run, we have to use the scope when we declare this dependency. The spring-boot-starter-batch dependency provides the dependencies which are required by Spring Batch.

dependency provides the dependencies which are required by Spring Batch. 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.

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.

We can get these dependencies by adding the following dependency declarations to the dependencies section of our pom.xml file:

<dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.liquibase</groupId> <artifactId>liquibase-core</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-batch</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency>

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 POM file.

dependency in our POM file. If we are using Spring Boot, we have to declare the spring-boot-starter-batch dependency in our POM file.

The next part of this tutorial describes how we can get the required dependencies with Gradle.

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