Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude library from all dependencies in Kotlin DSL build.gradle?

Tags:

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?

like image 894
Demigod Avatar asked May 30 '19 09:05

Demigod


People also ask

How do I exclude in dependencies build Gradle?

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.

How do I exclude tasks in Gradle?

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.

How do I exclude a specific version of a dependency in Maven?

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.

How do I exclude a dependency from a Gradle project?

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 .

How to define dependencies in Kotlin?

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).

What is the difference between Maven and Gradle exclusions?

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.

How do you exclude an artifact in Gradle?

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.


1 Answers

This works with the Gradle Kotlin DSL:

configurations {     all {         exclude(group = "com.google.guava", module = "listenablefuture")     } } 
like image 69
cstroe Avatar answered Oct 26 '22 15:10

cstroe