Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Publishing a shaded JAR #986

Open
ansman opened this issue Sep 24, 2024 · 0 comments
Open

Publishing a shaded JAR #986

ansman opened this issue Sep 24, 2024 · 0 comments

Comments

@ansman
Copy link

ansman commented Sep 24, 2024

Describe your use-case which is not covered by existing documentation.

I need to shade a specific library that does not maintain binary compatibility, I thus want to publish a JAR that has shadowed and relocated a specific library, while all other libraries are exposes in the POM like normal.

This worked fine until 8.3.1 when #904 was merged.

The issue is that the shadowJar task uses the all classifier by default which isn't supported by non Gradle clients. You can manually set the classifier to null or '' but then it conflicts with the jar task.

It would be great to call this specific use case out in the docs as it's quite common.

Reference any relevant documentation, other materials or issues/pull requests that can be used for inspiration.

My current solution can be found in my auto-dagger project.

Here is the relevant snippet:

pluginManager.withPlugin("com.gradleup.shadow") {
    val compileShaded: Configuration by configurations.creating
    configurations.named("compileOnly") { extendsFrom(compileShaded) }
    configurations.named("testRuntimeOnly") { extendsFrom(compileShaded) }
    // Since we use a custom configuration we make the shadow configuration extend the implementation
    // configuration to correctly include dependencies in the POM
    configurations.named("shadow") { extendsFrom(configurations["implementation"]) }

    // Since we change the classifier of the shadowJar we need to disable the default jar task or we'll get two
    // artifacts that have the same classifier
    tasks.named<Jar>("jar") {
        archiveClassifier.set("ignored")
    }

    tasks.named<ShadowJar>("shadowJar") {
        archiveClassifier.set("")
        configurations = listOf(compileShaded)
        isEnableRelocation = true
        relocationPrefix = "se.ansman.dagger.auto${project.path.replace(':', '.').replace('-', '_')}"
        mergeServiceFiles()
    }

    pluginManager.withPlugin("org.gradle.java-test-fixtures") {
        configurations.named("testFixturesImplementation") { extendsFrom(compileShaded) }
        configurations.named("testFixturesImplementation") { extendsFrom(configurations["implementation"]) }
        configurations.named("testFixturesApi") { extendsFrom(configurations["api"]) }
    }
}

The main downside of this is that other projects that depend on this would have to depend on the shadow configuration which in my case isn't an issue since none of these dependant modules are published but I can imagine it would be an issue if they were.

Are you interested in contributing to the documentation?

No response

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

No branches or pull requests

1 participant