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.

License

Notifications You must be signed in to change notification settings

davideas/GrabVer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 plugin management in settings.gradle file:

pluginManagement {
    repositories {
        maven { url "https://plugins.gradle.org/m2/" }
    }
}

Configure build script in each build.gradle file:

plugins {
    id "eu.davidea.grabver" version "2.0.3"
}

Usage

1. Version configuration

Apply the plugin in the module you desire, it will create a version.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>"
}

💬 Note: The 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).

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] #buildNr built on yyyy.MM.dd"
versioning.builtOn     // output: " built on yyyy.MM.dd"
versioning.date        // or versioning.getDate([format]) - default "yyyy.MM.dd"

// Example: grab the version name
version = versioning.name

⚠️ Note: To trigger the evaluation, 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

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-2024 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.

About

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.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published