I started migration from build.gradle
(Groovy) to build.gradle.kts
(Kotlin DSL). The thing is that com.google.common.util.concurrent.ListenableFuture
(from com.google.guava
) exists in several dependecies. Because of that build fails with java.lang.RuntimeException: Duplicate class ...
error.
Previously (when I had build.gradle
in Groovy) this problem was solved with this snippet:
configurations { all*.exclude group: 'com.google.guava', module: 'listenablefuture' }
But I can't find anything similar using Kotlin DSL. Could you please provide Kotlin alternative for the snippet above or suggest any other solution on how to deal with this?
Option 1) per-dependency exclude rules. When you specify a dependency in your build script, you can provide an exclude rule at the same time telling Gradle not to pull in the specified transitive dependency. For example, say we have a Gradle project that depends on Google's Guava library, or more specifically com.
To skip any task from the Gradle build, we can use the -x or –exclude-task option. In this case, we'll use “-x test” to skip tests from the build. As a result, the test sources aren't compiled, and therefore, aren't executed.
Multiple transitive dependencies can be excluded by using the <exclusion> tag for each of the dependency you want to exclude and placing all these exclusion tags inside the <exclusions> tag in pom. xml. You will need to mention the group id and artifact id of the dependency you wish to exclude in the exclusion tag.
When you specify a dependency in your build script, you can provide an exclude rule at the same time telling Gradle not to pull in the specified transitive dependency. For example, say we have a Gradle project that depends on Google’s Guava library, or more specifically com.google.guava:guava:30.1.1-jre .
Step 1: Define values in any Kotlin file under buildSrc/src/main/kotlin When you set this up it works like a charm for this purpose. From my research, when project uses Gradle Kotlin DSL, this is the most popular way for defining dependencies versions (often combined with dependencies names next to it).
On the upside, Gradle’s exclude handling is, in contrast to Maven, taking the whole dependency graph into account. So if there are multiple dependencies on a library, excludes are only exercised if all dependencies agree on them.
Within the closure we call exclude, passing: module the name of the artifact we want to exclude. This is equivalent to the name used to declare a dependency in Gradle. It’s also entirely valid to pass only group or only module to match more generically.
This works with the Gradle Kotlin DSL:
configurations { all { exclude(group = "com.google.guava", module = "listenablefuture") } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With