forked from launchdarkly/java-server-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.gradle
400 lines (349 loc) · 14.3 KB
/
build.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
apply plugin: 'java'
apply plugin: 'maven-publish'
apply plugin: 'org.ajoberstar.github-pages'
apply plugin: 'signing'
apply plugin: 'idea'
apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: 'io.codearte.nexus-staging'
configurations.all {
// check for updates every build for dependencies with: 'changing: true'
resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}
repositories {
mavenLocal()
// Before LaunchDarkly release artifacts get synced to Maven Central they are here along with snapshots:
maven { url "https://oss.sonatype.org/content/groups/public/" }
mavenCentral()
}
allprojects {
group = 'com.launchdarkly'
version = "${version}"
sourceCompatibility = 1.7
targetCompatibility = 1.7
}
ext {
sdkBasePackage = "com.launchdarkly.client"
sdkBaseName = "launchdarkly-java-server-sdk"
// List any packages here that should be included in OSGi imports for the SDK, if they cannot
// be discovered by looking in our explicit dependencies.
systemPackageImports = [ "javax.net", "javax.net.ssl" ]
}
ext.libraries = [:]
// Add dependencies to "libraries.internal" that are not exposed in our public API. These
// will be completely omitted from the "thin" jar, and will be embedded with shaded names
// in the other two SDK jars.
libraries.internal = [
"commons-codec:commons-codec:1.10",
"com.google.guava:guava:19.0",
"joda-time:joda-time:2.9.3",
"com.launchdarkly:okhttp-eventsource:1.9.1",
"org.yaml:snakeyaml:1.19",
"redis.clients:jedis:2.9.0"
]
// Add dependencies to "libraries.external" that are exposed in our public API, or that have
// global state that must be shared between the SDK and the caller.
libraries.external = [
"com.google.code.gson:gson:2.7",
"org.slf4j:slf4j-api:1.7.21"
]
// Add dependencies to "libraries.test" that are used only in unit tests.
libraries.test = [
"com.squareup.okhttp3:mockwebserver:3.10.0",
"org.hamcrest:hamcrest-all:1.3",
"org.easymock:easymock:3.4",
"junit:junit:4.12",
"ch.qos.logback:logback-classic:1.1.7"
]
dependencies {
implementation libraries.internal
compileClasspath libraries.external
runtime libraries.internal, libraries.external
testImplementation libraries.test, libraries.internal, libraries.external
// Unlike what the name might suggest, the "shadow" configuration specifies dependencies that
// should *not* be shaded by the Shadow plugin when we build our shaded jars.
shadow libraries.external
}
task wrapper(type: Wrapper) {
gradleVersion = '4.10.2'
}
buildscript {
repositories {
jcenter()
mavenCentral()
mavenLocal()
}
dependencies {
classpath 'org.ajoberstar:gradle-git:1.5.0-rc.1'
classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.1'
classpath "io.codearte.gradle.nexus:gradle-nexus-staging-plugin:0.8.0"
classpath "org.eclipse.virgo.util:org.eclipse.virgo.util.osgi.manifest:3.5.0.RELEASE"
classpath "org.osgi:osgi_R4_core:1.0"
}
}
jar {
baseName = sdkBaseName
// thin classifier means that the non-shaded non-fat jar is still available
// but is opt-in since users will have to specify it.
classifier = 'thin'
// doFirst causes the following step to be run during Gradle's execution phase rather than the
// configuration phase; this is necessary because it accesses the build products
doFirst {
// In OSGi, the "thin" jar has to import all of its dependencies.
addOsgiManifest(project.tasks.jar, [ configurations.runtime ], [])
}
}
// This builds the default uberjar that contains all of our dependencies except Gson and
// SLF4j, in shaded form. The user is expected to provide Gson and SLF4j.
shadowJar {
baseName = sdkBaseName
// No classifier means that the shaded jar becomes the default artifact
classifier = ''
dependencies {
exclude(dependency('org.slf4j:.*:.*'))
exclude(dependency('com.google.code.gson:.*:.*'))
}
// doFirst causes the following steps to be run during Gradle's execution phase rather than the
// configuration phase; this is necessary because they access the build products
doFirst {
shadeDependencies(project.tasks.shadowJar)
// Note that "configurations.shadow" is the same as "libraries.external", except it contains
// objects with detailed information about the resolved dependencies.
addOsgiManifest(project.tasks.shadowJar, [ project.configurations.shadow ], [])
}
}
// This builds the "-all"/"fat" jar, which is the same as the default uberjar except that
// Gson and SLF4j are bundled and exposed (unshaded).
task shadowJarAll(type: com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) {
baseName = sdkBaseName
classifier = 'all'
group = "shadow"
description = "Builds a Shaded fat jar including SLF4J"
from(project.convention.getPlugin(JavaPluginConvention).sourceSets.main.output)
configurations = [project.configurations.runtime]
exclude('META-INF/INDEX.LIST', 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA')
// doFirst causes the following steps to be run during Gradle's execution phase rather than the
// configuration phase; this is necessary because they access the build products
doFirst {
shadeDependencies(project.tasks.shadowJarAll)
// The "all" jar exposes its bundled Gson and SLF4j dependencies as exports - but, like the
// default jar, it *also* imports them ("self-wiring"), which allows the bundle to use a
// higher version if one is provided by another bundle.
addOsgiManifest(project.tasks.shadowJarAll, [ project.configurations.shadow ], [ project.configurations.shadow ])
}
}
task testJar(type: Jar, dependsOn: testClasses) {
classifier = 'test'
from sourceSets.test.output
}
// custom tasks for creating source/javadoc jars
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
githubPages {
repoUri = 'https://github.com/launchdarkly/java-server-sdk.git'
pages {
from javadoc
}
credentials {
username = githubUser
password = githubPassword
}
}
// Returns the names of all Java packages defined in this library - not including
// enclosing packages like "com" that don't have any classes in them.
def getAllSdkPackages() {
def names = []
project.convention.getPlugin(JavaPluginConvention).sourceSets.main.output.each { baseDir ->
if (baseDir.getPath().contains("classes" + File.separator + "java" + File.separator + "main")) {
baseDir.eachFileRecurse { f ->
if (f.name.endsWith(".class")) {
def subPath = f.getPath().substring(baseDir.getPath().length() + File.separator.length())
def pkgName = subPath.substring(0, subPath.lastIndexOf(File.separator)).replace(File.separator, ".")
names += pkgName
}
}
}
}
names.unique()
}
// Returns the names of all Java packages contained in the specified jar - not including
// enclosing packages like "com" that don't have any classes in them.
def getPackagesInDependencyJar(jarFile) {
new java.util.zip.ZipFile(jarFile).withCloseable { zf ->
zf.entries().findAll { !it.directory && it.name.endsWith(".class") }.collect {
it.name.substring(0, it.name.lastIndexOf("/")).replace("/", ".")
}.unique()
}
}
// Used by shadowJar and shadowJarAll to specify which packages should be shaded. We should
// *not* shade any of the dependencies that are specified in the "shadow" configuration,
// nor any of the classes from the SDK itself.
//
// This depends on our build products, so it can't be executed during Gradle's configuration
// phase; instead we have to run it after configuration, with the "afterEvaluate" block below.
def shadeDependencies(jarTask) {
def excludePackages = getAllSdkPackages() +
configurations.shadow.collectMany { getPackagesInDependencyJar(it)}
def topLevelPackages =
configurations.runtime.collectMany {
getPackagesInDependencyJar(it).collect { it.contains(".") ? it.substring(0, it.indexOf(".")) : it }
}.
unique()
topLevelPackages.forEach { top ->
jarTask.relocate(top, "com.launchdarkly.shaded." + top) {
excludePackages.forEach { exclude(it + ".*") }
}
}
}
def addOsgiManifest(jarTask, List<Configuration> importConfigs, List<Configuration> exportConfigs) {
jarTask.manifest {
attributes(
"Implementation-Version": version,
"Bundle-SymbolicName": "com.launchdarkly.client",
"Bundle-Version": version,
"Bundle-Name": "LaunchDarkly SDK",
"Bundle-ManifestVersion": "2",
"Bundle-Vendor": "LaunchDarkly"
)
// Since we're not currently able to use bnd or the Gradle OSGi plugin, we're not discovering
// imports by looking at the actual code; instead, we're just importing whatever packages each
// dependency is exporting (if it has an OSGi manifest) or every package in the dependency (if
// it doesn't).
def imports = forEachArtifactAndVisiblePackage(importConfigs, { a, p ->
bundleImport(p, a.moduleVersion.id.version, nextMajorVersion(a.moduleVersion.id.version))
}) + systemPackageImports
attributes("Import-Package": imports.join(","))
// Similarly, we're adding package exports for every package in whatever libraries we're
// making publicly available.
def sdkExports = getAllSdkPackages().collect { bundleExport(it, version) }
def exportedDependencies = forEachArtifactAndVisiblePackage(exportConfigs, { a, p ->
bundleExport(p, a.moduleVersion.id.version)
})
attributes("Export-Package": (sdkExports + exportedDependencies).join(","))
}
}
def bundleImport(packageName, importVersion, versionLimit) {
packageName + ";version=\"[" + importVersion + "," + versionLimit + ")\""
}
def bundleExport(packageName, exportVersion) {
packageName + ";version=\"" + exportVersion + "\""
}
def nextMajorVersion(v) {
def majorComponent = v.contains('.') ? v.substring(0, v.indexOf('.')) : v;
String.valueOf(Integer.parseInt(majorComponent) + 1)
}
def forEachArtifactAndVisiblePackage(configs, closure) {
configs.collectMany { it.resolvedConfiguration.resolvedArtifacts }
.collectMany { a ->
def exportedPackages = getOsgiPackageExportsFromJar(a.file)
if (exportedPackages == null) {
// This dependency didn't specify OSGi exports, so we'll just have to assume that
// we might need to use any package that's in this jar (with a little special-casing
// to exclude things we probably should not be importing).
exportedPackages = getPackagesInDependencyJar(a.file)
.findAll { !it.contains(".internal") }
}
exportedPackages.collect { p -> closure(a, p) }
}
}
def getOsgiPackageExportsFromJar(file) {
return new java.util.jar.JarFile(file).withCloseable { jar ->
def manifest = jar.manifest
if (manifest == null) {
return null
}
def dict = new java.util.Hashtable() // sadly, the manifest parser requires a Dictionary
manifest.mainAttributes.each { k, v -> dict.put(k.toString(), v.toString()) }
return org.eclipse.virgo.util.osgi.manifest.BundleManifestFactory.createBundleManifest(dict)
.exportPackage.exportedPackages.collect { it.packageName }
}
}
artifacts {
archives jar, sourcesJar, javadocJar, shadowJar, shadowJarAll
}
test {
testLogging {
events "passed", "skipped", "failed", "standardOut", "standardError"
showStandardStreams = true
exceptionFormat = 'full'
}
}
idea {
module {
downloadJavadoc = true
downloadSources = true
}
}
nexusStaging {
packageGroup = "com.launchdarkly"
}
def pomConfig = {
name 'LaunchDarkly SDK for Java'
packaging 'jar'
url 'https://github.com/launchdarkly/java-server-sdk'
licenses {
license {
name 'The Apache License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {
id 'jkodumal'
name 'John Kodumal'
email 'john@launchdarkly.com'
}
}
scm {
connection 'scm:git:git://github.com/launchdarkly/java-server-sdk.git'
developerConnection 'scm:git:ssh:git@github.com:launchdarkly/java-server-sdk.git'
url 'https://github.com/launchdarkly/java-server-sdk'
}
}
publishing {
publications {
shadow(MavenPublication) { publication ->
project.shadow.component(publication)
artifactId = sdkBaseName
artifact jar
artifact sourcesJar
artifact javadocJar
artifact shadowJarAll
artifact testJar
pom.withXml {
def root = asNode()
root.appendNode('description', 'Official LaunchDarkly SDK for Java')
asNode().children().last() + pomConfig
}
}
}
repositories {
mavenLocal()
maven {
def releasesRepoUrl = "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
def snapshotsRepoUrl = "https://oss.sonatype.org/content/repositories/snapshots/"
url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
credentials {
username ossrhUsername
password ossrhPassword
}
}
}
}
signing {
sign publishing.publications.shadow
}
tasks.withType(Sign) {
onlyIf { !"1".equals(project.findProperty("LD_SKIP_SIGNING")) } // so we can build jars for testing in CI
}
// This task is used by the logic in ./packaging-test to get copies of all the direct and transitive
// dependencies of the SDK, so they can be put on the classpath as needed during tests.
task exportDependencies(type: Copy, dependsOn: compileJava) {
into "packaging-test/temp/dependencies-all"
from configurations.runtime.resolvedConfiguration.resolvedArtifacts.collect { it.file }
}