I released the intermediate package of my Test With Spring course. Take a look at the course >>

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.

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

If you are not familiar with Spring Batch or Gradle, you should read the following blog posts before you continue reading this blog post:

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

Let’s move on and find out how we can get the required dependencies when we are writing a “normal” Spring application.

Getting the Dependencies of a Spring Application

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

  • The JDBC driver provides a database specific implementation of the JDBC API. We use the H2 in-memory database because it makes our example application easier to run.
  • Liquibase. We use Liquibase for creating the database of our example application when the Spring container is started.
  • The datasource provides database connections to our application. We use the HikariCP datasource because it is the fastest datasource on this planet.
  • The JPA Provider implements the Java Persistence API. We use Hibernate because it is the most common JPA provider.
  • Spring Data JPA hides the used JPA provider behind its repository abstraction.
  • The dependencies required by a Spring web application. These dependencies include the following Spring modules: spring-aspects, spring-context-support, and spring-webmvc. We also need to get the Servlet API. We need these dependencies because this is the “easiest” way to start a Spring container which ensures that our scheduled jobs are run.
  • Spring Object/XML Mapping support provides support for converting XML documents into objects and vice versa. We need this support when we read information from XML files and write information into XML files.
  • Spring Batch Core contains the classes that are required to launch and control Spring Batch jobs. Also, it includes the spring-batch-infrastructure module.

When we are writing Spring applications, we can get the required dependencies by using one of these two options:

  1. We can manage the dependency versions by using the Spring IO Platform.
  2. We can use the traditional way and manage the dependency versions manually.

Let’s find out how we can get the required dependencies with the Spring IO Platform.

Using the Spring IO Platform

We can get the required dependencies by following these steps:

  1. Enable the Spring IO Platform.
  2. Configure the required dependencies in the build.gradle file.

Enabling the Spring IO Platform

We can enable the Spring IO Platform by following these steps:

First, we need to ensure that our build script resolves its dependencies by using the Bintray’s JCenter Maven repository. We can do this by adding the following buildscript block into our build.gradle file:

buildscript {
    repositories {
        jcenter()
    }
}

Second, we need to add the Spring IO dependency management Gradle plugin into the classpath of our build script. We can do this by adding the following dependencies block into the buildscript block:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath ('io.spring.gradle:dependency-management-plugin:0.5.4.RELEASE')
    }
}
If we use only the Spring IO dependency management Gradle plugin, we don’t need to use the Bintray’s JCenter Maven repository. However, our example application uses another Gradle plugin that is not found from the Maven central repository.

Third, we need to apply the Spring IO dependency management plugin by adding the following line into our build script:

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

Fourth, we need to import the Spring IO Platform POM that specifies the dependency versions of our dependencies. We can do this by adding the following lines into our build.gradle file:

dependencyManagement {
    imports {
        mavenBom 'io.spring.platform:platform-bom:2.0.2.RELEASE'
    }
}

We have now enabled the Spring IO Platform. Let’s find out how we can configure the required dependencies.

Configuring the Required Dependencies

After we have enabled the Spring IO Platform, we don’t have to worry about dependency versions because the Spring IO Platform takes care of that. This means that we can get the required dependencies by adding the following dependencies block into the build.gradle file:

dependencies {
    compile(
            'org.liquibase:liquibase-core',
            'com.zaxxer:HikariCP',
            'org.hibernate:hibernate-entitymanager',
            'org.springframework.data:spring-data-jpa',
            'org.springframework:spring-context-support',
            'org.springframework:spring-webmvc',
			'org.springframework:spring-oxm',
            'org.springframework.batch:spring-batch-core'

    )
    providedCompile(
            'javax.servlet:javax.servlet-api'
    )
    runtime(
            'com.h2database:h2',
            'org.springframework:spring-aspects'
    )
}

Let’s move on and 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 need specify the version numbers of all dependencies. We can do this by adding the following dependencies block into our build.gradle file:

dependencies {
    compile(
            'org.liquibase:liquibase-core:3.4.2',
            'com.zaxxer:HikariCP:2.4.3',
            'org.hibernate:hibernate-entitymanager:4.3.11.Final',
            'org.springframework.data:spring-data-jpa:1.9.2.RELEASE',
            'org.springframework:spring-context-support:4.2.4.RELEASE',
            'org.springframework:spring-webmvc:4.2.4.RELEASE',
			'org.springframework:spring-oxm:4.2.4.RELEASE',
            'org.springframework.batch:spring-batch-core:3.0.6.RELEASE'

    )
    providedCompile(
            'javax.servlet:javax.servlet-api:3.1.0'
    )
    runtime(
            'com.h2database:h2:1.4.190',
            'org.springframework:spring-aspects:4.2.4.RELEASE'
    )
}

We are now aware how we can get the required dependencies of a normal Spring application. However, we might want to use Spring Boot because it simplifies the dependency management and helps us to get rid of boilerplate code. Let’s find out how we can get the required dependencies of a Spring Boot application.

Getting the Dependencies of a Spring Boot Application

We can get the dependencies of Spring Boot Application by following these steps:

First, we need to ensure that our build script resolves its dependencies by using the Maven central repository. We can do this by adding the following buildscript block into our build.gradle file:

buildscript {
    repositories{
        mavenCentral()
    }
}

Second, we need to add the Spring Boot Gradle plugin into the classpath of our buildscript. We can do this adding the following dependencies block into the buildscript block:

buildscript {
    repositories{
        mavenCentral()
    }
    dependencies {
        classpath('org.springframework.boot:spring-boot-gradle-plugin:1.3.2.RELEASE')
    }
}

Third, we need to apply the Spring Boot Gradle plugin by adding the following line into our build script:

apply plugin: 'spring-boot'

Fourth, we need to configure the required dependencies. When we configure the required dependencies, we don’t have to worry about dependency versions because Spring Boot takes care of that. We need to get the following dependencies:

  • The spring-boot-starter-batch dependency provides the dependencies that are required by Spring Batch. We need to add this dependency into the compile configuration.
  • The spring-boot-starter-data-jpa dependency provides the dependencies that are required Spring Data JPA. We need to add this dependency into the compile configuration.
  • Spring Object/XML Mapping support provides support for converting XML documents into objects and vice versa. We need this support when we read information from XML files and write information into XML files.
  • Liquibase. We use Liquibase for creating the database of our example application when the Spring container is started. We need to add this dependency into the runtime configuration.
  • The JDBC driver provides a database specific implementation of the JDBC API. We use the H2 in-memory database because it makes our example application easier to run. We need to add this dependency into the runtime configuration.

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

dependencies {
    compile(
            'org.springframework.boot:spring-boot-starter-batch',
            'org.springframework.boot:spring-boot-starter-data-jpa',
			'org.springframework:spring-oxm'
    )
    runtime(
            'org.liquibase:liquibase-core',
            'com.h2database:h2'
    )
}

Let’s summarize what we learned from this blog post.

Summary

This blog post has taught us three things:

  • We learned to get the dependencies of a normal Spring application.
  • We know how we can get the dependencies of a Spring Boot application.
  • If we get the required dependencies by using the Spring IO Platform or Spring Boot, we don’t have to worry about dependency versions.

The next part of this tutorial describes how we can read information from CSV and XML files.

P.S. You can get the example applications of this blog post from Github: Spring example and Spring Boot example.

About the Author

Petri Kainulainen is passionate about software development and continuous improvement. He is specialized in software development with the Spring Framework and is the author of Spring Data book.

About Petri Kainulainen →

1 comment… add one

    Leave a Comment