we are need quickbooks library 6.3.0 and the joda-time library is 2.1 is added as transitive dependency. https://mvnrepository.com/artifact/com.intuit.quickbooks-online/ipp-v3-java-data/6.3.0
In our project, we need joda time library is 2.12.7. https://mvnrepository.com/artifact/joda-time/joda-time/2.12.7
Now, we are facing class path conflict in joda libary.
The solution is to change the base package of joda-time library and it's usage in Quickbooks library.
The Shadow Jar plugin helps us acheive this.
Shadow is a Gradle plugin for combining a project's dependency classes and resources into a single output Jar. The combined Jar is often referred to a fat-jar or uber-jar. Shadow utilizes JarInputStream and JarOutputStream to efficiently process dependent libraries into the output jar without incurring the I/O overhead of expanding the jars to disk
Gradle Shadow Plugin
Shadowing a project output has 2 major use cases:
- Creating an executable JAR distribution
- Bundling and relocating (changing package names) common dependencies in libraries to avoid classpath conflicts
- Create new module (
shadow-module
) in your Java/Kotlin project - Create
build.gradle.kt
inshadow-module
with the following content
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
plugins {
id("java")
id("com.github.johnrengelman.shadow") version "7.0.0"
}
group = "com.example"
version = "0.0.1"
repositories {
mavenCentral()
}
dependencies {
testImplementation(platform("org.junit:junit-bom:5.10.0"))
testImplementation("org.junit.jupiter:junit-jupiter")
implementation("com.intuit.quickbooks-online:ipp-v3-java-data:6.1.2")
}
tasks.test {
useJUnitPlatform()
}
tasks.withType<ShadowJar>() {
relocate("org.joda.time", "com.super.shaded.org.joda.time")
}
- Include the module in your project
// https://mvnrepository.com/artifact/joda-time/joda-time
implementation("joda-time:joda-time:2.12.7")
implementation(project("path" to "shadow-module", "configuration" to "shadow"))
Now, the joda-time 2.17.7 lib is accessible under the original package org.joda.time
.
import org.joda.time.Instant
In the quickbooks lib, all the joda-time lib usage is changed to com.super.shaded.org.joda.time
. Also, we can use older version of joda using the shaded package name.
import com.super.shaded.org.joda.time.Instant
like this