Exclude dependency in all modules except one in Gradle - 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 }
}
}

Related

Remove transitive classpath dependency - plugins block

I want to remove the log4j transitive dependency from the shadow gradle plugin.
plugins {
id 'com.github.johnrengelman.shadow' version '7.1.2'
}
I search for a solution but did not get any. I know I can remove this using the following code -
buildscript {
repositories {
gradlePluginPortal()
}
dependencies {
classpath 'gradle.plugin.com.github.jengelman.gradle.plugins:shadow:7.1.2', {
exclude module: 'log4j'
}
}
}
But is there any way to do this with plugins block?
No, the plugins { } is itself a DSL which is limited by design. This is documented in the documentation:
https://docs.gradle.org/current/userguide/plugins.html#sec:constrained_syntax
The plugins {} block does not support arbitrary code. It is constrained, in order to be idempotent (produce the same result every time) and side effect free (safe for Gradle to execute at any time).
It is designed to do one thing and one thing only: apply plugins.
I used following code to exclude the dependency -
buildscript {
repositories {
..
}
dependencies {
..
}
configurations.classpath {
exclude module: 'log4j'
}
}
plugins {
id 'com.github.johnrengelman.shadow' version '7.1.2'
}

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

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.

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 to set a dependency for a project with all subprojects?

I don't like that I repeat every repository dependency (let us say, junit), for the main project and for subprojects. Is there a possibility to make the subproject to use the dependencies of the main project. Or is there another way to escape that repetition?
Unlike the accepted answer it's better to use the following:
allprojects {
plugins.withType(JavaPlugin) {
dependencies {
testCompile 'junit:junit:4.12'
}
}
}
The changes will be applied immediately if java plugin already exists or will watch for it to be added and apply later.
Updated
At the moment I use better way to control configuration for plugin - pluginManager. The effect is the same as for plugins.withType, but you don't have to know plugin's class name:
Example for org.springframework.boot plugin:
apply plugin: 'org.springframework.boot'
allprojects {
pluginManager.withPlugin('org.springframework.boot') {
springBoot {
buildInfo()
layout 'DIR'
}
}
}
root/build.gradle
allprojects {
if (plugins.hasPlugin('java')) {
dependencies {
testCompile 'junit:junit:4.12'
}
}
}

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