Jump to content

Recommended Posts

Posted (edited)

Hi all, 

I have the following issue: I'd like to parse some json using gson and rely on the (somewhat new) java record types. This is supported in gson 2.10+. Gson is already a dependency used by minecraft, however it's 2.8.x for 1.19.2 which I'm targeting at the moment.

My idea was to include the newer version of the library in my mod and then shadow it so it gets used inside the scope of my mod instead of the older 2.8.
This works fine for building the jar: If I decompile my mod.jar, I can see that it's correctly using the shadowed classes.

However obviously when using the runClient intellj config, the shadowing doesn't get invoked. Is there any way of invoking shadow when using runClient, or am I on the wrong track and there's a better way of doing this entirely?

 

Thanks in advance!

 

Edit: After some further thinking, I've come up with this abomination:
 

build.gradle

// New task for extracting the result of shadowJar into the classes directory
// This includes our shadowed gson jar
tasks.register("extractShadowJar", Copy) {
	// Depend on shadowJar so we always use the up to date version of the shadowed content
    dependsOn shadowJar
    from(zipTree(shadowJar.archiveFile)) {
        // filter to copy only our code (and ignore assets, META-INF, etc)
        // Also copies gson as it gets shadowed into com.oppendev.shadow.gson
        include "com/**"
    }
    duplicatesStrategy(DuplicatesStrategy.INCLUDE)
    into("$buildDir/classes/java/main") // Extract into the classes directory
}

// Tell gradle to invoke our new task before executing any java code. This way we ensure that we use the shadowed gson
tasks.withType(JavaExec).configureEach {
    dependsOn(extractShadowJar)
}

// Shadow config
shadowJar {
    relocate 'com.google.gson', 'com.oppendev.shadow.gson'
    configurations = [project.configurations.runtimeClasspath]
    zip64 true

    dependencies {
        include(dependency('com.google.code.gson:gson'))
    }
}

Is this a reasonable thing to do? Is this completely cursed and I should burn in dev ops hell? Is there a better way to do this? Feel free to grill me in the replies :D

Edited by OppenDev

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.