Skip to content
Gradle Automatic Build Versioning Plugin - An easy Gradle plugin that follows semver.org rules to automatically generate the Patch version, Build number and Code version, while Major, Minor and Pre-Release suffix remain under our control.
Branch: master
Clone or download
Latest commit fb1d2a3 Jun 17, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
art GrabVer logo Jul 26, 2017
module_a
module_b Version 2.0.0 Jun 17, 2019
src Version 2.0.0 Jun 17, 2019
.gitignore Updated .gitignore Aug 6, 2017
LICENSE Initial commit May 21, 2017
README.md Merge remote-tracking branch 'origin/master' Jun 17, 2019
build-test.gradle Version 2.0.0 Jun 17, 2019
build.gradle Version 2.0.0 Jun 17, 2019
gradlew Initial commit May 21, 2017
gradlew.bat Initial commit May 21, 2017
jfrog-bintray-publish.gradle Configuration for Bintray Jul 26, 2017
settings.gradle Configuration for Bintray Jul 26, 2017

README.md

Gradle Gradle Gradle Licence

Logo

An easy Gradle plugin that follows semver.org rules to automatically generate the Patch version, Build number and Code version, while Major, Minor and Pre-Release suffix remain under our control.

I saw plenty of plugins that require a long configuration and continuous adjustment just to update those numbers, if so, better without any plugin then! With this plugin we are required to manage only 2 variables.

Inspired from Android Studio Automatic Incremental Gradle Versioning. Customized into library with PreRelease, Auto-Reset and Sub-Modules features.

Easy to apply, it works with any project type with sub modules too.

Rules

User values

major: Required, user defined value for breaking changes.
minor: Required, user defined value for new features, but backwards compatible. If you increase Major version, this value must be coherent(=0).
patch: Optional, user defined value (or auto-generated value) for backwards compatible bug fixes only.
preRelease: Optional, user defined value for pre-releases suffix.
incrementOn: Optional, custom task name to trigger the increase of the version (default: assembleRelease, bundleRelease, grabverRelease).
saveOn: Optional, custom task name for which you want to save the versioning file (default: build, assembleDebug, assembleRelease, bundleDebug, bundleRelease, grabverRelease, jar, war, explodedWar).

Calculation

patch - If not specified by user, increases at each release, but it auto resets back to 0 when Minor or Major version changes or if preRelease is set.
build - Increases at each build.
code - Increases at each release.

Installation

Configure script dependencies in the project build.gradle file:

buildscript {
    repositories {
        // Using Bintray repository:
        maven { url "http://dl.bintray.com/davideas/maven" }
        // or Gradle plugins repository:
        maven { url "https://plugins.gradle.org/m2/" }
    }
    dependencies {
        // Using Bintray or Gradle Plugins repository
        classpath "eu.davidea:grabver:2.0.0"
    }
}

Usage

1. Version configuration

Apply the plugin in the module you desire, it will create a properties file under that module!

plugins {
    ...
    id: 'eu.davidea.grabver'
}

versioning {
    // Required (number)
    major 1
    minor 0
    // Optional, force custom patch (number)
    patch 7
    // Optional (any string)
    preRelease "RC1"
    // Optional, custom task name to trigger the increase of the version
    incrementOn "<task-name>"
    // Optional, custom task name for which you want to save the versioning file
    saveOn "<task-name>"
}

2. Grab your new version

versioning.major
versioning.minor
versioning.patch
versioning.build
versioning.preRelease
versioning.code        // needed for all Android projects
versioning.name        // output: "major.minor.patch[-preRelease]"
versioning.fullName    // output: "major.minor.patch[-preRelease] #build built on yyyy.mm.dd"
versioning.builtOn     // output: " built on yyyy.mm.dd"
versioning.date        // or versioning.getDate([format]) - default "yyyy.mm.dd"

Note: To trigger the evaluation, the user must grab one of the above attribute.

3. Run it

  • Via command line:
/**
 * To increment build number only:
 * - Code and Patch remain unchanged
 */
gradle [build | jar | war | explodedWar | assembleDebug | bundleDebug]

/**
 * To increment build, patch and code:
 * Code and Patch are incremented because of releases:
 * - Code is incremented if exists a release task.
 *   Example: "bundleRelease", "grabverRelease", ":module:assembleRelease"
 * - But Patch may be resetted if Major or Minor is changed or if preRelease is set
 */
gradle [assembleRelease | bundleRelease | grabverRelease]
  • In Android Studio:
    • via menu build > Build Bundle / APK (bundleDebug | assembleDebug)
    • via menu build > Generate Signed Bundle / APK (bundleRelease | assembleRelease).
    • by running the App (assembleDebug | assembleRelease, depending by the build variant).

From version 2, the plugin evaluates the run tasks in silent mode to automatically skip the new version evaluation and to not print unnecessary logs if no save task was detected. A single warning line is however produced.
Single tasks performed such as clean, test, flyway and all third parties invented plugin tasks out there will not trigger the versioning evaluation. Example:

Command Outcome
gradle clean test Evaluation skipped
gradle javadoc Evaluation skipped
gradle flywayClean Evaluation skipped
gradle clean build Evaluation triggered and new values saved
gradle clean war grabverRelease Evaluation triggered, versioning increased and new values saved

Note: File version.properties is auto-generated, but once it's created, you can modify its content as of your convenience. Just remember to add it to your Version Control System (from time to time).

Contributions

Everybody is welcome to improve existing solution.

Note: Unit tests work fine if you open the project with IntelliJ Idea, while with Android Studio they don't. Alternatively, you can simulate a real build script by running gradle install and gradle -b build-test.gradle [grabverRelease] OR testing with modules gradle build [grabverRelease].

License

Copyright 2017-2019 Davidea Solutions Sprl

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
You can’t perform that action at this time.