Skip gradle dependency if not found - gradle

I am looking for a solution to add optional dependency.
It is possible to add dependency based on property:
if (project.hasProperty("isLocal")) {
implementation("com.example.gradle:dependency1")
}
But I wonder if there isn't something like if dependency exists than use dependency. By default in the absence of dependency I will receive build failed. I would like succeed build for specific libraries which may be missing.

Related

How to excplude module cxf-rt-transports-http

I have the following dependency in build.gradle:
compile ('org.apache.cxf:cxf-bundle:2.4.2') {
exclude module: 'log4j-over-slf4j'
}
I would like to also exclude the module that contains transports-http because of a conflict between libraries (using HTTPConduit). I tried to exclude it, but I don't think I have the right module name
The dependencies task will give you a dependency report where you can see what transitive dependencies are being pulled in.
./gradlew dependencies --configuration compileClasspath
If you were to examine the output of that task, you'll see that org.apache.cxf:cxf-bundle does not appear to bring in cxf-rt-transports-http. So you'll need to examine your project to figure out where the duplicate dependency is.
Gradle also offers a means to handle version conflicts as well: https://docs.gradle.org/current/userguide/viewing_debugging_dependencies.html#sec:resolving-version-conflict

Exclude dependencies by repository

Is there a way to tell Gradle to exclude all dependencies of a particular dependency, that would be pulled from a given repository ?
Removing that repository from the repositories list would not work, because I need that repository for other dependencies. But for one particular group, I want to exclude all dependencies that would come from that repository.
Something like :
dependencies {
compile <my_first_package>
compile('my_second_package') {
exclude repository.name:thirdPartyRepository
}
}
Looking at the source code, there are only two possibilities how to exclude a dependency and that is to exclude by module or a group. You can also mark a dependency not to fetch its transitive dependencies:
compile('my_second_package') {
transitive = false
}
But that's all, there seems to be no way to remove dependencies based on repository. You can force versions using a ResolutionStrategy, maybe take a look if something there helps your use case.
As a workaround, maybe you can repackage the wrong dependency under a different name or version. Or you could use the transitive = false and simply add all the transitive dependencies that you actually want as declared dependencies.

How add more than one dependency in Gradle build file?

This question may be silly but, I could not find a simple example how to add more than one dependency to dependencies closure in a gradle file the example given in the documentations is :
dependencies {
compile 'commons-beanutils:commons-beanutils:1.8.3'
}
What if I want add spring and Hibernate and other ?
dependencies {
compile 'commons-beanutils:commons-beanutils:1.8.3'
compile 'some:other-dependency:1.8.3'
}
You may also want to read Gradle Dependency Management Basics and Advanced Dependency Management

How to write conditional dependency base on build type in gradle

There is dependency in my sub project
dependencies {
...
apt "org.androidannotations:androidannotations:$AAVersion"
...
}
I don't know how to disable this dependency under particular build type.
Maybe I can define different project property, then use them in if-else statement. but... how to define properties according to build type?

Determine source of transitive dependency

I have a project in which I am using sl4j with log4j. I recently added a few new dependencies to my project, and one of these new dependencies that I added is including a transitive dependency to logback-classic, which includes another binding for sj4j.
I want to get rid of logback, but I have no clue which of my direct dependencies added the transitive dependency so that I can exclude it.
In maven I know how to get the entire graph of dependencies to determine which is the source of a transitive dependency, but I have no clue of how to do this with gradle.
Does anyone knows how to get the source dependency of a transitive dependency with gradle?
To show the whole dependency tree for each class path, use:
> gradle dependencies
If you are only interested in a particular class path, use (say):
> gradle dependencies --configuration compile
Use the -p option to run on a sub-project.
To show who pulls in a particular dependency onto a particular class path, and how any version conflicts were resolved, use (say):
> gradle dependencyInsight --dependency logback --configuration compile
Note that you can also exclude a dependency from a whole class path (or multiple). Usually this is more reliable than excluding a particular transitive dependency. For example:
configurations.all*.exclude module: "logback-classic"
An updated answer:
I used this code to resolve a dependency issue between logback and log4j:
configurations.all {
resolutionStrategy.dependencySubstitution {
substitute module('org.apache.logging.log4j:log4j-slf4j-impl') using module ('ch.qos.logback:logback-classic:1.2.3')
}
}
This solution finds any dependencies on log4j-slf4j-impl and instructs it to choose the one from logback (this is a spring app). This solution was surprisingly difficult to track down, but likely very useful in many situations.
Here's the gradle documentation on handling conflicting candidates.

Resources