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.
major: User defined value for breaking changes.
minor: User defined value for new features, but backwards compatible.
patch: User defined value (or auto-generated value) for backwards compatible bug fixes only.
preRelease: Optional, user defined value for pre-releases suffix.
dependsOn: Optional, saving versioning file depends by the task-name specified here (default: compileJava, assembleDebug & assembleRelease).
build - Increases at each build.
code - Increases at each release.
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.
minor - User defined value, it must be coherent(=0) if you increase Major version.
Auto-skip versioning when 'clean', 'test' or 'grabverSkip' tasks are enqueued.
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 repository:
classpath 'eu.davidea:grabver:1.0.0'
// or with Gradle Plugins Repository
classpath "gradle.plugin.eu.davidea:grabver:1.0.0"
}
}
Apply the plugin in the module you desire, it will create a properties file under that module!
apply plugin: 'eu.davidea.grabver'
versioning {
// required (number)
major = 1
minor = 0
// force patch, optional (number)
patch = 7
dependsOn = "<task-name>"
// optional (any string)
preRelease = 'RC1'
}
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.fullVersionName // output: "major.minor.patch[-preRelease] #build built on yyyy.mm.dd"
versioning.builtOn // output: " built on yyyy.mm.dd"
versioning.date // or .getDate([format]) - default "yyyy.mm.dd"
- Via command line:
// To increase build number only:
// - Code and Patch remain unchanged
gradle [build | assembleDebug]
// To increase build, patch and code:
// Code and Patch are increased because of release:
// - Code is increased if exists a task that contains: "bundle", "grabverRelease", ":module:assembleRelease"
// - But Patch can be resetted if Major or Minor is changed or if preRelease is set
gradle [grabverRelease | assembleRelease]
- In Android Studio:
- via menu build > Build APK (assembleDebug) | Generate signed APK (assembleRelease).
- by running the App (assembleDebug | assembleRelease, depending by the build variant).
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.
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 --build-file build-test.gradle [grabverRelease]
OR testing with modules gradle build [grabverRelease]
.
Copyright 2017 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.