From 86cfc66a40083091ebbd0b3a721bc8ab92db1acd Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Fri, 12 Aug 2016 16:27:57 +0200 Subject: [PATCH] Automatically set version from git - Uses last tag on master branch (this is set on 'git flow release finish') - branch name snapshot otherwise, e.g. 'feature-nio-SNAPSHOT', 'develop-SNAPSHOT' --- build.gradle | 6 +-- .../ch/dissem/gradle/GitFlowVersion.groovy | 53 +++++++++++++++++++ .../gradle-plugins/gitflow-version.properties | 1 + gradle/wrapper/gradle-wrapper.properties | 4 +- 4 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 buildSrc/src/main/groovy/ch/dissem/gradle/GitFlowVersion.groovy create mode 100644 buildSrc/src/main/resources/META-INF/gradle-plugins/gitflow-version.properties diff --git a/build.gradle b/build.gradle index 0e96284..311c3f9 100644 --- a/build.gradle +++ b/build.gradle @@ -3,12 +3,10 @@ subprojects { apply plugin: 'maven' apply plugin: 'signing' apply plugin: 'jacoco' + apply plugin: 'gitflow-version' sourceCompatibility = 1.7 group = 'ch.dissem.jabit' - version = '1.1.0-SNAPSHOT' - - ext.isReleaseVersion = !version.endsWith("SNAPSHOT") repositories { mavenCentral() @@ -35,7 +33,7 @@ subprojects { } signing { - required { isReleaseVersion && project.getProperties().get("signing.keyId")?.length() > 0 } + required { isRelease && project.getProperties().get("signing.keyId")?.length() > 0 } sign configurations.archives } diff --git a/buildSrc/src/main/groovy/ch/dissem/gradle/GitFlowVersion.groovy b/buildSrc/src/main/groovy/ch/dissem/gradle/GitFlowVersion.groovy new file mode 100644 index 0000000..81af76b --- /dev/null +++ b/buildSrc/src/main/groovy/ch/dissem/gradle/GitFlowVersion.groovy @@ -0,0 +1,53 @@ +package ch.dissem.gradle + +import org.gradle.api.Plugin +import org.gradle.api.Project + +/** + * Sets the version as follows: + * + */ +class GitFlowVersion implements Plugin { + def getBranch(Project project) { + def stdout = new ByteArrayOutputStream() + project.exec { + commandLine 'git', 'rev-parse', '--abbrev-ref', 'HEAD' + standardOutput = stdout + } + return stdout.toString().trim() + } + + def getTag(Project project) { + def stdout = new ByteArrayOutputStream() + project.exec { + commandLine 'git', 'describe', '--abbrev=0' + standardOutput = stdout + } + return stdout.toString().trim() + } + + def isRelease(Project project) { + return "master" == getBranch(project); + } + + def getVersion(Project project) { + if (project.ext.isRelease) { + return getTag(project) + } else { + return getBranch(project).replaceAll("/", "-") + "-SNAPSHOT" + } + } + + @Override + void apply(Project project) { + project.ext.isRelease = isRelease(project) + project.version = getVersion(project) + + project.task('version') << { + println "Version deduced from git: '${project.version}'" + } + } +} diff --git a/buildSrc/src/main/resources/META-INF/gradle-plugins/gitflow-version.properties b/buildSrc/src/main/resources/META-INF/gradle-plugins/gitflow-version.properties new file mode 100644 index 0000000..1fb4f78 --- /dev/null +++ b/buildSrc/src/main/resources/META-INF/gradle-plugins/gitflow-version.properties @@ -0,0 +1 @@ +implementation-class=ch.dissem.gradle.GitFlowVersion diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index a657ea5..ebcdff0 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Thu Aug 11 17:36:39 CEST 2016 +#Fri Aug 12 11:52:04 CEST 2016 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip