How do I exclude all instances of a transitive dependency when using Gradle?
Ah, the following works and does what I want:
configurations {
runtime.exclude group: "org.slf4j", module: "slf4j-log4j12"
}
It seems that an Exclude Rule only has two attributes - group
and module
.
Hence for excluding from only an individual dependency, we can do something like:
dependencies {
compile ('org.springframework.data:spring-data-hadoop-core:2.0.0.M4-hadoop22') {
exclude group: "org.slf4j", module: "slf4j-log4j12"
}
}
However, the above syntax doesn't prevent you from specifying any arbitrary property as a predicate. When trying to exclude from an individual dependency you cannot specify arbitrary properties. For example, this fails:
dependencies {
compile ('org.springframework.data:spring-data-hadoop-core:2.0.0.M4-hadoop22') {
exclude group: "org.slf4j", name: "slf4j-log4j12"
}
}
with
No such property: name for class: org.gradle.api.internal.artifacts.DefaultExcludeRule
So even though you can specify a dependency with a group:
and name:
you can't specify an exclusion with a name:
!?!
Perhaps a separate question, but what exactly is a module then? I can understand the Maven notion of groupId:artifactId:version, which I understand translates to group:name:version in Gradle. But then, how do I know what module (in gradle-speak) a particular Maven artifact belongs to?
gradle exclude a transitive dependency
ref https://docs.gradle.org/current/userguide/dependency_downgrade_and_exclude.html#sec:excluding-transitive-deps
groovy:
implementation('org.apache.kafka:kafka-streams:2.3.0') {
exclude group: 'org.apache.kafka', module: 'kafka-streams'
}
Kotlin:
implementation("org.apache.kafka:kafka-streams:2.3.0") {
exclude(group = "org.apache.kafka", module = "kafka-streams")
}
As you can see the exclude can only support group: , module:
args - not the single string format g:m:v
.
What does this all*.exclude means in Gradle transitive dependency?
In this context, all*.
refers to all configurations
...
and it applies exclude group: 'org.mockito', module: 'mockito-all'
to all of them.
The all*.
syntax is the short-handed notation of:
configurations {
all.collect { configuration ->
configuration.exclude group: 'org.mockito', module: 'mockito-all'
}
}
The *.
syntax is called "spread-dot operator", which is a Groovy syntax (see paragraph 8.1
).
Exclude transitive dependency of Gradle plugin
You can manipulate the classpath of the buildscript itself through:
buildscript {
configurations {
classpath {
exclude group: 'org', module: 'foo' // For a global exclude
}
}
dependencies {
classpath('org:bar:1.0') {
exclude group: 'org', module: 'baz' // For excluding baz from bar but not if brought elsewhere
}
}
}
How to exclude a dependency from built JAR in Gradle?
I think you want something like this.
dependencies {
compileOnly "com.thesamet.scalapb:scalapb-runtime-grpc_2.13:0.11.1"
}
ref: https://blog.gradle.org/introducing-compile-only-dependencies
What is the proper syntax of excluding transient dependencies in gradle, name or module?
The current documentation (7.4.2) shows module
:
https://docs.gradle.org/7.4.2/userguide/dependency_downgrade_and_exclude.html
implementation('commons-beanutils:commons-beanutils:1.9.4') {
exclude group: 'commons-collections', module: 'commons-collections'
}
name
is probably a legacy naming. Go with what the current documentation has, module
.
Related Topics
Does a Tcp Socket Connection Have a "Keep Alive"
Jcombobox Selection Change Listener
Is It Bad to Explicitly Compare Against Boolean Constants E.G. If (B == False) in Java
Create Java Console Inside a Gui Panel
Why Does the Ternary Operator Unexpectedly Cast Integers
Parsing a Date's Ordinal Indicator ( St, Nd, Rd, Th ) in a Date-Time String
Convert Boolean to Int in Java
How to Implement a Db Listener in Java
How to Read and Understand the Java Stack Trace
Prime Numbers by Eratosthenes Quicker Sequential Than Concurrently
Clone() VS Copy Constructor VS Factory Method
How to Re-Attach Detached Objects in Hibernate