com.github.moleksyuk:vcs-semantic-version

Gradle plugin for populating project.version property based on semantic version specification (http://semver.org/).


License
MIT

Documentation

vcs-semantic-version

GitHub version License Build Status Coverage Status Dependency Status

Gradle plugin for populating project.version property based on semantic version specification.

The vcs-semantic-version plugin is hosted at Gradle Plugins Portal, Bintray's JCenter and Maven Central.

Functionality

The following functionality is provided by the vcs-semantic-version plugin:

  • Auto detects version control system of project
  • Populates project.version property with such pattern MAJOR.MINOR.PATCH-PRE_RELEASE

How to use

CASE#1: Set project.version using buildSemanticVersion task

1.1. Apply the com.github.moleksyuk.vcs-semantic-version plugin to your gradle project.

Gradle >= 2.1

plugins {
  id 'com.github.moleksyuk.vcs-semantic-version' version '1.1.3'
}

Gradle < 2.1

apply plugin: 'com.github.moleksyuk.vcs-semantic-version'

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.github.moleksyuk:vcs-semantic-version:1.1.3'
    }
}

1.2. Configure vcs-semantic-version plugin

Configure the plugin through the semanticVersion extension.

semanticVersion {
    major = 1 // required
    minor = 0 // required
    preRelease = 'beta' // optional
    accurev.stream = 'MyStream' // required only for ACCUREV VCS
}

1.3. Setup dependency

Specify task that should depends on buildSemanticVersion task.

For example:

jar.dependsOn buildSemanticVersion

CASE#2: Set project.version before all possible tasks

There are some tasks in plugins that use project.version.

For example:

  • plugin: java task: jar - builds artifact with specified version
  • plugin: sonar-runner task: sonarRunner - publishes code quality results into SonarQube
  • plugin: maven task: uploadArchives - publishes artifact to Maven repository
  • plugin: maven-publish task: publish - publishes artifact to Maven repository

Having multiple plugins that depend on project.version property requires to define dependency for each task:

jar.dependsOn buildSemanticVersion
tasks.sonarRunner.dependsOn buildSemanticVersion
....

More over maven-publish plugin has issue with dynamically populated project.version property.

To fix all these troubles use such configuration.

2.1. Define dependency for com.github.moleksyuk.vcs-semantic-version plugin to your gradle project.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.github.moleksyuk:vcs-semantic-version:1.1.3'
    }
}

2.2. Create semanticVersion extension manually

import com.github.moleksyuk.plugin.SemanticVersionPluginExtension
project.extensions.create(SemanticVersionPluginExtension.NAME, SemanticVersionPluginExtension)

2.3. Init semanticVersion extension

semanticVersion {
    major = 1
    minor = 2
}

2.4. Apply vcs-semantic-version plugin

apply plugin: 'com.github.moleksyuk.vcs-semantic-version'

The complete example:

import com.github.moleksyuk.plugin.SemanticVersionPluginExtension
project.extensions.create(SemanticVersionPluginExtension.NAME, SemanticVersionPluginExtension)
semanticVersion {
    major = 1
    minor = 2
}
apply plugin: 'com.github.moleksyuk.vcs-semantic-version'
apply plugin: 'maven-publish' // must be after vcs-semantic-version

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.github.moleksyuk:vcs-semantic-version:1.1.3'
    }
}

NOTE: Keep in mind that all dependent plugins on project.version should be applied after row:

apply plugin: 'com.github.moleksyuk.vcs-semantic-version'

How it works

1. Detects VCS for project with such rules:

  • If root project folder contains .git folder then GIT version control system is used
  • If root project folder contains .hg folder then MERCURIAL version control system is used
  • If root project folder contains .svn folder then SUBVERSION version control system is used
  • If non of above but contains .acignore file then ACCUREV version control system is used

2. Gets the latest revision number from VCS

For GIT

git rev-list HEAD --count

For MERCURIAL

hg id --num --rev tip

For SUBVERSION

svnversion .

For ACCUREV

accurev hist -ft -t highest -s [accurev.stream]

3. Populates project version property with such pattern:

MAJOR.MINOR.PATCH-PRE_RELEASE

where:

  • MAJOR - specified in semanticVersion extension
  • MINOR - specified in semanticVersion extension
  • PATCH - calculated in step#2
  • PRE_RELEASE - specified in semanticVersion extension