How To Exclude Dependencies In Gradle Based Java Project

Table of Contents

Introduction

I am going to show you how to exclude dependency from build.gradle script in gradle based Java project. Specially when you use several starter dependencies in your Spring Boot project, then many children dependencies are found and hence many libraries are downloaded in the project’s classpath.

gradle build

These many libraries may not be needed for your project and you may want to exclude these libraries. There might be other reason also to exclude the dependencies from your dependency tree hierarchy.

The exclusion reason may be:

  1. Your project is not compatible with the version currently in use and want to use a specific version
  2. You found vulnerability with a version of the library is being used and want to upgrade
  3. You don’t need the specific library at all for your project and keeping it in the project may break the functionality

Prerequisites

Gradle, Java, Spring/Spring Boot (depends on project nature)

Exclude Dependencies/Libraries

Here I will show you how to exclude dependencies or libraries from the project class path. These libraries might not be required for your project or you need to use different versions of libraries other than default versions that come with the parent dependencies.

Let’s say you want to exclude log4j-api module from your dependencies. Now you need to check where log4j-api dependency is found. So, once you find the log4j-api dependency in dependency tree hierarchy then you can easily exclude the log4j-api dependency.

For example, you will find log4j-api dependency under spring-boot-starter-web, spring-boot-starter-logging, or in some other dependencies.

Now you have two options to exclude dependencies:

  1. Use exclusion globally so from every dependency the log4j-api will be excluded wherever found
  2. Use exclusion in particular dependency where log4j-api is found

It would be great idea if you first check the dependency tree hierarchy of all libraries in your project class path using the gradlew dependencies command using CLI.

Exclude Per Configuration

To exclude globally you need to apply exclusion under configuration {} tag. So, in your build.gradle script use the following content to exclude the log4j-api from all dependencies wherever log4j-api is found.

configuration.all {
 compile.exclude module: ‘log4j-api’
}

The above content tells build script to exclude an artifact with log4j-api from all dependencies wherever it is found.

Now if you want to also exclude log4j-to-slf4j then you can add this also in the following manner:

configuration.all {
     compile.exclude module: ‘log4j-api’
     compile.exclude module: ‘log4j-to-slf4j’
}

If you want to exclude more dependency, then you need to add more in a separate line.

If you want to exclude all modules or artifacts (artifactId) under a group (groupId) then you can use the following example:

configuration.all {
     compile.exclude group: ‘org.apache.logging.log4j’
}

As an alternative way, you can also use configuration.implementation {} to exclude transitive dependencies from your application. This excludes the dependency from the compile, runtimetestCompile, and testRuntime classpaths.

configuration.implementation {
     compile.exclude group: ‘org.apache.logging.log4j’
}

Exclude Per Dependency

Exclusion happens at each dependency level. So, you need to exclude a particular dependency from a dependency where you found your child dependency to be excluded.

If you want to exclude from a particular dependency, then you can use the following example:

compile(“org.springframework.boot:spring-boot-starter-web:2.3.2.RELEASE”) {
    exclude group: “org.apache.logging.log4j”, module: “log4j-api”
    exclude group: “org.apache.logging.log4j”, module: “log4j-to-slf4j”
}

Or, with a newer version of gradle,

implementation(“org.springframework.boot:spring-boot-starter-web:2.3.2.RELEASE”) {
    exclude group: “org.apache.logging.log4j”, module: “log4j-api”
    exclude group: “org.apache.logging.log4j”, module: “log4j-to-slf4j”
}

Exclude per dependency does exclusion from a particular dependency whereas exclude per configuration will exclude transitive dependency across all dependencies.

You can also exclude multiple artifacts from a particular group. For example, if you want to use Junit 4 with Spring Boot 2.6.6 and your application also requires spring-boot-starter-test dependency (for example, you want to use ReflectionUtils class), so you need to exclude Junit 5 from spring-boot-starter-test dependency in the build.gradle file.

So, first let’s see what are there in the spring-boot-starter-test dependency:

spring boot junit 5 exclude

Now let’s exclude all Junit related APIs from spring-boot-starter-test dependency:

testImplementation 'org.springframework.boot:spring-boot-starter-test:2.6.6' {
	exclude group: 'org.junit.jupitor'
	exclude group: 'org.junit.vintage'
	exclude group: 'org.mockito'
}

Therefore, all artifacts under groups org.junit.jupitor, org.junit.vintage and org.mockito will be excluded from the class path libraries.

Leave a Reply

Your email address will not be published.