GradleUp / shadow

Gradle plugin to create fat/uber JARs, apply file transforms, and relocate packages for applications and libraries. Gradle version of Maven's Shade plugin.
https://www.gradleup.com/shadow/
Apache License 2.0
3.73k stars 393 forks source link

Partial ConfigureShadowRelocation task #330

Open maniax89 opened 7 years ago

maniax89 commented 7 years ago

Basically, I have been trying to figure out a way to create an partial "uber" jar. The documentation clearly states that by default it will create an "uber" jar from the shadowJar task, but I was interested in the proper way to only include one of the dependencies, say dependency A (as well as A's dependencies) AND relocate all the packages of dependency A as well. I believe that http://imperceptiblethoughts.com/shadow/#using_shadow_to_package_gradle_plugins adds in the ConfigureShadowRelocation task which achieves my goal, but it seems to do it for ALL dependencies (not sure how to just pick one dependency, dependency A).

Configuring the relocation has always been possible, but the build author is required to know all the package names before hand. Shadow v2.0 corrects this by introducing a new task type ConfigureShadowRelocation. Tasks of this type are configured to target a an instance of a ShadowJar task and run immediately before it.

See below for my configuration

Shadow Version

2.0.1

Gradle Version

Gradle 4.1

Gradle Build Script(s)

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.1'
    }
}

apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: 'java'

dependencies {
  compile "A"
  compile "B"
  compile "C"
}

shadowJar {
  //what do i put here to just do relocation for "A"
}
maniax89 commented 7 years ago

So here's my new configuration:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.1'
    }
}

apply plugin: 'com.github.johnrengelman.plugin-shadow'
apply plugin: 'java'

configurations {
    shadowAndCompile
    compile.extendsFrom(shadowAndCompile)
}

dependencies {
    shadowAndCompile 'A'
    compile 'B'
    compile 'C'
}

configureRelocationShadowJar {
    prefix = "somecustomprefix"
}

shadowJar {
    configurations = [project.configurations.shadowAndCompile]
}

artifacts {
    archives shadowJar
}

uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: mavenLocal().url)
            pom.whenConfigured {
                p -> p.dependencies = p.dependencies.findAll {
                    dep -> dep.groupId != "A"
                }
            }
            pom.groupId = "my.group"
            pom.artifactId = "my-artifact"
            pom.version = "1.0.0"
        }
    }
}

Does this look like the right way to go about this?

I wasn't sure how to make the uploadShadow task to only remove the shadowed dependencies from the POM (and leave the rest of the compile dependencies)

johnrengelman commented 6 years ago

Your solution looks good for the compilation part. You might not need the pom configuration if you change compile.extendsFrom(shadowCompile) to compileClasspath.extendsFrom(shadowCompile)