The first part of my Spring Batch tutorial explained why you should use Spring Batch instead of writing your own batch jobs and identified the basic building blocks of a Spring Batch job. Before you can move on and see Spring Batch in action, you have to get the required dependencies.
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 Maven.
Let’s start by taking a look at the modules provided by Spring Batch.
This blog post assumes that:
Introduction to the Modules of Spring Batch
Spring Batch provides the following modules:
- The
spring-batch-infrastructuremodule contains the common readers and writers, and provides services for application developers and the core module. - The
spring-batch-coremodule contains the classes which are required to launch and control Spring Batch jobs. - The
spring-batch-testmodule provides support for writing automated tests for Spring Batch jobs. - The
spring-batch-integrationmodule 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, you need this dependency only when your application is run, and that’s why you have to use the
runtimescope when you declare this dependency. - 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.
- The datasource provides database connections to your application. You should 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-infrastructuremodule.
When you are writing a “legacy” Spring application, you can get the required dependencies by using one of these two options:
- You can manage the dependency versions by using the dependency management of Spring Boot.
- 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 use 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 configure the parent POM of your Maven project. Spring Boot has starter called spring-boot-starter-parent which provides dependency and plugin management for Maven projects. When you want to use the dependency management of Spring Boot, you have to set this starter as the parent of your Maven project. You can do this by adding the following XML to your POM file:
1<parent>
2 <groupId>org.springframework.boot</groupId>
3 <artifactId>spring-boot-starter-parent</artifactId>
4 <version>2.3.1.RELEASE</version>
5</parent>
Second, after you have configured the parent POM of your Maven project, you don’t have to worry about dependency versions because they are inherited from the parent POM. This means that you can get the required dependencies by adding the following XML to the dependencies section of your POM file:
1<!-- Database -->
2<dependency>
3 <groupId>com.h2database</groupId>
4 <artifactId>h2</artifactId>
5 <scope>runtime</scope>
6</dependency>
7
8<!-- Liquibase -->
9<dependency>
10 <groupId>org.liquibase</groupId>
11 <artifactId>liquibase-core</artifactId>
12</dependency>
13
14<!-- DataSource -->
15<dependency>
16 <groupId>com.zaxxer</groupId>
17 <artifactId>HikariCP</artifactId>
18</dependency>
19
20<!-- Spring Batch -->
21<dependency>
22 <groupId>org.springframework.batch</groupId>
23 <artifactId>spring-batch-core</artifactId>
24</dependency>
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 dependency declarations to the dependencies section of your pom.xml file:
1<!-- Database -->
2<dependency>
3 <groupId>com.h2database</groupId>
4 <artifactId>h2</artifactId>
5 <version>1.4.200</version>
6 <scope>runtime</scope>
7</dependency>
8
9<!-- Liquibase -->
10<dependency>
11 <groupId>org.liquibase</groupId>
12 <artifactId>liquibase-core</artifactId>
13 <version>3.8.9</version>
14</dependency>
15
16<!-- DataSource -->
17<dependency>
18 <groupId>com.zaxxer</groupId>
19 <artifactId>HikariCP</artifactId>
20 <version>3.4.5</version>
21</dependency>
22
23<!-- Spring Batch -->
24<dependency>
25 <groupId>org.springframework.batch</groupId>
26 <artifactId>spring-batch-core</artifactId>
27 <version>4.2.4.RELEASE</version>
28</dependency>
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 configure the parent POM of your Maven project. When you are writing a Spring Boot application, you must set the spring-boot-starter-parent as the parent of your Maven project. You can do this by adding the following XML to your POM file:
1<parent>
2 <groupId>org.springframework.boot</groupId>
3 <artifactId>spring-boot-starter-parent</artifactId>
4 <version>2.3.1.RELEASE</version>
5</parent>
Second, you have to configure the required dependencies. When you configure these dependencies, you can ignore the dependency versions because they are inherited from the parent POM. 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, you need this dependency only when your application is run, and that’s why you have to use the
runtimescope when you declare this dependency. - 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. Because you need this dependency only when your application is run, you have to use the
runtimescope when you declare this dependency. - The
spring-boot-starter-batchdependency provides the dependencies which are required by Spring Batch. - The
spring-boot-starter-jdbcdependency is a starter that provides the dependencies which allow you to use JDBC and HikariCP datasource in your Spring Boot application.
You can get these dependencies by adding the following dependency declarations to the dependencies section of your pom.xml file:
1<dependency>
2 <groupId>com.h2database</groupId>
3 <artifactId>h2</artifactId>
4 <scope>runtime</scope>
5</dependency>
6<dependency>
7 <groupId>org.liquibase</groupId>
8 <artifactId>liquibase-core</artifactId>
9 <scope>runtime</scope>
10</dependency>
11<dependency>
12 <groupId>org.springframework.boot</groupId>
13 <artifactId>spring-boot-starter-batch</artifactId>
14</dependency>
15<dependency>
16 <groupId>org.springframework.boot</groupId>
17 <artifactId>spring-boot-starter-jdbc</artifactId>
18</dependency>
Additional Reading:
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-coredependency in your POM file. - If you are using Spring Boot, you have to declare the
spring-boot-starter-batchdependency in your POM file.
The next part of this tutorial describes how you can get the required dependencies with Gradle.
P.S. You can get the example applications of this blog post from Github.