Skip to content

Commit bc4a3d3

Browse files
committed
Use git information to set snapshot versions
Derives the artifact version from the name of the current git branch. Such builds are always snapshot versions. Release versions are only built from annotated tags as introduced in commit 04276e4.
1 parent e262122 commit bc4a3d3

File tree

2 files changed

+84
-23
lines changed

2 files changed

+84
-23
lines changed

build.gradle

Lines changed: 83 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
import java.util.regex.Matcher
1818

1919
plugins {
20-
id 'org.ajoberstar.grgit' version '2.0.0-rc.1'
20+
id 'org.ajoberstar.grgit' version '2.0.1'
2121
}
2222

23-
ext.gitTag = getGitTag()
23+
ext.scmInfo = getScmInfo()
2424

2525
allprojects {
2626
group = 'org.culturegraph'
27-
version = getVersionFrom(rootProject.ext.gitTag) ?: '5.0.0-SNAPSHOT'
27+
version = scmInfo.version
2828
ext.mavenName = null
2929
}
3030

@@ -82,14 +82,13 @@ subprojects {
8282

8383
signing {
8484
required {
85-
rootProject.ext.gitTag != null &&
86-
gradle.taskGraph.hasTask(tasks.uploadArchives)
85+
scmInfo.isRelease() && gradle.taskGraph.hasTask(tasks.uploadArchives)
8786
}
8887
sign configurations.archives
8988
}
9089

9190
def mavenProjectDescription = {
92-
name project.ext.mavenName ?: project.name
91+
name project.mavenName ?: project.name
9392
if (project.description) {
9493
description project.description
9594
}
@@ -118,7 +117,7 @@ subprojects {
118117
connection 'scm:git:https://github.com/culturegraph/metafacture-core.git'
119118
developerConnection 'scm:git:https://github.com/culturegraph/metafacture-core.git'
120119
url 'https://github.com/culturegraph/metafacture-core'
121-
tag rootProject.ext.gitTag ?: 'HEAD'
120+
tag project.scmInfo.tag ?: 'HEAD'
122121
}
123122
issueManagement {
124123
system 'Github'
@@ -175,36 +174,91 @@ subprojects {
175174
}
176175
}
177176

177+
class ScmInfo {
178+
def version
179+
def tag
180+
181+
ScmInfo(version, tag) {
182+
this.version = version
183+
this.tag = tag
184+
}
185+
186+
def isRelease() {
187+
return tag != null
188+
}
189+
}
190+
191+
def getScmInfo() {
192+
def version = null
193+
def tag = getGitTag()
194+
if (tag != null) {
195+
logger.lifecycle('SCM tag found. Making a release build')
196+
version = extractVersionFromTag(tag)
197+
} else {
198+
logger.lifecycle('No SCM tag found. Making a snapshot build')
199+
version = getSnapshotVersion()
200+
}
201+
logger.lifecycle("Version is $version")
202+
return new ScmInfo(version, tag)
203+
}
204+
205+
def getSnapshotVersion() {
206+
if (grgit == null) {
207+
logger.warn('No Git repository found')
208+
return 'non-scm-build-SNAPSHOT'
209+
}
210+
if (grgit.branch.current().fullName.equals('HEAD')) {
211+
logger.lifecycle('Detached HEAD found')
212+
return "commit-${grgit.head().id}-SNAPSHOT"
213+
}
214+
if (grgit.branch.current().name.equals('master')) {
215+
logger.lifecycle('On master branch')
216+
return 'master-SNAPSHOT'
217+
}
218+
if (grgit.branch.current().name.startsWith('releases/')) {
219+
logger.lifecycle('Release branch found')
220+
return "${extractVersionFromBranch(grgit.branch.current().name)}-SNAPSHOT"
221+
}
222+
logger.lifecycle('Feature branch found')
223+
return "feature-${grgit.branch.current().name}-SNAPSHOT"
224+
}
225+
178226
def getGitTag() {
179-
def tags = getGitTags(grgit)
227+
if (grgit == null) {
228+
logger.warn('No Git repository found')
229+
return null
230+
}
231+
if (!grgit.status().isClean()) {
232+
logger.warn('Working copy has modifications. Will not look for tags')
233+
return null
234+
}
235+
def tags = getAnnotatedTags()
180236
if (tags.isEmpty()) {
181-
logger.lifecycle("HEAD is not tagged. Making a snapshot build")
237+
logger.lifecycle('HEAD has no annotated tags')
182238
return null
183239
}
184240
if (tags.size() > 1) {
185-
logger.warn("HEAD has ${tags.size()} tags. Making a snapshot build")
241+
logger.warn("HEAD has ${tags.size()} annotated tags")
186242
return null
187243
}
188244
def tag = tags[0]
189-
if (tag.tagger == null || tag.dateTime == null) {
190-
logger.warn("Found lightweight tag ${tag.name}. Making a snapshot build")
191-
return null
192-
}
193-
logger.lifecycle("Found annotated tag ${tag.name}. Making a release build")
245+
logger.lifecycle("Found annotated tag $tag.name")
194246
return tag.name
195247
}
196248

197-
def static getGitTags(grgit) {
249+
def getAnnotatedTags() {
198250
def tags = []
199251
for (tag in grgit.tag.list()) {
200-
if (tag.commit == grgit.head()) {
252+
if (tag.commit == grgit.head()
253+
&& tag.tagger != null
254+
&& tag.dateTime != null) {
201255
tags.add tag
202256
}
203257
}
204258
return tags
205259
}
206260

207-
def static extractVersion(tag) {
261+
def static extractVersionFromTag(tag) {
208262
Matcher matcher =
209263
tag =~ /metafacture-core-(\d+\.\d+\.\d+(-[-A-Za-z0-9]+)?)/
210264
if (!matcher.matches()) {
@@ -218,9 +272,16 @@ def static extractVersion(tag) {
218272
return matcher.group(1)
219273
}
220274

221-
def static getVersionFrom(tag) {
222-
if (tag != null) {
223-
return extractVersion(tag)
275+
def static extractVersionFromBranch(branch) {
276+
Matcher matcher =
277+
branch =~ /releases\/metafacture-core-(\d+\.\d+\.\d+(-[-A-Za-z0-9]+)?)/
278+
if (!matcher.matches()) {
279+
throw new GradleException("""\
280+
Unsupported branch format: $branch
281+
Could not extract version from branch. Supported branch formats are
282+
releases/metafacture-core-X.Y.Z and
283+
releases/metafacture-core-X.Y.Z-QUALIFIER
284+
""".stripIndent())
224285
}
225-
return null
286+
return matcher.group(1)
226287
}

metafacture-runner/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ jar {
9292
}
9393
filesMatching('build.properties', {
9494
filter(ReplaceTokens, tokens: [
95-
version: project.version,
95+
version: project.version.toString(),
9696
timestamp: new Date().getTime().toString(),
9797
userName: System.properties['user.name']
9898
])

0 commit comments

Comments
 (0)