Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: December 29, 2024
When working on a Kotlin project with Gradle, there are scenarios where we need to exclude certain libraries or transitive dependencies to avoid conflicts or reduce the size of our final build. Gradle provides mechanisms to exclude libraries globally across all dependencies.
In this tutorial, we’ll explore how to exclude a library or a transitive dependency using the Kotlin DSL syntax in a Gradle build script.
Dependencies in a Gradle project often include other libraries as transitive dependencies. However, there are several reasons why we might need to exclude specific libraries:
By understanding these common scenarios, we can effectively manage the dependency graph and ensure only the required libraries are included in the build.
Gradle provides flexible ways to exclude unwanted libraries or transitive dependencies in a Kotlin DSL build script. We can achieve this either globally, across all dependencies, or at a more granular level for specific dependencies. The exclusion mechanisms leverage the configurations and dependencies blocks, allowing us to control the dependency graph precisely.
In the following sections, we’ll explore how to exclude libraries globally or for individual dependencies using the Kotlin DSL syntax.
To exclude a library or transitive dependency globally across all dependencies, we use the configurations block in our build.gradle.kts file:
configurations.all {
exclude(group = "org.apache.commons", module = "commons-logging")
}
In this example, the exclude function specifies the group and module of the dependency we want to remove. The group refers to the group ID, and the module refers to the artifact ID of the library.
If we only want to exclude a library from a particular dependency instead of applying it globally, we use the dependencies block with an inline exclude function:
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web") {
exclude(group = "org.apache.commons", module = "commons-logging")
}
}
In this example, the spring-boot-starter-web dependency includes commons-logging as a transitive dependency. By adding the exclude block, we remove commons-logging specifically for this dependency while leaving other dependencies unaffected.
To exclude multiple libraries, we can add multiple exclude statements inside the configurations block:
configurations.all {
exclude(group = "org.apache.commons", module = "commons-logging")
exclude(group = "ch.qos.logback", module = "logback-classic")
}
Alternatively, for better readability and maintainability, we can define a list of exclusions and loop over them:
val exclusions = listOf(
"org.apache.commons" to "commons-logging",
"ch.qos.logback" to "logback-classic"
)
configurations.all {
exclusions.forEach { (group, module) ->
exclude(group = group, module = module)
}
}
This approach is particularly useful when dealing with a large number of exclusions, as it keeps the code clean and easier to maintain.
To verify that exclusions are applied, we can use Gradle’s dependency insight task:
./gradlew dependencies --configuration implementation
This will display the resolved dependency graph, helping us confirm that the unwanted libraries are excluded.
In this article, we explored how to exclude a library or transitive dependency in Gradle using Kotlin DSL. Whether globally or for a specific dependency, Gradle provides flexible options to manage exclusions effectively.
By carefully applying exclusions, we can resolve dependency conflicts, optimize our build, and ensure compatibility with desired libraries.