What's the different between all*.exclude and all.exclude in configurations.all - gradle

I want to know what exactly different between all*.exclude and all.exclude in configurations.all when you want to exclude dependencies
configurations.all {
all.exclude
all*.exclude group: 'org.json', module: 'json'
}

The correct syntax is:
Using the all method
configurations.all {
exclude group: 'org.json', module: 'json'
}
OR
Using the all property
configurations {
all*.exclude(group: 'org.json', module: 'json')
}
The all property holds a list of all configuration objects within the project configurations.
If you want to see what it actually it contains you can do:
println configurations.all.names
OR
println configurations.all*.name
And the syntax *. is a groovy specific operator called the spread operator. You can read how that works to understand why it worked here.

Related

Exclude dependency in all modules except one in Gradle

I have a dependency that I need removed from all modules of my application (10 modules), except for one.
In the top level build.gradle file, I have:
configurations.all { exclude group: 'com.nasty', module: 'nasty-dependency' }
Is there an easy way to express that for modules A thru I, I want to exclude this dependency, but in module J include it?
Thanks.
Off the top of my head
[':A', ':B', ':C'].each {
project(it).configurations.all { exclude group: 'com.nasty', module: 'nasty-dependency }
}
Or maybe
allprojects {
if (path !=':X') {
configurations.all { exclude group: 'com.nasty', module: 'nasty-dependency }
}
}

What does this "all*.exclude" means in Gradle transitive dependency?

I wonder what does "all*.exclude" mean in Gradle transitive dependency ?
configurations {
compile.exclude group: 'org.hamcrest', module: 'hamcrest-core'
all*.exclude group: 'org.mockito', module: 'mockito-all'
}
Is "all*.exclude" in the code above syntax of Gradle or some else.
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 dependencies

I want to exclude dependencies from a dependency :
dependencies {
...
implementation "org.springframework.security:spring-security-web:$springSecurityVersion" {
exclude "org.springframework:spring-core:$springSecurityVersion"
exclude "org.springframework:spring-beans:$springSecurityVersion"
exclude "org.springframework:spring-context:$springSecurityVersion"
exclude "org.springframework:spring-web:$springSecurityVersion"
}
...
}
I tried a couple of options by changing the syntax sugar but in vain.
Updated Exception :
org.gradle.tooling.BuildException: Could not run build action using Gradle distribution 'https://services.gradle.org/distributions/gradle-4.1-bin.zip'.
at org.gradle.tooling.internal.consumer.ExceptionTransformer.transform(ExceptionTransformer.java:51)
at org.gradle.tooling.internal.consumer.ExceptionTransformer.transform(ExceptionTransformer.java:29)
at org.gradle.tooling.internal.consumer.ResultHandlerAdapter.onFailure(ResultHandlerAdapter.java:41)
at org.gradle.tooling.internal.consumer.async.DefaultAsyncConsumerActionExecutor$1$1.run(DefaultAsyncConsume
...
Apart from that one more line that I see that could be useful is
Could not find method
org.springframework.security:spring-security-web:5.0.4.RELEASE() for
arguments
[build_8wp04892dra2g2ruliafngaad$_run_closure3$_closure5#28d49bbc] on
object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
Did you try this syntax, without specifying the version :
implementation ("org.springframework.security:spring-security-web:$springSecurityVersion") {
exclude group: 'org.springframework', module: 'spring-core'
exclude group: 'org.springframework', module: 'spring-beans'
exclude group: 'org.springframework', module: 'spring-context'
exclude group: 'org.springframework', module: 'spring-web'
}
edit : Delete your .gradle directory in your home directory and try again
edit2 : answer's fixed, thanks to op's comment

How to exclude multiple groups when using dependencies with gradle

Just like this code:
dependencies {
compile ('com.wdullaer:materialdatetimepicker:3.2.2') {
exclude group: 'com.android.support', module: 'support-v4'
exclude group: 'com.android.support', module: 'design'
}
}
In an android app build.gradle file, when I want to dependency a remote library, how to use exclude group syntax to exclude multiple groups?
Although the code above is a right way, but it`s a little bit complicated.Is there a simpler way?
Well basically 'exclude' is just a method belongs to 'ModuleDependency' class which accepts a 'Map' of 'group' and 'module' and there's no way to pass more.
However you can use 'Groovy' power in this case and for each 'group' from list to call method 'exclude' on 'ModuleDependency' and to pass current 'group'. Take a look at approximate code below.
compile() { dep ->
[group1, group2].each{ group -> dep.exclude group: group }
}
Using this solution with Groovy combinations (Cartesian product) to exclude some list from all configurations:
/**
* These artifacts must be excluded from all configurations and dependencies.
*/
final excludedModules = [
'module-to-exclude-1', 'module-to-exclude-2'
]
/**
* Exclude dependencies from all configurations.
* This configuration solves an issue
* when the same transitive dependency is included by different libraries.
*/
configurations {
[all, excludedModules].combinations { config, moduleToExclude ->
config.exclude module: moduleToExclude
}
}
Example is here:
compile group: 'org.jitsi', name: 'libjitsi', version: '1.0-9-g4b85531', {
[new Tuple('org.opentelecoms.sdp', 'sdp-api'),
new Tuple('junit', 'junit')].each {
exclude group: "${it.get(0)}", module: "${it.get(1)}"
}
}

How can I exclude from Gradle dependency lists after the fact?

This is for a Maven-to-Gradle build conversion on a large build. Think Rodan, Ghidora, Godzilla, etc. Yeah. That big.
Given a dependency that looks like this:
ext.jbossBom = [ ... ]// This is in the root project.
compile (rootProject.ext.jbossBom) //This is not in the root project
How can I exclude items from the above? I've tried variants of:
compile (rootProject.ext.jbossBom) {
exclude group: "some.group", module: "some.module"
}
jbossBom is a collection. Remove the element your want to eliminate:
compile (rootProject.ext.jbossBom.findAll{ !it.startsWith('some.group')})
To exclude a certain transitive dependency globally(irrespective of which dependency brings it in), you can do:
configurations {
compile.exclude group: 'commons-math3', module: 'commons-math3'
compile.exclude group: 'commons-pool2', module: 'commons-pool2'
}
To exclude a specific transitive dependency from each of the contents of jbossBom, you can do:
dependencies{
jbossBom.each{
compile (it){exclude group:'foo', module:'bar'}
}
}

Resources