Skip to content

Commit 451ca4e

Browse files
committed
reuse versioning and git properties
1 parent a469351 commit 451ca4e

File tree

4 files changed

+187
-128
lines changed

4 files changed

+187
-128
lines changed

build.gradle

+46-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import org.apache.tools.ant.filters.ReplaceTokens
2+
3+
14
buildscript {
25
ext.jdkVersionDefault = 17
36
ext.javaClassVersionDefault = 11
@@ -396,25 +399,60 @@ configure(subprojects.findAll {! it.name.startsWith('spark-lineage')}) {
396399
}
397400
}
398401

402+
apply plugin: 'com.gorylenko.gradle-git-properties'
403+
gitProperties {
404+
keys = ['git.commit.id','git.commit.id.describe','git.commit.time']
405+
// using any tags (not limited to annotated tags) for "git.commit.id.describe" property
406+
// see http://ajoberstar.org/grgit/grgit-describe.html for more info about the describe method and available parameters
407+
// 'it' is an instance of org.ajoberstar.grgit.Grgit
408+
customProperty 'git.commit.id.describe', { it.describe(tags: true) }
409+
gitPropertiesResourceDir = rootProject.buildDir
410+
failOnNoGitDirectory = false
411+
}
412+
413+
def gitPropertiesGenerated = false
414+
415+
apply from: 'gradle/versioning/versioning-global.gradle'
416+
417+
tasks.register("generateGitPropertiesGlobal", com.gorylenko.GenerateGitPropertiesTask) {
418+
doFirst {
419+
if (!gitPropertiesGenerated) {
420+
println "Generating git.properties"
421+
gitPropertiesGenerated = true
422+
} else {
423+
// Skip actual execution if already run
424+
println "git.properties already generated, skipping..."
425+
onlyIf { false }
426+
}
427+
}
428+
}
429+
399430
subprojects {
400431

401432
apply plugin: 'maven-publish'
402-
apply plugin: 'com.gorylenko.gradle-git-properties'
403433
apply plugin: 'com.diffplug.spotless'
404434

405-
gitProperties {
406-
keys = ['git.commit.id','git.commit.id.describe','git.commit.time']
407-
// using any tags (not limited to annotated tags) for "git.commit.id.describe" property
408-
// see http://ajoberstar.org/grgit/grgit-describe.html for more info about the describe method and available parameters
409-
// 'it' is an instance of org.ajoberstar.grgit.Grgit
410-
customProperty 'git.commit.id.describe', { it.describe(tags: true) }
411-
failOnNoGitDirectory = false
435+
def gitPropertiesTask = tasks.register("copyGitProperties", Copy) {
436+
dependsOn rootProject.tasks.named("generateGitPropertiesGlobal")
437+
doLast{
438+
println "copying git.properties"
439+
}
440+
def sourceFile = file("${rootProject.buildDir}/git.properties")
441+
from sourceFile
442+
into "$project.buildDir/resources/main"
412443
}
413444

414445
plugins.withType(JavaPlugin).configureEach {
446+
project.tasks.named(JavaPlugin.CLASSES_TASK_NAME).configure{
447+
dependsOn gitPropertiesTask
448+
}
415449
if (project.name == 'datahub-web-react') {
416450
return
417451
}
452+
/* TODO: evaluate ignoring jar timestamps for increased caching (compares checksum instead)
453+
jar {
454+
preserveFileTimestamps = false
455+
}*/
418456

419457
dependencies {
420458
implementation externalDependency.annotationApi
+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/**
2+
Applies a consistent versioning scheme to all projects using this script
3+
4+
Uses git tags to mint versions by default.
5+
git tags can be of a few forms:
6+
- short sha (typical for a PR or a commit) (e.g. 38960ae)
7+
- versioned tags (typical for a release) (e.g. v0.8.45, v0.8.45.1, v0.8.45rc1, v0.8.45.1rc4)
8+
9+
Produces the following variables and supports token replacement
10+
- version: server version amenable for creating jars
11+
- fullVersion: full version string
12+
- cliMajorVersion: cli version amenable for binding to server as a default
13+
0.8.44 or 0.8.44-1 (for clean tags) or 0.8.45-SNAPSHOT (for unclean repositories)
14+
15+
All inference can be overridden by passing in the releaseVersion property
16+
e.g. -PreleaseVersion=0.2.3.4 will set the jar version to 0.2.3-4
17+
18+
**/
19+
20+
import groovy.json.JsonBuilder
21+
22+
def detailedVersionString = "0.0.0-unknown-SNAPSHOT"
23+
def cliMajorVersion = "0.15.0" // base default cli major version
24+
def snapshotVersion = false
25+
def javaVersion = ""
26+
27+
if (project.hasProperty("releaseVersion")) {
28+
version = releaseVersion
29+
detailedVersionString = releaseVersion
30+
} else {
31+
try {
32+
// apply this plugin in a try-catch block so that we can handle cases without .git directory
33+
apply plugin: "com.palantir.git-version"
34+
def details = versionDetails()
35+
detailedVersionString = gitVersion()
36+
version = details.lastTag
37+
version = version.startsWith("v")? version.substring(1): version
38+
def suffix = details.isCleanTag? "": "-SNAPSHOT"
39+
snapshotVersion = ! details.isCleanTag
40+
}
41+
catch (Exception e) {
42+
e.printStackTrace()
43+
// last fall back
44+
version = detailedVersionString
45+
}
46+
}
47+
48+
// trim version if it is of size 4 to size 3
49+
def versionParts = version.tokenize(".")
50+
cliMajorVersion = version
51+
if (versionParts.size() > 3) {
52+
// at-least 4 part version
53+
// we check if the 4th part is a .0 in which case we want to create a release
54+
if ((versionParts.size() == 4) && (versionParts[3] == '0')) {
55+
versionParts = versionParts[0..2]
56+
}
57+
version = versionParts[0..2].join('.')
58+
if (versionParts.size() > 3) {
59+
version = version + "-" + versionParts[3..versionParts.size()-1].join('-')
60+
}
61+
}
62+
63+
if (snapshotVersion) {
64+
if (versionParts[versionParts.size()-1].isInteger()) {
65+
def base_version = versionParts[0..versionParts.size()-2].join('.')
66+
version = base_version + '.' + (versionParts[versionParts.size()-1].toInteger()+1).toString() + "-SNAPSHOT"
67+
cliMajorVersion = base_version + "." + versionParts[versionParts.size()-1]
68+
} else {
69+
// we are unable to part the last token as an integer, so we just append SNAPSHOT to this version
70+
version = versionParts[0..versionParts.size()-1].join('.') + '-SNAPSHOT'
71+
cliMajorVersion = versionParts[0..versionParts.size()-1].join('.')
72+
}
73+
74+
// differences from metadata-integration/java/versioning.gradle
75+
if (versionParts[versionParts.size()-1].isInteger()) {
76+
javaVersion = versionParts[0..versionParts.size()-2].join('.') + '.' + (versionParts[versionParts.size()-1].toInteger()+1).toString() + "-SNAPSHOT"
77+
} else {
78+
// we are unable to part the last token as an integer, so we just append SNAPSHOT to this version
79+
javaVersion = versionParts[0..versionParts.size()-1].join('.') + '-SNAPSHOT'
80+
}
81+
}
82+
83+
// Note: No task, we want this executed during config phase, once for rootProject.
84+
def data = [
85+
fullVersion: detailedVersionString,
86+
cliMajorVersion: cliMajorVersion,
87+
version: version,
88+
javaVersion: javaVersion
89+
]
90+
91+
// Convert to JSON
92+
def jsonBuilder = new JsonBuilder(data)
93+
def outputFile = file("${rootProject.buildDir}/version.json")
94+
95+
// Ensure buildDir exists
96+
rootProject.buildDir.mkdirs()
97+
98+
// Write to file
99+
outputFile.text = jsonBuilder.toPrettyString()
100+
101+
println "JSON data written to ${outputFile}"

gradle/versioning/versioning.gradle

+18-68
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,33 @@
1-
/**
2-
Applies a consistent versioning scheme to all projects using this script
3-
4-
Uses git tags to mint versions by default.
5-
git tags can be of a few forms:
6-
- short sha (typical for a PR or a commit) (e.g. 38960ae)
7-
- versioned tags (typical for a release) (e.g. v0.8.45, v0.8.45.1, v0.8.45rc1, v0.8.45.1rc4)
8-
9-
Produces the following variables and supports token replacement
10-
- version: server version amenable for creating jars
11-
- fullVersion: full version string
12-
- cliMajorVersion: cli version amenable for binding to server as a default
13-
0.8.44 or 0.8.44-1 (for clean tags) or 0.8.45-SNAPSHOT (for unclean repositories)
14-
15-
All inference can be overridden by passing in the releaseVersion property
16-
e.g. -PreleaseVersion=0.2.3.4 will set the jar version to 0.2.3-4
17-
18-
**/
19-
20-
1+
import groovy.json.JsonSlurper
212
import org.apache.tools.ant.filters.ReplaceTokens
223

4+
235
def detailedVersionString = "0.0.0-unknown-SNAPSHOT"
246
def cliMajorVersion = "0.15.0" // base default cli major version
25-
def snapshotVersion = false
26-
if (project.hasProperty("releaseVersion")) {
27-
version = releaseVersion
28-
detailedVersionString = releaseVersion
29-
} else {
30-
try {
31-
// apply this plugin in a try-catch block so that we can handle cases without .git directory
32-
apply plugin: "com.palantir.git-version"
33-
def details = versionDetails()
34-
detailedVersionString = gitVersion()
35-
version = details.lastTag
36-
version = version.startsWith("v")? version.substring(1): version
37-
def suffix = details.isCleanTag? "": "-SNAPSHOT"
38-
snapshotVersion = ! details.isCleanTag
39-
}
40-
catch (Exception e) {
41-
e.printStackTrace()
42-
// last fall back
43-
version = detailedVersionString
44-
}
45-
}
467

47-
// trim version if it is of size 4 to size 3
48-
def versionParts = version.tokenize(".")
49-
cliMajorVersion = version
50-
if (versionParts.size() > 3) {
51-
// at-least 4 part version
52-
// we check if the 4th part is a .0 in which case we want to create a release
53-
if ((versionParts.size() == 4) && (versionParts[3] == '0')) {
54-
versionParts = versionParts[0..2]
55-
}
56-
version = versionParts[0..2].join('.')
57-
if (versionParts.size() > 3) {
58-
version = version + "-" + versionParts[3..versionParts.size()-1].join('-')
8+
def inputFile = file("${rootProject.buildDir}/version.json")
9+
10+
task readJsonData {
11+
if (inputFile.exists()) {
12+
def jsonSlurper = new JsonSlurper()
13+
def data = jsonSlurper.parse(inputFile)
14+
15+
detailedVersionString = data.fullVersion
16+
cliMajorVersion = data.cliMajorVersion
17+
version = data.version
18+
} else {
19+
println "JSON file not found: ${inputFile.path}"
5920
}
6021
}
6122

62-
if (snapshotVersion) {
63-
if (versionParts[versionParts.size()-1].isInteger()) {
64-
def base_version = versionParts[0..versionParts.size()-2].join('.')
65-
version = base_version + '.' + (versionParts[versionParts.size()-1].toInteger()+1).toString() + "-SNAPSHOT"
66-
cliMajorVersion = base_version + "." + versionParts[versionParts.size()-1]
67-
} else {
68-
// we are unable to part the last token as an integer, so we just append SNAPSHOT to this version
69-
version = versionParts[0..versionParts.size()-1].join('.') + '-SNAPSHOT'
70-
cliMajorVersion = versionParts[0..versionParts.size()-1].join('.')
71-
}
23+
task printVersionDetails() {
24+
println("fullVersion=" + detailedVersionString)
25+
println("cliMajorVersion=" + cliMajorVersion)
26+
println("version=" + version)
7227
}
7328

7429
processResources {
7530
filter(ReplaceTokens, tokens:[fullVersion: detailedVersionString])
7631
filter(ReplaceTokens, tokens:[cliMajorVersion: cliMajorVersion])
7732
}
7833

79-
task printVersionDetails() {
80-
println("fullVersion=" + detailedVersionString)
81-
println("cliMajorVersion=" + cliMajorVersion)
82-
println("version=" + version)
83-
}
+22-52
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,30 @@
1-
/**
2-
Applies a consistent versioning scheme to all projects using this script
3-
-PreleaseVersion=0.2.3.4 will set the jar version to 0.2.3-4
1+
import groovy.json.JsonSlurper
2+
import org.apache.tools.ant.filters.ReplaceTokens
43

5-
Not providing a property will make it use git tags to mint either a version like:
6-
0.8.44 or 0.8.44-1 (for clean tags) or 0.8.45-SNAPSHOT (for unclean repositories)
74

8-
**/
5+
def detailedVersionString = "0.0.0-unknown-SNAPSHOT"
6+
def cliMajorVersion = "0.15.0" // base default cli major version
97

8+
task readJsonData {
9+
def inputFile = file("${rootProject.buildDir}/version.json")
1010

11-
import org.apache.tools.ant.filters.ReplaceTokens
11+
if (inputFile.exists()) {
12+
def jsonSlurper = new JsonSlurper()
13+
def data = jsonSlurper.parse(inputFile)
1214

13-
def detailedVersionString = "0.0.0-unknown-SNAPSHOT"
14-
def snapshotVersion = false
15-
if (project.hasProperty("releaseVersion")) {
16-
version = releaseVersion
17-
detailedVersionString = releaseVersion
18-
} else {
19-
try {
20-
// apply this plugin in a try-catch block so that we can handle cases without .git directory
21-
apply plugin: "com.palantir.git-version"
22-
def details = versionDetails()
23-
detailedVersionString = gitVersion()
24-
version = details.lastTag
25-
version = version.startsWith("v")? version.substring(1): version
26-
def suffix = details.isCleanTag? "": "-SNAPSHOT"
27-
snapshotVersion = ! details.isCleanTag
28-
}
29-
catch (Exception e) {
30-
e.printStackTrace()
31-
// last fall back
32-
version = detailedVersionString
33-
}
15+
detailedVersionString = data.fullVersion
16+
cliMajorVersion = data.cliMajorVersion
17+
version = data.javaVersion
18+
} else {
19+
println "JSON file not found: ${inputFile.path}"
3420
}
35-
// trim version if it is of size 4 to size 3
36-
def versionParts = version.tokenize(".")
37-
if (versionParts.size() > 3) {
38-
// at-least 4 part version
39-
// we check if the 4th part is a .0 in which case we want to create a release
40-
if ((versionParts.size() == 4) && (versionParts[3] == '0')) {
41-
versionParts = versionParts[0..2]
42-
}
43-
version = versionParts[0..2].join('.')
44-
if (versionParts.size() > 3) {
45-
version = version + "-" + versionParts[3..versionParts.size()-1].join('-')
46-
}
47-
}
21+
}
4822

49-
if (snapshotVersion) {
50-
if (versionParts[versionParts.size()-1].isInteger()) {
51-
version = versionParts[0..versionParts.size()-2].join('.') + '.' + (versionParts[versionParts.size()-1].toInteger()+1).toString() + "-SNAPSHOT"
52-
} else {
53-
// we are unable to part the last token as an integer, so we just append SNAPSHOT to this version
54-
version = versionParts[0..versionParts.size()-1].join('.') + '-SNAPSHOT'
55-
}
56-
}
23+
processResources {
24+
filter(ReplaceTokens, tokens:[fullVersion: detailedVersionString])
25+
}
5726

58-
processResources {
59-
filter(ReplaceTokens, tokens:[fullVersion: detailedVersionString])
60-
}
27+
task printVersionDetails() {
28+
println("fullVersion=" + detailedVersionString)
29+
println("version=" + version)
30+
}

0 commit comments

Comments
 (0)