How to exclude transitive jar dependency in Gradle? - java

My project depends on dependency A and dependency B.
Dependency A also depends on dependency B, but a different version of it.
The problem is in project A's build.gradle dependency B is included as a jar directly using the file and fileTree methods:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile files('libs/B.jar')
}
My project's build.gradle looks like this:
dependencies {
compile 'com.B:B:myvesion'
compile('com.A:A:theirversion') {
exclude module: 'B'
}
}
But my build fails with this exception:
Execution failed for task ':myproject:transformDexArchiveWithDexMergerForDebug'.
> com.android.build.api.transform.TransformException: com.android.dex.DexException: Multiple dex files define Lcom/B/someclass;
I want to exclude the jar version of B from my project and use my version. How do I do this? Seems like the exclude instruction doesn't work in my case.

You can't do it for the jar files.
You can exclude transitive dependencies (described in the pom file). The jar file is not a transitive dependency.

Related

Gradle 'exclude' local `fileTree` based transitive dependency from sub-project

For some weird reasons, I'm using local filetree based jars in the gradle classpath.
from sub-module(project), I want to exclude one of the local jar. but I'm missing the syntax for it, can someone provide me correct syntax to exclude the local jar file(s) as a transitive dependency here?
dependencies {
implementation(project(":my-simple-project")) {
exclude fileTree(dir: "../lib/", include: ["axis2-1.7.8/axis2-transport-http-1.7.8.jar"])
}
}
OR
dependencies {
implementation(project(":my-simple-project")) {
exclude files("../lib/axis2-1.7.8/axis2-transport-http-1.7.8.jar"])
}
}
I got an error as
* What went wrong:
A problem occurred evaluating project ':my-simple-project'.
> Could not find method exclude() for arguments [directory '../lib/'] on DefaultProjectDependency{dependencyProject='project ':my-simple-project'', configuration='default'} of type org.gradle.api.internal.artifacts.dependencies.DefaultProjectDependency.
You can only exclude module dependencies, not file dependencies.
If you cannot resolve your dependencies from public repositories like Maven Central or JCenter, you can create a local Maven repository (even inside your project structure) and resolve the files directly from there:
repositories {
maven {
url uri("${projectDir}/lib")
}
}
dependencies {
implementation 'org.apache.axis2:axis2-transport-http:1.7.8'
}
Please note, that you must modify the directory structure in your lib folder to match the Maven directory structure (or alternatively define an Ivy repository with a custom structure).
You can exclude nested jar using the following syntax:
fileTree(dir: 'lib').exclude {details ->
(details.file.canonicalPath.contains("axis2-1.7.8")
&& details.file.canonicalPath.contains('axis2-transport-http-1.7.8'))}
OR
fileTree(dir: 'lib').exclude { details ->
details.file.text.contains('axis2-transport-http')
}
Reference : Gradle fileTree exclude all except certain directories

How to exclude transitive dependency from liberty-server build.gradle file

I got 2 java projects and 1 server all three having build.gradle file defining and configuring the dependencies I need in my project.
Code snippet of Liberty server build.gradle is as follows:
configuration{
project1
project2
oracle
extrasecuritystuff
}
dependencies{
project1: "fskfksd"
project2: "sdfd"
extrasecuritystuff 1."fsfd"
2."ewrwer"
}
doTask{....
My question is how do I exclude a transitive dependency present in 2."ewrwer". What is the groovy/gradle syntax for doing so?
Based on the gradle docs I tried something like this but did not work out, once I triggered the Jenkins build It was still pulling in the transitive dependency jars.
configurations {
project1
project2
extrasecuritystuff {
exclude group: 'javax.jms', module: 'jms'
exclude group: 'com.sun.jdmk', module: 'jmxtools'
}
}
dependencies {
project1 'org.javax.etc'
project2 'org.blah.blah'
extrasecuritystuff 'log4j:log4j:1.2.15'
}
I'm assuming here that you are using some plugin like the 'application' plugin to generate a .zip file of your project, and that your goal is to exclude the transitive dependency from the .zip file. The reason could be that the transitive dependency has an undesirable license, or is too large, and your code is never actually calling functions that need the transitive dependency. (Make absolutely 100% double sure of this, or your application will crash.) Or, the target system already has that dependency installed. (Likewise.)
You had a few syntax issues in your example code, but I've interpreted the question according to my understanding. In the example, my dependency ewrwer is pulling in lib-foo which I don't want.
configuration{
project1
project2
extrasecuritystuff
}
dependencies{
project1 ( [...] )
project2 ( [...] )
extrasecuritystuff ('org.blah:fsfd:1.0.1')
extrasecuritystuff ('org.bloop:ewrwer:2.12') {
exclude group: 'org.bleep', module: 'lib-foo'
}
}
For more about managing transitive dependencies, as always refer to the official Gradle docs.

what's the gradle alternative to a fat JAR?

If transitive libs aren't packaged with the JAR task:
By default, jar task in gradle builds an executable jar file from your project source files. It will not contain any transitive libs that are needed for your program.
To the contrary, Netbeans does package JAR dependencies or transitive libs. Rather than a fat JAR how does gradle include libs?
plugins {
id 'com.gradle.build-scan' version '1.8'
id 'java'
id 'application'
}
mainClassName = 'net.bounceme.dur.mbaas.json.Main'
buildScan {
licenseAgreementUrl = 'https://gradle.com/terms-of-service'
licenseAgree = 'yes'
}
repositories {
jcenter()
}
jar {
manifest {
attributes 'Main-Class': 'net.bounceme.dur.mbaas.json.Main'
}
}
dependencies {
//compile fileTree(dir: 'libs', include: ['*.jar'])
runtime group: 'com.google.firebase', name: 'firebase-admin', version: '5.2.0'
compile fileTree(dir: 'lib', include: '*.jar')
}
in relation to another question: what's the "way" to package libs with the JAR task which doesn't result in a "fat JAR"?
When using the application plugin, all libraries used by the application (own jar or transitive libraries) are put in a libs folder when packaged as an app. then these libs are referenced in the shell or batch script as classpath when launching the app.
The easiest way to create a fatjar in gradle, is to use the shadow plugin available via the plugin portal. see https://plugins.gradle.org/plugin/com.github.johnrengelman.plugin-shadow for details

Dependency resolution for classes with same package structure in Gradle

Say I have a project ProjectA with a compile dependency on core. And core depends on deepcore. Thus, ProjectA has a transitive dependency on deepcore.
So, build script for ProjectA has this
dependencies {
compile "com.something:core:1.0.0"
}
And build script for core has this
dependencies {
compile "com.something:deep-core:1.0.0"
}
Now, there is a class CoreService defined in both core and deepcore with the same package structure. And I am using that class from my ProjectA, which implementation will it use? How do configure my dependency so that I am using the version from core?
This should do what you are looking for.
dependencies {
compile "com.something:deep-core:1.0.0" {
exclude group: 'com.unwanted', module: 'unwanted'
}
}

Gradle: excluding a jar from runtime dependencies

I need to exclude a jar from runtime dependency via Gradle.
I am getting this error:
Caused by: java.lang.IllegalStateException: Conflicting persistence unit definitions for name 'ldb-jpa': file:/D:/EricFrancis/shared/build/libs/shared.jar, file:/D:/EricFrancis/shared/build/resources/main
I'm trying to exclude the jar.
How do I tell gradle to do this?
Without more information (Gradle version, relevant parts of build script, etc.), it's hard to say. But since this isn't a Maven or Ivy dependency, I'd consider not adding it in the first place.
It turns out that I did not understand how configurations worked.
I was able to exclude the jar via:
configurations {
testRuntime {
exclude module: 'share'
}
testCompile {
exclude module: 'share'
}
}

Categories

Resources