forked from launchdarkly/java-server-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.gradle
More file actions
445 lines (388 loc) · 16.4 KB
/
build.gradle
File metadata and controls
445 lines (388 loc) · 16.4 KB
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
buildscript {
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
classpath "org.eclipse.virgo.util:org.eclipse.virgo.util.osgi.manifest:3.5.0.RELEASE"
classpath "org.osgi:osgi_R4_core:1.0"
}
}
plugins {
id "java"
id "java-library"
id "checkstyle"
id "signing"
id "com.github.johnrengelman.shadow" version "5.2.0"
id "maven-publish"
id "de.marcphilipp.nexus-publish" version "0.3.0"
id "io.codearte.nexus-staging" version "0.21.2"
id "org.ajoberstar.git-publish" version "2.1.3"
id "idea"
}
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()
}
configurations.all {
// check for updates every build for dependencies with: 'changing: true'
resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}
allprojects {
group = 'com.launchdarkly'
version = "${version}"
archivesBaseName = 'launchdarkly-java-server-sdk'
sourceCompatibility = 1.7
targetCompatibility = 1.7
}
ext {
sdkBasePackage = "com.launchdarkly.client"
// 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 = [:]
ext.versions = [
"commonsCodec": "1.10",
"gson": "2.7",
"guava": "19.0",
"jodaTime": "2.9.3",
"okhttpEventsource": "1.11.0",
"slf4j": "1.7.21",
"snakeyaml": "1.19",
"jedis": "2.9.0"
]
// 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:${versions.commonsCodec}",
"com.google.guava:guava:${versions.guava}",
"joda-time:joda-time:${versions.jodaTime}",
"com.launchdarkly:okhttp-eventsource:${versions.okhttpEventsource}",
"org.yaml:snakeyaml:${versions.snakeyaml}",
"redis.clients:jedis:${versions.jedis}"
]
// 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:${versions.gson}",
"org.slf4j:slf4j-api:${versions.slf4j}"
]
// Add dependencies to "libraries.test" that are used only in unit tests.
libraries.test = [
"com.squareup.okhttp3:mockwebserver:3.12.10",
"com.squareup.okhttp3:okhttp-tls:3.12.10",
"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
api 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
}
configurations {
// We need to define "internal" as a custom configuration that contains the same things as
// "implementation", because "implementation" has special behavior in Gradle that prevents us
// from referencing it the way we do in shadeDependencies().
internal.extendsFrom implementation
}
checkstyle {
configFile file("${project.rootDir}/checkstyle.xml")
}
jar {
// 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 {
// 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) {
classifier = 'all'
group = "shadow"
description = "Builds a Shaded fat jar including SLF4J"
from(project.convention.getPlugin(JavaPluginConvention).sourceSets.main.output)
configurations = [project.configurations.runtimeClasspath]
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
}
// Force the Javadoc build to fail if there are any Javadoc warnings. See: https://discuss.gradle.org/t/javadoc-fail-on-warning/18141/3
if (JavaVersion.current().isJava8Compatible()) {
tasks.withType(Javadoc) {
// The '-quiet' as second argument is actually a hack,
// since the one paramater addStringOption doesn't seem to
// work, we extra add '-quiet', which is added anyway by
// gradle. See https://github.com/gradle/gradle/issues/2354
// See JDK-8200363 (https://bugs.openjdk.java.net/browse/JDK-8200363)
// for information about the -Xwerror option.
options.addStringOption('Xwerror', '-quiet')
}
}
// 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.internal.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) {
// For a prerelease build with "-beta", "-rc", etc., the prerelease qualifier has to be
// removed from the bundle version because OSGi doesn't understand it.
def implementationVersion = version.replaceFirst('-.*$', '')
jarTask.manifest {
attributes(
"Implementation-Version": implementationVersion,
"Bundle-SymbolicName": "com.launchdarkly.client",
"Bundle-Version": implementationVersion,
"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, implementationVersion) }
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"
numberOfRetries = 40 // we've seen extremely long delays in closing repositories
}
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)
artifact jar
artifact sourcesJar
artifact javadocJar
artifact shadowJarAll
artifact testJar
pom.withXml {
def root = asNode()
root.appendNode('description', 'Official LaunchDarkly SDK for Java')
// The following workaround is for a known issue where the Shadow plugin assumes that all
// non-shaded dependencies should have "runtime" scope rather than "compile".
// https://github.com/johnrengelman/shadow/issues/321
// https://github.com/launchdarkly/java-server-sdk/issues/151
// Currently there doesn't seem to be any way way around this other than simply hacking the
// pom at this point after it has been generated by Shadow. All of the dependencies that
// are in the pom at this point should have compile scope.
def dependenciesNode = root.getAt('dependencies').get(0)
dependenciesNode.each { dependencyNode ->
def scopeNode = dependencyNode.getAt('scope').get(0)
scopeNode.setValue('compile')
}
root.children().last() + pomConfig
}
}
}
repositories {
mavenLocal()
}
}
nexusPublishing {
clientTimeout = java.time.Duration.ofMinutes(2) // we've seen extremely long delays in creating repositories
repositories {
sonatype {
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.runtimeClasspath.resolvedConfiguration.resolvedArtifacts.collect { it.file }
}
gitPublish {
repoUri = 'git@github.com:launchdarkly/java-server-sdk.git'
branch = 'gh-pages'
contents {
from javadoc
}
commitMessage = 'publishing javadocs'
}