@@ -5,6 +5,10 @@ import java.util.concurrent.atomic.AtomicInteger
55
66buildscript {
77 repositories {
8+ def mavenCentralMirror = providers. gradleProperty(' mavenCentralMirror' ). getOrNull()
9+ if (mavenCentralMirror) {
10+ maven { url = mavenCentralMirror }
11+ }
812 gradlePluginPortal()
913 google()
1014 }
@@ -14,17 +18,44 @@ buildscript {
1418 }
1519}
1620
21+ def globalShadowExclusions = []
22+ if (rootProject. hasProperty(' shadowExclusions' )) {
23+ def rawShadowExclusions = rootProject. findProperty(" shadowExclusions" )
24+ if (! (rawShadowExclusions instanceof String )) {
25+ throw new RuntimeException (" Property 'shadowExclusions' must be a String but was: ${ rawShadowExclusions.getClass().name} " )
26+ }
27+ globalShadowExclusions = rawShadowExclusions. split(" ," )
28+ }
29+ subprojects {
30+ ext {
31+ shadowExclusions = globalShadowExclusions. collect()
32+ }
33+ }
34+
1735def relocatedProjects = projectsWithFlags(' java' , ' relocate' )
1836def numConfiguredRelocatedProjects = new AtomicInteger ()
1937configure(relocatedProjects) {
38+
39+ // per-dependency shade configuration
40+ // while shade per-project shades all dependencies, shade per-dependency allows for finer control
41+ def shadeConfig = project. configurations. create(" shade" ) {
42+ canBeResolved = true
43+ canBeConsumed = false
44+ }
45+ project. configurations. api. extendsFrom(shadeConfig)
46+
2047 // Generate the shaded JARs.
2148 task shadedJar(
2249 type : ShadowJar ,
2350 group : ' Build' ,
2451 description : ' Builds the shaded main JAR.' ,
2552 dependsOn : tasks. classes) {
2653
27- configureShadowTask(project, delegate, true )
54+ def self = this
55+ def delegate0 = delegate
56+ project. afterEvaluate {
57+ self. configureShadowTask(project, delegate0, true )
58+ }
2859 archiveClassifier. set(' shaded' )
2960
3061 // Exclude the legacy file listing.
@@ -49,14 +80,6 @@ configure(relocatedProjects) {
4980 return false
5081 }
5182
52- def shadowExclusions = []
53- if (rootProject. hasProperty(' shadowExclusions' )) {
54- shadowExclusions = rootProject. findProperty(' shadowExclusions' ). split(" ," )
55- }
56- shadowExclusions. each {
57- exclude it
58- }
59-
6083 // Set the 'Automatic-Module-Name' property in MANIFEST.MF.
6184 if (project. ext. automaticModuleName != null ) {
6285 doFirst {
@@ -92,7 +115,11 @@ configure(relocatedProjects) {
92115 description : ' Builds the shaded test JAR.' ,
93116 dependsOn : tasks. testClasses) {
94117
95- configureShadowTask(project, delegate, false )
118+ def self = this
119+ def delegate0 = delegate
120+ project. afterEvaluate {
121+ self. configureShadowTask(project, delegate0, false )
122+ }
96123 archiveBaseName. set(" test-${ tasks.jar.archiveBaseName.get()} -shaded" )
97124 }
98125
@@ -335,9 +362,18 @@ private void configureShadowTask(Project project, ShadowJar task, boolean isMain
335362 // e.g. WEB-INF/lib/hello.jar
336363 exclude ' **/*.jar'
337364 }
365+ project. ext. shadowExclusions. each {
366+ exclude it
367+ }
338368
339369 configurations = [project. configurations[isMain ? ' compileClasspath' : ' testCompileClasspath' ]]
340370
371+ def shadeConfig = project. configurations. findByName(" shade" )
372+ def shadedDeps = shadeConfig ?
373+ shadeConfig. dependencies. collect { " ${ it.group} :${ it.name} " }. toSet() :
374+ [] as Set<String >
375+
376+
341377 relocations. each { props ->
342378 task. relocate props[' from' ], props[' to' ]
343379 }
@@ -361,8 +397,10 @@ private void configureShadowTask(Project project, ShadowJar task, boolean isMain
361397 }
362398 }
363399 } else { // hasFlags('relocate')
364- // Only want to rewrite source references, not bundle dependencies.
365- exclude { true }
400+ // Only want to shade if explicitly specified on the dependency
401+ exclude {
402+ return ! shadedDeps. contains(" ${ it.moduleGroup} :${ it.moduleName} " )
403+ }
366404 }
367405 }
368406 }
0 commit comments