forked from linkedin/avro-util
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle
361 lines (315 loc) · 11.4 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
/*
* Copyright 2018 LinkedIn Corp.
* Licensed under the BSD 2-Clause License (the "License").
* See License in the project root for license information.
*/
import java.security.MessageDigest
import java.util.regex.Pattern
buildscript {
repositories {
mavenLocal()
mavenCentral()
}
}
plugins {
id "maven-publish"
id "jacoco"
id 'com.github.johnrengelman.shadow' version '5.2.0' apply false
id 'com.github.kt3k.coveralls' version '2.10.1'
id "org.sonarqube" version "3.0"
}
group = 'com.linkedin.avroutil1'
repositories {
mavenLocal()
mavenCentral()
}
//does not include "helper" as that module has its own publishing section (because fat jar)
//does not include "avro-fastserde" as that module has its own publishing section (because we want it to depend on the fat jar)
//does not include "avro-codegen" as that module has its own publishing section (because we want it to depend on the fat jar)
Set<String> projectsToPublish = new HashSet<>(Arrays.asList("spotbugs-plugin", "parser"))
Set<String> projectsToRecordGitInfoFor = new HashSet<>(projectsToPublish)
projectsToRecordGitInfoFor.add("helper")
ext {
gitHash = "unknown"
gitName = "unknown"
}
subprojects {
//apply group and version to all submodules
group = rootProject.group
version = rootProject.version
plugins.withType(JavaPlugin) {
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
testImplementation "org.testng:testng:6.14.3"
//redirect logging to log4j2 for tests
testRuntimeOnly "org.apache.logging.log4j:log4j-core:2.17.1"
testRuntimeOnly "org.apache.logging.log4j:log4j-slf4j-impl:2.17.1"
}
//maintain compatibility with java8 runtime
compileJava {
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
}
test {
useTestNG()
testLogging {
showStandardStreams = true
showExceptions = true
showStackTraces = true
exceptionFormat = org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL
events "started", "passed", "skipped", "failed"
}
}
assemble.dependsOn ':parseGitInfo' //make sure we have git info before packaging
if (projectsToRecordGitInfoFor.contains(project.name)) {
jar {
doFirst { //for some odd reason this is needed to pick up gitHash
//version embedding at build time
//we create a file called __Versioning__[md5] and set its payload to contain versioning info
//
//the file name is not fixed, but contains an md5 hash of the project group name and version.
//its not fixed so that if multiple libraries are repackaged (think fat/shaded jar) the versioning files
//are unlikely to conflict or get overwritten. the md5 hash part is chosen such that builds are
//"repeatable" - re-running the same build results in the same output. this is nice for incremental builds.
//
//the value contains versioning information for both this module and the entire project it was built as part of.
//
//this information allows us, at runtime, to determine what libraries/versions exist on a given classpath
MessageDigest md = MessageDigest.getInstance("MD5")
md.update(String.valueOf(project.group).getBytes("UTF-8"))
md.update(String.valueOf(project.name).getBytes("UTF-8"))
md.update(String.valueOf(project.version).getBytes("UTF-8"))
byte[] digest = md.digest()
String hash = hex(digest)
String versioningFileName = "__Versioning__" + hash
String versioningPayload = "{" +
"\"format\":\"v1\"," +
"\"project\":\"" + rootProject.group + ":" + rootProject.name + ":" + rootProject.version + "\"," +
"\"module\":\"" + project.group + ":" + project.name + ":" + project.version + "\"," +
"\"branch\":\"" + rootProject.gitName + "\"," +
"\"revision\":\"" + rootProject.gitHash + "\"" +
"}"
String outFolder = "${project.buildDir}/resources/main/META-INF/"
mkdir outFolder
file("${outFolder}/${versioningFileName}").text = versioningPayload
}
manifest {
//embed module names and version information
//into jar manifests
attributes(
'Name': "${project.name}",
'Specification-Title': "${project.name}",
'Specification-Version': "${project.version}",
'Specification-Vendor': "LinkedIn",
'Implementation-Title': "${project.name}",
'Implementation-Version': "${project.version}",
'Implementation-Vendor': "LinkedIn",
)
}
}
}
task sourceJar(type: Jar) {
from sourceSets.main.allJava
classifier "sources"
}
task javadocJar(type: Jar) {
from javadoc
classifier = 'javadoc'
}
task testJar(type: Jar) {
from sourceSets.test.allJava
classifier = 'tests'
}
if (projectsToPublish.contains(project.name)) {
publishing {
publications {
"$project.name"(MavenPublication) {
groupId project.group
artifactId project.name
version project.version
from components.java
artifact sourceJar
artifact javadocJar
artifact testJar
//we strive to meet https://central.sonatype.org/pages/requirements.html
pom {
name = 'Avro Util'
description = 'utilities for writing code that works across major avro versions'
url = 'https://github.com/linkedin/avro-util'
licenses {
license {
name = 'BSD 2-Clause'
url = 'https://raw.githubusercontent.com/linkedin/avro-util/master/LICENSE'
}
}
developers {
developer {
id = 'radai-rosenblatt'
name = 'Radai Rosenblatt'
email = '[email protected]'
organization = 'LinkedIn'
organizationUrl = 'linkedin.com'
}
developer {
id = 'abhishekmendhekar'
name = 'Abhishek Mendhekar'
organization = 'LinkedIn'
organizationUrl = 'linkedin.com'
}
developer {
id = 'jimhe'
name = 'Jim He'
email = '[email protected]'
organization = 'LinkedIn'
organizationUrl = 'linkedin.com'
}
developer {
id = 'ghosthack'
name = 'Adrian Fernandez'
email = '[email protected]'
organization = 'LinkedIn'
organizationUrl = 'linkedin.com'
}
}
scm {
connection = 'scm:git:git://github.com:linkedin/avro-util.git'
developerConnection = 'scm:git:ssh://github.com:linkedin/avro-util.git'
url = 'https://github.com/linkedin/avro-util'
}
}
}
}
repositories {
mavenLocal()
maven {
name "LinkedInJfrog"
url "https://linkedin.jfrog.io/artifactory/avro-util"
credentials {
if (System.getenv('JFROG_USER') != null && System.getenv('JFROG_KEY') != null) {
username System.getenv('JFROG_USER')
password System.getenv('JFROG_KEY')
}
}
}
}
}
}
}
plugins.withType(CheckstylePlugin) {
checkstyle {
toolVersion '8.25'
}
}
plugins.withType(JacocoPlugin) {
jacoco {
toolVersion = "0.8.5"
}
//we dont need reports at the individual module level as we generate an aggregate
jacocoTestReport {
reports {
xml.enabled false
html.enabled false
csv.enabled false
}
}
}
}
tasks.withType(GenerateModuleMetadata) {
//we ship helper as a fat helper-all jar, which means we modify the
//pom.xml for most of our artifacts. gradle's now module metadata publication
//leaks "internal" dependencies on the regular helper module (which we dont
//publish) and causes issues. so we disable it
enabled = false
}
//generates an aggregate coverage report for the entire project
task codeCoverageReport(type: JacocoReport) {
executionData files()
project.afterEvaluate { //delay until this (parent) project is fully configured
subprojects.each { Project subproject ->
subproject.afterEvaluate { //delay until sub-module is fully configured (or there wont be any plugins applied)
if (subproject.pluginManager.hasPlugin('jacoco')) {
//add source sets from all relevant submodules
sourceSets subproject.sourceSets.main
}
}
}
executionData.setFrom(fileTree(project.rootDir.absolutePath).include("**/build/jacoco/*.exec"))
}
reports {
xml.enabled true
xml.destination = new File("${buildDir}/reports/jacoco/report.xml")
html.enabled false //useful as a debug aid and for local dev
csv.enabled false
}
}
coveralls {
jacocoReportPath "${buildDir}/reports/jacoco/report.xml"
}
//the root project doesnt have a clean task applied (?!), so we add one ourselves.
task clean {
doLast {
delete "${buildDir}"
}
}
wrapper {
gradleVersion = '6.5.1'
distributionType = Wrapper.DistributionType.ALL
}
tasks.register("parseGitInfo") {
group = 'Versioning'
description = 'retrieves the current branch/tag name and git hash'
doLast {
def headFile = file("${rootProject.rootDir}/.git/HEAD")
if (!headFile.exists()) {
//we're not a git working copy
logger.warn("${headFile} not found - this doesnt appear to be a git checkout")
rootProject.gitHash = "unknown"
rootProject.gitName = "unknown"
return
}
def contents = headFile.text
//.git/HEAD is either a hash (if detached head) or a ref
def refPattern = Pattern.compile("ref:\\s+(refs/(\\w+)/(.*))\\s+\$")
def matcher = refPattern.matcher(contents)
if (!matcher.matches()) {
rootProject.gitHash = contents.trim()
rootProject.gitName = contents.trim() //use the hash as the branch name
logger.info("git working copy is in detached head mode at revision ${gitHash}")
} else {
def path = matcher.group(1)
def category = matcher.group(2)
def name = matcher.group(3)
def refFile = file("${rootProject.rootDir}/.git/${path}")
if (!refFile.exists()) {
//TODO - add support for packed-refs
throw new IllegalStateException("unable to find ${refFile}")
}
rootProject.gitHash = refFile.text.trim()
rootProject.gitName = name
def type
switch (category) {
case "heads":
type = "branch"
break
case "tags":
type = "tag"
break
default:
logger.warn("unknown ref folder ${category}")
type = "unknown"
break
}
logger.info("git working copy is at ${type} ${rootProject.gitName} at revision ${rootProject.gitHash}")
}
}
}
static String hex(byte[] bytes) {
StringBuilder result = new StringBuilder(bytes.length * 2);
for (byte b : bytes) {
result.append(String.format("%02X", b));
}
return result.toString();
}