Not able to exclude dependency gradle - gradle

I need to exclude slf4j dependency from io.confluent:kafka-schema-registry:5.3.0 . I have tried using
implementation ('io.confluent:kafka-schema-registry:5.3.0') {
exclude(group='org.slf4j',module='slf4j-loh4j12')
}
But i keep getting an error
Cannot set the value of read-only property 'group' for DefaultExternalModuleDependency{group='io.confluent', name='kafka-schema-registry', version='5.3.0', configuration='default'} of type org.gradle.api.internal.artifacts.dependencies.DefaultExternalModuleDependency.
Can anyone please tell me how to achieve this. I have tried multiple way but not able to do it

The syntax for exclude() is incorrect. You must use : instead of =. exclude() takes a Map as input, thus, in Groovy DSL, it must be written as:
implementation ('io.confluent:kafka-schema-registry:5.3.0') {
exclude(group: 'org.slf4j', module: 'slf4j-log4j12')
}

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!

Dev mode dependency for gradle in IDEA

I want to add dependency on spring-boot-devtools but only for development.
I try to achieve this by having this snippet in my build.gradle:
if (project.hasProperty('use-spring-boot-devtools')) {
compile 'org.springframework.boot:spring-boot-devtools'
}
Then I can define in my ~/.gradle/gradle.properties
use-spring-boot-devtools = true
Unfortunately this doesn't work when I run import project in IDEA. I would like to use answer to related question but still can't figure out how to define environment variable that will affect gradle inside IDEA.
Do not use hyphens to concat your key in the gradle.properties. Instead define it in camel case:
useSpringBootDevtools=true
And for your build.gradle file, use the following syntax for your conditional dependency:
if(useSpringBootDevtools.toBoolean())
{
// your conditional dependency here
}
Make sure to append toBoolean() to your key since it's not casted automatically by Gradle.

localGroovy method within plugin groovy code

In my plugins, I am always trying to add Groovy to Java. Now this code works just fine when in a build.gradle file, but when I move it to a plugin groovy file so I can compile it and distribute it, it says it can't find the method localGroovy(). What's the correct syntax to call the localGroovy() method here?
project.with {
plugins.withType(JavaPlugin) {
//noinspection GroovyAssignabilityCheck
configurations.all {
resolutionStrategy {
force localGroovy()
}
}
}
}
The method localGroovy() is defined in the DependencyHandler interface. I don't know where in your build script you were using the above code snippet, but you would need to be in the scope of a DependencyHandler to access this method. However, in your plugin, you could just use the DependencyHandler under projects.dependencies:
[...]
resolutionStrategy {
force project.dependencies.localGroovy()
}
[...]
Please note, that I did not test this code and it relies on your statement that the used method force(...) can handle whatever Dependency implementation is returned by localGroovy().
According to the docs, the force(...) method can handle the following inputs:
String in a format of: 'group:name:version', for example: 'org.gradle:gradle-core:1.0'
instance of ModuleVersionSelector
any collection or array of above will be automatically flattened
As long as the Dependency returned by localGroovy() implements the ModuleVersionsSelector interface (e.g. as ExternalDependency), the solution above should work like a charm.

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.

How to use a dependency as file object?

Is there an elegant way to use a specific dependency as a file object (cast dependency to file object).
It is often needed to pass a file as an argument to a task/ant task etc.
I helped me with
configurations.myDependency.files.iterator().next()
But this looks not very intuitive.
I think you mean configuration not dependency. Assuming you have something like:
configurations{
myConf
}
dependencies{
myConf 'mydep:mydep:1.0'
}
Then, if you are confident there is going to be only one file in all of your dependencies for myConf then you can do configurations.myConf.singleFile (return type is File).
However, since configuration can contain multiple files, in order to make your code more robust you should iterate over all files in configurations.myConf.files (return type is Set<File>).
If you need to extract specific dependency jar from a configuration you can do something like:
configurations.myConf.files { dep -> dep.name == 'mydep' }
where dep is of type Dependency and the return type is Set<File>.
For more details see Configuration javadoc.

Resources