Gradle dependency management in multi module project

In this tutorial you will know, how you can organize efficiently your dependencies in project with multiple modules.

When you have big project with many modules sharing some dependencies its worth having one place, where you can define and organize them.
This approach is called BOM (which stands for Bill Of Materials): It is a place (usually the root module) where you define specific versions of all the dependencies used in the project. Later (in other modules of the project) you can refer to those dependencies without indicating the specific version.
This way of managing versions of dependent libraries introduces consistency into your project.

Some build automation tools have built in dependency management mechanism providing BOM – for example for Maven you can make use of the dependencyManagement configuration.
Unfortunately Gradle doesn’t have such built in functionality, but you can use plugin io.spring.dependency-management to easily achieve the same effect.

Here is shown what the root configuration file should look like:

plugins {
    id "base"
    id "io.spring.dependency-management" version "1.0.6.RELEASE"
}

description = """pets"""

allprojects {
    group = 'com.devcases'
    version = '0.0.1-SNAPSHOT'

    apply plugin: 'java'

    tasks.withType(JavaCompile) {
        options.encoding = 'UTF-8'
    }

    repositories {
        maven { url "http://repo.maven.apache.org/maven2" }
        mavenCentral()
    }
}

subprojects {
    sourceCompatibility = 1.8
    targetCompatibility = 1.8

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

    dependencyManagement {
        dependencies {
            dependency 'org.springframework.boot:spring-boot-starter-web:2.1.0.RELEASE'
        }
    }
}

After importing plugin io.spring.dependency-management we can use two sections in the root module:

  • allprojects – for defining common configuration for sub projects and root project
  • subprojects – for defining common configuration for sub projects without the root project
In our example we define repositories which we are using in all modules of our project and a dependency of the spring-boot-starter-web library.
Plugin dependency-management allows us to share any other configuration, not only dependencies – in our example we define sourceCompatibility,  targetCompatibility and import plugin: ‘io.spring.dependency-management’ in all the submodules.
After defining this configuration we can use it in submodules with very small amount of code:
dependencies {
    compile project(':pets-domain')
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web'
}

The first line of above code snippet imports another module ‘pets-domain’. The second line imports external library.
In submodules we shouldn’t use any versions of the libraries – they are managed only in the root module.

The example code you can download from here

Leave a Reply

Your email address will not be published. Required fields are marked *