Skip to content

Instantly share code, notes, and snippets.

@valarpirai
Created July 16, 2024 03:17
Show Gist options
  • Save valarpirai/953dd3e5197366a805a22cdb569813e5 to your computer and use it in GitHub Desktop.
Save valarpirai/953dd3e5197366a805a22cdb569813e5 to your computer and use it in GitHub Desktop.

Problem statement

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.

Solution

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 Jar

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

Plugin

Gradle Shadow Plugin

Benefits of Shadow

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

Steps

  1. Create new module (shadow-module) in your Java/Kotlin project
  2. Create build.gradle.kt in shadow-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")
}

  1. 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment