forked from linkedin/avro-util
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.gradle
297 lines (259 loc) · 9.62 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
/*
* Copyright 2018 LinkedIn Corp.
* Licensed under the BSD 2-Clause License (the "License").
* See License in the project root for license information.
*/
import javax.xml.bind.DatatypeConverter
import java.security.MessageDigest
import java.util.regex.Pattern
buildscript {
repositories {
mavenLocal()
jcenter()
}
dependencies {
classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4"
}
}
plugins {
id "maven-publish"
id "com.jfrog.bintray" version "1.8.4"
id 'com.github.johnrengelman.shadow' version '5.2.0' apply false
}
group = 'com.linkedin.avroutil1'
//does not include "helper" as that module has its own publishing section (because fat jar)
Set<String> projectsToPublish = new HashSet<>(Arrays.asList("avro-migration-helper", "avro-codegen", "avro-fastserde"))
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()
jcenter()
}
dependencies {
testImplementation "org.testng:testng:6.14.3"
}
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 = DatatypeConverter.printHexBinary(digest).toUpperCase()
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",
)
}
}
}
}
if (projectsToPublish.contains(project.name)) {
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'
}
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'
}
}
}
//record all of this module's publications in the bintray publications list
//for the root project
List<String> allPublicationNames = new ArrayList<>(Arrays.asList(rootProject.bintray.publications))
List<String> projectAndDepPublicationNames = new ArrayList<>(project.publishing.publications.names)
for (String projectPubName : projectAndDepPublicationNames) {
if (!allPublicationNames.contains(projectPubName)) {
allPublicationNames.add(projectPubName)
}
}
rootProject.bintray.publications = allPublicationNames.toArray()
}
}
}
}
plugins.withType(CheckstylePlugin) {
checkstyle {
toolVersion '8.25'
}
}
}
bintray {
user = System.getenv('BINTRAY_USER')
key = System.getenv('BINTRAY_KEY')
publications = [] //filled by child closure above
pkg {
repo = 'maven'
name = 'avro-util'
userOrg = 'linkedin'
licenses = ['BSD 2-Clause']
vcsUrl = 'https://github.com/linkedin/avro-util.git'
version {
name = project.version
}
publish = true
}
}
wrapper {
gradleVersion = '6.2'
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}")
}
}
}