The version catalog in Gradle is very useful to have one place in our project to define our project and plugin dependencies with their versions. But we can also use it to define our project version and then refer to that version from the version catalog in our build script file. That way the version catalog is our one place to look for everything related to a version. In the version catalog we have a versions section and there we can define a key with a version value. The name of the key could be our project or application name for example. We can use type safe accessors generated by Gradle in our build script to refer to that version.

In the following example build script written with Kotlin we see how we can refer to the version from the version catalog:

// File: build.gradle.kts
description = "Sample project for Gradle version catalog"

// Set version using version catalog.
version = libs.versions.app.version.get()

// We can use the TaskContainer to keep all
// task related things in one place.
tasks {
    // Register a new task to print out the project version.
    register("projectVersion") {
        doLast {
            println("Project version: " + version)
        }
    }
}

And the version catalog is defined in the following file:

# File: gradle/libs.versions.toml
[versions]
app-version = "2.0.1"

When we run the task projectVersion we see our project version in the output:

$ gradle projectVersion

> Task :projectVersion
Project version: 2.0.1

BUILD SUCCESSFUL in 831ms
1 actionable task: 1 executed
$

Written with Gradle 7.6.

shadow-left