Skip to content

Commit 245a701

Browse files
authored
Relocate dependencies in the generated shadowJar (GoogleCloudPlatform#212)
* Add gradle task to relocate dependencies in shaded JAR * Prevent conscrypt shading * Prevent accidental shading for source classes For some reason shadow plugin shades the source classes as well, this should not be the case. In order to prevent this accidental shading of source classes, we relocate the source classes back the original package to exclude them from automatic relocation.
1 parent 8ec593b commit 245a701

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ subprojects {
8888
groovyGradle {
8989
target '*.gradle' // default target of groovyGradle
9090
greclipse()
91-
licenseHeaderFile rootProject.file('buildscripts/spotless.license.gradle'), '(plugins|description)'
91+
licenseHeaderFile rootProject.file('buildscripts/spotless.license.gradle'), '(import|plugins|description)'
9292
}
9393
}
9494

exporters/auto/build.gradle

+21-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
import com.github.jengelman.gradle.plugins.shadow.relocation.SimpleRelocator
1617
plugins {
1718
id "maven-publish"
1819
id "com.github.johnrengelman.shadow"
@@ -41,10 +42,27 @@ dependencies {
4142
}
4243
}
4344

44-
4545
shadowJar {
46+
// gradle warns since explicit or implicit dependency on this shadowJar is not declared
47+
dependsOn ':shared-resourcemapping:shadowJar'
4648
archiveClassifier.set('shaded')
47-
mergeServiceFiles()
49+
// since we relocate source classes back to their original package, we do not want the SPIs to be re-written for relocated package
50+
mergeServiceFiles {
51+
exclude "META-INF/services/io.opentelemetry.sdk.autoconfigure.spi.metrics.ConfigurableMetricExporterProvider"
52+
exclude "META-INF/services/io.opentelemetry.sdk.autoconfigure.spi.traces.ConfigurableSpanExporterProvider"
53+
}
54+
}
55+
56+
// Auto relocation for all dependencies
57+
tasks.named('shadowJar') {
58+
// Prevent 'conscrypt' from being relocated.
59+
// See https://github.com/GoogleCloudPlatform/opentelemetry-operations-java/issues/161#issuecomment-1478831626 for details
60+
// Relocating it back to its original location excludes the package from being automatically relocated to the specified prefix
61+
relocate new SimpleRelocator("org.conscrypt", "org.conscrypt", new ArrayList<String>(), new ArrayList<String>())
62+
relocate new SimpleRelocator("com.google.cloud.opentelemetry.auto", "com.google.cloud.opentelemetry.auto", new ArrayList<String>(), new ArrayList<String>())
63+
64+
enableRelocation true
65+
relocationPrefix 'shadow'
4866
}
4967

5068
publishing {
@@ -58,4 +76,4 @@ publishing {
5876
}
5977
}
6078
}
61-
}
79+
}

0 commit comments

Comments
 (0)