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'

(cherry picked from commit 86cfc66a40)
This commit is contained in:
Christian Basler 2016-08-12 22:02:06 +02:00
parent 631e71bc74
commit 41c4447514
4 changed files with 58 additions and 6 deletions

View File

@ -3,12 +3,10 @@ subprojects {
apply plugin: 'maven' apply plugin: 'maven'
apply plugin: 'signing' apply plugin: 'signing'
apply plugin: 'jacoco' apply plugin: 'jacoco'
apply plugin: 'gitflow-version'
sourceCompatibility = 1.7 sourceCompatibility = 1.7
group = 'ch.dissem.jabit' group = 'ch.dissem.jabit'
version = '1.1.0-SNAPSHOT'
ext.isReleaseVersion = !version.endsWith("SNAPSHOT")
repositories { repositories {
mavenCentral() mavenCentral()
@ -35,7 +33,7 @@ subprojects {
} }
signing { signing {
required { isReleaseVersion && project.getProperties().get("signing.keyId")?.length() > 0 } required { isRelease && project.getProperties().get("signing.keyId")?.length() > 0 }
sign configurations.archives sign configurations.archives
} }

View File

@ -0,0 +1,53 @@
package ch.dissem.gradle
import org.gradle.api.Plugin
import org.gradle.api.Project
/**
* Sets the version as follows:
* <ul>
* <li>If the branch is 'master', the version is set to the latest tag (which is expected to be set by Git flow)</li>
* <li>Otherwise, the version is set to the branch name, with '-SNAPSHOT' appended</li>
* </ul>
*/
class GitFlowVersion implements Plugin<Project> {
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}'"
}
}
}

View File

@ -0,0 +1 @@
implementation-class=ch.dissem.gradle.GitFlowVersion

View File

@ -1,6 +1,6 @@
#Thu Aug 11 17:36:39 CEST 2016 #Fri Aug 12 11:52:04 CEST 2016
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists 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