Gradle multiple compile dependencies syntax - syntax

I am trying to declare a compile dependency in Gradle 1.12, with multiple items that shares the same exclude clauses (this is to avoid repeating the exclusion everywhere). I know I can do something like this:
configurations {
compile.exclude group: 'com.google.gwt'
all*.exclude group: 'com.google.guava'
}
but this will affect ALL configurations. What I want is something like this (which does not work in Gradle 1.12 as written below):
compile (
["org.jboss.errai:errai-data-binding:2.4.4.Final"]
,["org.jboss.errai:errai-data-ioc:2.4.4.Final"]
){
exclude group: 'com.google.gwt'
exclude group: 'com.google.guava'
}
so I can gather together all dependencies for which I need exclusion in one place, and still be able to have elsewhere this:
compile 'com.google.guava:guava:17.0'
Update:
Just to clarify, my only goal is to replace this piece of code:
compile ('bla.bla.bla:1.0'){
exclude 'same.component:1.0' //Ugly repeat
}
compile ('boo.boo.boo:1.0'){
exclude 'same.component:1.0' //Ugly repeat
}
compile ('uh.uh.uh:1.0'){
exclude 'same.component:1.0' //Ugly repeat
}
compile ('oh.oh.oh:1.0'){
exclude 'same.component:1.0' //Ugly repeat
}
with something short and sweet like this (not working currently):
compile( 'bla.bla.bla:1.0'
,'boo.boo.boo:1.0'
,'uh.uh.uh:1.0'
,'oh.oh.oh:1.0'
)
{
exclude 'same.component:1.0' //Only once! Sweet!
}

There is no way to have per-dependency excludes while still being able to use the compile 'com.google.guava:guava:17.0' syntax. configurations.compile.exclude ... will only affect the compile configuration (and configurations inheriting from it), and is almost always preferable over per-dependency excludes.
Another solution is to factor out dependency declarations with something like:
ext.libs = [
error_data_ioc: dependencies.create("org.jboss.errai:errai-data-ioc:2.4.4.Final") {
exclude group: 'com.google.gwt'
exclude group: 'com.google.guava'
}
]
Then you can reuse these declarations wherever you need them (e.g. dependencies { compile libs.error_data_io }; also works from a subproject). If you really wanted, you could also share the same { exclude ... } block among multiple declarations (by assigning it to a local variable).

Related

Spring boot, exclude dependencies from test in Gradle 5

I want to exclude two jars from test and only use them when the application is actually running.
dependencies {
runtimeOnly('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')
runtimeOnly('org.springframework.cloud:spring-cloud-starter-config')
}
How do I explicitly tell Gradle 5 not to load these jars during the running of tests? I have already disabled their use but they keep getting loaded anyway. I was hoping for something simple like below, but I've been unable to find a conclusive answer.
test.exclude {
group 'org.springframework.cloud'
}
EDIT
Copy paste solution
configurations.testCompile.exclude(group: 'org.springframework.cloud', module: 'spring-cloud-starter-netflix-eureka-client')
configurations.testCompile.exclude(group: 'org.springframework.cloud', module: 'spring-cloud-starter-config')
Inside your dependencies block, you can do something like:
configurations.testCompile.exclude(group: 'the-group', module: 'the-module')
Hope this helps!

How to exclude specific jars when using gradle in IntelliJ

I think there is a function to exculde from the pop-up menu by right-clicking on the screen below.
And I added my own build.gradle like below, but the dependency I want is not removed.
dependencies {
compile ('org.springframework.boot:spring-boot-starter-data-jpa:2.0.5.RELEASE') {
exclude group: 'org.apache.logging', module: 'log4j-to-slf4j'
}
....
....
}
Is that right?
Seems you missed "log4j" at the end of group name: "exclude group: 'org.apache.logging.log4j', module: 'log4j-to-slf4j' "

Gradle Task messes with runtime Dependencies

Another strange behaviour of gradle...
So I've found this post:
Gradle exclude module for Copy task
Totally fine and works like a charm to exclude things from copying.
But here is where it gets interesting. This is how my Copy Task looks:
task copyDependencies(type: Copy) {
into "$buildDir/libs/dependencies"
from configurations.runtime {
exclude module: 'groovy'
exclude module: 'aws-java-sdk-s3'
exclude module: 'commons-io'
}
}
If I try to run the Application through Gradles 'application run' task. It fails with "Main Class xxx couldn't be found or loaded". Digging deeper into the problem I noticed that Groovy couldn't be resolved.
I don't even run this Task, or depend on it.
But if I comment out line 4 like this:
task copyDependencies(type: Copy) {
into "$buildDir/libs/dependencies"
from configurations.runtime {
//exclude module: 'groovy'
exclude module: 'aws-java-sdk-s3'
exclude module: 'commons-io'
}
}
The Application starts like normal, until it reaches a point where it needs Commons-IO. I still want to use this copyDependencies Task at other times, without changing the code there though.
Can somebody explain me this behaviour ?
I imagine manipulating the configuration.runtime anywhere in the gradle file, changes it for every other task ?
In your from configuration block, you are referencing the runtime configuration, but in the same time you are altering this configuration by adding some exclusion rules. This will alter the original (and unique) runtime configuration which will be used by all other tasks in your build project, as you have guessed. This explains the "Main Class xxx couldn't be found or loaded" error you get when trying to execute the run task, since the runtime configuration (classpath) does not contain the needed library.
If you want to write exclusions rules by group and/or module in your copyDependencies task, one possible way would be to work on a copy of the original runtime configuration; you could define a new Configuration for this purpose:
configurations{
runtimeDeps.extendsFrom runtime
}
task copyDependencies(type: Copy) {
into "$buildDir/libs/dependencies"
from configurations.runtimeDeps {
exclude module: 'groovy'
exclude module: 'aws-java-sdk-s3'
exclude module: 'commons-io'
}
}

How can I exclude dependencies brought in from other sub-projects?

This is not a duplicate since those other solutions did not work.
I have a sub-project:
:commons:widget
gradle.build (sub-project) resembles this:
configurations {providedCompile}
dependencies {
compile project(":commons:other-widget")
...other dependencies...
}
If you display the dependencies:
+--- project :commons:some-other-project
+--- project :commons:exclude-me-project (*)
\--- org.apache.cxf:cxf-rt-frontend-jaxrs: -> 3.0.3 (*)
What doesn't work:
Any of the usual syntax. I've tried every variation I can think of. Even went looking for the API but was unable to find what I need there.
In this project's dependencies section:
...
compile project(":commons:some-other-project") {
exclude (":commons:exclude-me-project")
}
Result:
Could not find method exclude() for arguments [:commons:some-other-project] on project
I've also tried:
compile ( project (':commons:some-other-project') ) {
transitive = false
}
Result: Instead of removing dependencies of ":commons:some-other-project", it removes ":commons:some-other-project".
I have a large and complicated project to convert. I've a lot of this sort of work ahead of me. Given a project as a dependency, how do I exclude things from it?
exclude for dependencies has a little bit another syntax, so try to do it providing the module name, which is equals to the exclude-me-project name, like:
compile(project(":commons:some-other-project")) {
exclude module: "exclude-me-project"
}
Alternatively, you may exclude all the transitive dependencies for commons project, but it will remove all the deps of the some-other-project project, including the exclude-me-project:
compile(project(":commons:some-other-project")) {
transitive = false
}
for new gradle syntax you can do something like:
implementation (project(path: ':my_library_v1.0.0')) {
exclude (group: 'com.google.code.gson', module: 'gson')
}
Kotlin DSL answer
This will exclude a subproject from another subproject:
implementation(project(":nice-subproject")) {
exclude(module = "annoying-transitive-subproject")
}
Also note that using exclude on a test fixture dependency requires a cast
implementation(testFixtures(project(":nice-subproject")) as ModuleDependency) {
exclude(module = "annoying-transitive-subproject")
}

Gradle: exclude and include multiple groups/modules in single line from compile closure

this syntax may be not yet provided but I am asking to avoid redundant code.
Right now I am excluding jars like this
compile ('com.mygroup:myJar:0.1.1-M1-SNAPSHOT+') {
exclude group: 'org.apache.xmlgraphics'
exclude group:'org.apache.avalon.framework'
exclude group:'net.engio'
exclude group: 'com.google.guava'
}
How can i exclude multiple groups/modules on a single line of code, for example, this syntax
compile ('com.mygroup:myJar:0.1.1-M1-SNAPSHOT+'){
exclude group: ['org.apache.xmlgraphics', 'org.apache.avalon.framework', 'net.engio', 'com.google.guava']
}
Or is there any other short code which does the same thing.
Thank You.
You could do something like this:
compile ('com.mygroup:myJar:0.1.1-M1-SNAPSHOT+'){
['org.apache.xmlgraphics', 'org.apache.avalon.framework', 'net.engio', 'com.google.guava'].each {
exclude group: it
}
}
Note that this is leveraging a Groovy feature, not a Gradle feature.
Note also that I don't believe that include is a thing in this context (see the method summary here).
compile ('com.mygroup:myJar:0.1.1-M1-SNAPSHOT+'){dep ->
['org.apache.xmlgraphics', 'org.apache.avalon.framework', 'net.engio', 'com.google.guava'].each {group -> dep.exclude group: group }
}
Refer to this.

Resources