forked from apache/kudu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shadow.gradle
141 lines (125 loc) · 5.95 KB
/
shadow.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
// This file contains tasks and configuration to support shading dependencies
// consistently when a subproject requires shaded artifacts.
import org.gradle.api.internal.artifacts.publish.ArchivePublishArtifact
apply plugin: "com.github.johnrengelman.shadow"
knows.enabled = false // Disable the "easter egg" knows task.
knows.group = "" // Hide the "easter egg" knows task.
shadowJar.group = "" // Hide shadowJar task since it's used by the default build.
// Configure a shaded jar to replace the default jar
shadowJar.classifier = null // Configure shadow jar to have the default classifier.
jar.finalizedBy(shadowJar) // Generate the shaded jar anytime the jar task is run.
jar.classifier = "unshaded" // Add an unshaded classifier to the default jar.
// Add the shadowJar to the published artifacts.
artifacts {
archives shadowJar
}
// Remove the unshaded jar from the published artifacts.
configurations.archives.artifacts.removeAll {
it instanceof ArchivePublishArtifact && it.archiveTask == jar
}
// Ensure we always relocate these shaded dependencies to the same
// location across all modules.
shadowJar {
relocate "com.google.common", "org.apache.kudu.shaded.com.google.common"
relocate "com.google.gradle.osdetector", "org.apache.kudu.shaded.com.google.gradle.osdetector"
relocate "com.google.gson", "org.apache.kudu.shaded.com.google.gson"
relocate "com.google.protobuf", "org.apache.kudu.shaded.com.google.protobuf"
relocate "com.google.thirdparty", "org.apache.kudu.shaded.com.google.thirdparty"
relocate "com.sangupta", "org.apache.kudu.shaded.com.sangupta"
// Pulled in via osdetector.
relocate "kr.motd.maven", "org.apache.kudu.shaded.kr.motd.maven"
relocate "org.apache.http", "org.apache.kudu.shaded.org.apache.http"
relocate "org.apache.commons", "org.apache.kudu.shaded.org.apache.commons"
// Pulled in via Guava.
relocate "org.checkerframework", "org.apache.kudu.shaded.org.checkerframework"
relocate "org.hamcrest", "org.apache.kudu.shaded.org.hamcrest"
relocate "org.HdrHistogram", "org.apache.kudu.shaded.org.HdrHistogram"
relocate "org.jboss.netty", "org.apache.kudu.shaded.org.jboss.netty"
relocate "scopt", "org.apache.kudu.shaded.scopt"
}
// ------------------------------------------------------------------
// Everything below is a "hack" to support partial shading and
// accurate pom generation. At some point this logic should exist
// in the shadow plugin itself.
// https://github.com/johnrengelman/shadow/issues/166
// https://github.com/johnrengelman/shadow/issues/159
// ------------------------------------------------------------------
// Add a configuration to support unshaded compile dependencies.
// By default shadow assumes all dependencies are shaded.
configurations.create("compileUnshaded")
configurations.shadow.extendsFrom(configurations.compileUnshaded)
configurations.compile.extendsFrom(configurations.compileUnshaded)
// We use afterEvaluate to add additional configuration once all the definitions
// in the projects build script have been applied
afterEvaluate {
// Ensure compileUnshaded dependencies are included in the pom.
[install, uploadArchives].each { task ->
task.repositories.each {
configure(it.pom.scopeMappings) {
// The priority value is arbitrary.
addMapping(
MavenPlugin.COMPILE_PRIORITY,
configurations.compileUnshaded,
Conf2ScopeMappingContainer.COMPILE)
}
}
}
// Ensure compileUnshaded dependencies are not compiled into shadowJar.
project.configurations.compileUnshaded.dependencies.each { dep ->
def depStr = "${dep.group}:${dep.name}:${dep.version}"
logger.info "Excluding ${depStr} from being bundled into the shaded jar."
shadowJar {
dependencies {
exclude(dependency(depStr))
}
}
}
}
// Remove the shaded dependencies from the generated pom.
// This hack allows the project to support partially shaded jars,
// where the shadow plugin by default would remove all compile and runtime dependencies.
tasks.withType(Upload) {
def installer = install.repositories.mavenInstaller
def deployer = uploadArchives.repositories.mavenDeployer
// Handle install and deploy in the same way.
[installer, deployer]*.pom*.whenConfigured { pom ->
def filter = shadowJar.getDependencyFilter()
def configs = shadowJar.getConfigurations()
def shadowDependencies = configs.collectMany {
// Find all dependencies included in the shaded jar.
it.resolvedConfiguration.firstLevelModuleDependencies.findAll {
filter.isIncluded(it)
}
}
// Remove the shaded dependencies from the pom.
shadowDependencies.each { shaded ->
def depStr = "${shaded.getModuleGroup()}:${shaded.getModuleName()}:${shaded.getModuleVersion()}"
logger.info "Excluding ${depStr} from the generated pom."
pom.dependencies.removeAll { dep ->
dep.groupId == shaded.getModuleGroup() &&
dep.artifactId == shaded.getModuleName() &&
dep.version == shaded.getModuleVersion()
}
}
// Re-sort the generated maven dependencies to make pom comparisons easier.
pom.dependencies = pom.dependencies.sort { dep ->
"$dep.scope:$dep.optional:$dep.groupId:$dep.artifactId"
}
}
}