How to exclude multiple groups when using dependencies with gradle - 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)}"
}
}

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

Gradle 5 QueryDsl Generating Duplicate Classes

I am using queryDsl to generate Q classes with Gradle. It used to work fine with Gradle 3.5, but on upgrading to Gradle 5.5.1, it is failing with duplicate class error.
My generateQueryDsl task works fine generating the classes under 'gensrc/' but on compileJava, the classes are generated again under 'build/generated/' which ends up giving duplicate class error.
dependencies {
api("org.springframework.boot:spring-boot-starter-data-jpa") {
exclude group: "org.hibernate", module: "hibernate-entitymanager"
exclude group: "org.hibernate", module: "hibernate-core"
exclude group: "org.apache.tomcat", module: "tomcat-jdbc"
}
api("com.zaxxer:HikariCP:${hikaricpVersion}")
api("com.h2database:h2:1.4.193")
api("mysql:mysql-connector-java")
api("com.microsoft.sqlserver:sqljdbc42:6.0.8112")
api("org.springframework.data:spring-data-jpa")
api("org.springframework:spring-jdbc")
api("org.springframework:spring-orm")
api("org.eclipse.persistence:javax.persistence:${eclipseLinkPersistenceVersion}")
api("org.eclipse.persistence:eclipselink:${eclipseLinkVersion}")
api("org.eclipse.persistence:org.eclipse.persistence.jpa:${eclipseLinkVersion}")
api("com.mysema.querydsl:querydsl-sql:${queryDslVersion}")
api("com.mysema.querydsl:querydsl-jpa:${queryDslVersion}")
api("com.mysema.querydsl:querydsl-apt:${queryDslVersion}")
annotationProcessor('com.mysema.querydsl:querydsl-apt:3.7.4:jpa')
annotationProcessor("org.springframework.boot:spring-boot-starter-data-jpa")
}
task generateQueryDSL(type: JavaCompile, group: 'build) {
source = sourceSets.main.java
classpath = configurations.compileClasspath
options.annotationProcessorPath = configurations.annotationProcessor
destinationDir = file('gensrc/main/java')
}
compileJava {
dependsOn generateQueryDSL
}
error: duplicate class: com.persistence.domain.model.QOrganizationBasedModel
and likewise for all generated classes
When you use the annotationProcessor configuration, the default compileJava task adds the processor to the compiler, and it will generate classes in build/generated/sources/annotationProcessor/java/main.
In your case, you also declare an additional JavaCompile task, which you give the same annotationProcessor configuration, which will then generate the same classes again.
To solve this, I would simply delete generateQueryDSL task entirely as compileJava most likely does everything you need already. And if you like the generated sources in a different folder, you can do that through CompileOptions, but I would recommend having them under the build folder for most cases.

Gradle war plugin pulls in javadoc and sources

I have a strange problem. I have a project which creates a war file with some custom inclusions like images etc. So far it looks good. The only problem left is that gradle pulls in source jars/zips and javadoc jars/zip into my WEB-INF/lib/ folder of my war.
I thought it might be a problem with Idea but same results with the command line. I guess it has something to do with the dependency configuration?
I use compile and runtime scopes and my artifacts are resolved from Artifactory.
Can anyone point me to a direction where to fix that?
Update:
When i create a task:
task copyAllDependencies(type: Copy) {
from configurations.runtime
into 'allRuntime'
}
or
task copyAllDependencies(type: Copy) {
from configurations.compile
into 'allCompile'
}
I'll get the sources as well. So it seems that it has something to do with the compile/runtime configuration. They're pulling the sources and javadoc. But why?!
Dependencies are declared like this:
dependencies {
compile group: 'org.drools', name: 'drools-core', version: DROOLS_VERSION
compile group: 'org.drools', name: 'drools-compiler', version: DROOLS_VERSION
...
runtime group: 'net.sourceforge.barbecue', name: 'barbecue', version: '1.5-beta1', ext: 'jar'
...
testCompile group: 'org.fitnesse', name: 'fitnesse', version: '20130531'
...
}
Here's another attempt... a bit hacky but might work
configurations {
tempCompile
tempRuntime
tempTestCompile
}
dependencies {
tempCompile "org.drools:drools-core:${DROOLS_VERSION}"
tempRuntime "net.sourceforge.barbecue:barbecue:1.5-beta1#jar"
tempTestCompile "org.fitnesse:fitnesse:20130531"
...
compile configurations.tempCompile.asFileTree.matching {
exclude '**/*-sources.jar'
exclude '**/*-javadoc.jar'
}
runtime configurations.tempRuntime.asFileTree.matching {
exclude '**/*-sources.jar'
exclude '**/*-javadoc.jar'
}
testCompile configurations.tempTestCompile.asFileTree.matching {
exclude '**/*-sources.jar'
exclude '**/*-javadoc.jar'
}
}
As we discovered in the comments, your dependencies are bringing in javadoc and sources as transitive dependencies. You can possibly exclude these by
configurations.all { Configuration config ->
['com.group1', 'com.group2', ..., 'com.groupN'].each { groupId ->
config.exclude [group: groupId, classifier: 'javadoc']
config.exclude [group: groupId, classifier: 'sources']
}
}
Note: I'm not an ivy user so the selector (classifier: 'javadoc' etc) may need tweaking

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