Exclude a transitive dependency with Gradle’s Kotlin DSL
1 min readAug 16, 2019
Excluding a transitive dependency from something you depend on is often necessary to exclude unwanted libraries. Beware that the unwanted library may be included by more than one dependency.
To exclude JUnit 4 from spring-boot-starter-test
(in favour of JUnit 5) simply do:
dependencies {
testImplementation("org.springframework.boot:spring-boot-starter-test"){
exclude("junit", "junit")
}
}
Check that no other dependencies include JUnit 4:
./gradlew dependencyInsight --dependency junit:junit --configuration testCompileClasspath
Note the --configuration
option. Be sure to specify the correct configuration. If unsure what configuration to use look at the output of any gradle task with the --info
option. For example:
./gradlew projects --info
If you’re still unsure about configurations, just make sure this returns nothing:
./gradlew dependencies | grep junit