Okay, you can absolutely control whether the gradle-release plugin (net.researchgate.release) performs a MAJOR, MINOR, or PATCH increment using properties. This is a common pattern for automating releases in CI/CD pipelines or allowing command-line control.
The key is to read a project property within the release block's configuration and set the incrementType based on its value.
Here's how you can do it:
- Define a Property Name: Choose a property name that you will use, for example,
releaseIncrement.
- Configure the
release Block: Inside your build.gradle (or build.gradle.kts), access the property and set incrementType accordingly.
Example using build.gradle (Groovy DSL):
plugins {
id 'java' // or 'groovy', 'kotlin', etc.
id 'net.researchgate.release' version '3.0.2' // Use the latest version
}
// ... other project configuration ...
release {
// Default to PATCH if the property is not provided or invalid
def increment = project.hasProperty('releaseIncrement') ? project.property('releaseIncrement').toUpperCase() : 'PATCH'
if (increment == 'MAJOR') {
incrementType = 'MAJOR'
logger.lifecycle('Release increment type set to: MAJOR')
} else if (increment == 'MINOR') {
incrementType = 'MINOR'
logger.lifecycle('Release increment type set to: MINOR')
} else {
// Default or invalid value case
incrementType = 'PATCH'
logger.lifecycle('Release increment type set to: PATCH (default or invalid value)')
}
// Optional: Configure scm adapter, version formatting, etc.
// scm {
// adapter = 'git'
// requireBranch = 'main' // or 'master'
// }
// version {
// git {
// // customize version naming if needed
// }
// }
}
// ... other tasks ...
Example using build.gradle.kts (Kotlin DSL):
plugins {
java // or groovy, kotlin, etc.
id("net.researchgate.release") version "3.0.2" // Use the latest version
}
// ... other project configuration ...
configure<net.researchgate.gradle.plugins.release.ReleasePluginExtension> {
// Default to PATCH if the property is not provided or invalid
val increment = if (project.hasProperty("releaseIncrement")) {
project.property("releaseIncrement").toString().toUpperCase()
} else {
"PATCH"
}
when (increment) {
"MAJOR" -> {
incrementType = "MAJOR"
logger.lifecycle("Release increment type set to: MAJOR")
}
"MINOR" -> {
incrementType = "MINOR"
logger.lifecycle("Release increment type set to: MINOR")
}
else -> {
// Default or invalid value case
incrementType = "PATCH"
logger.lifecycle("Release increment type set to: PATCH (default or invalid value)")
}
}
// Optional: Configure scm adapter, version formatting, etc.
// scm {
// adapter = "git"
// requireBranch = "main" // or "master"
// }
// version {
// git {
// // customize version naming if needed
// }
// }
}
// ... other tasks ...
How to Use:
You would then trigger the release task (or specific release tasks like releasePrepare, releasePerform) from the command line, passing the property using the -P flag:
-
For a Patch Release (default):
./gradlew release
or
./gradlew release -P releaseIncrement=PATCH
-
For a Minor Release:
./gradlew release -P releaseIncrement=MINOR
-
For a Major Release:
./gradlew release -P releaseIncrement=MAJOR
Explanation:
project.hasProperty('releaseIncrement'): Checks if a property named releaseIncrement was passed to the build (e.g., via -P or defined in gradle.properties).
project.property('releaseIncrement').toUpperCase(): If the property exists, it retrieves its value and converts it to uppercase for case-insensitive comparison.
- The
if/else if/else (or when in Kotlin) block then checks the uppercase value and sets the release.incrementType accordingly.
- If the property is not provided, or if its value is anything other than "MAJOR" or "MINOR" (case-insensitive), it defaults the
incrementType to "PATCH".
- The
logger.lifecycle lines are helpful for build logs to show which increment type was selected.
This setup gives you flexible control over your release versioning without modifying your build script for each release type.