Generate your changelog with Gradle changelog plugin

Gradle changelog is a neat plugin by Jetbrains that ease the process of maintaining a changelog following the keep a changelog convention. If like me, you usually use the latest git tag to set the version of your release there’s a little “chicken & egg problem” that needs to be solved in order to ease its use.

Once the plugin is applied to your configuration you can execute gradle patchChangelog to generate your changelog. It will take the content of the unreleased section of your changelog and will create a new section with the version you set in the plugin configuration. But if you use git to provide the version (with another gradle plugin), you probably start to understand where the problem lies :

  • If we set the tag first and launch patchChangelog, we will need to commit the newly generated commit and the tag will be one commit behind where it should be.
  • If we don’t set the tag, well… that’s bad as the changelog will be generated from the n-1 release tag.

Of course we can do it manually :

  1. Set the release tag
  2. Generate the changelog with gradle patchChangelog
  3. Move the tag to the HEAD

But as always, if you can automate it, automate it. So in order to solves this, we just need to create a small gradle task that will depend on the changelog generation task and do the necessary steps for us :

tasks.register("prepareChangelog") {
    dependsOn("patchChangelog")
    doLast {
        exec {
            commandLine("git", "commit", "--amend", "-a", "--no-edit")
        }
        exec {
            commandLine("git", "tag", "$version", "-f", "HEAD")
        }
    }
}

And voilĂ  ! Every time we need to do the final steps of the release we just have to tag and execute gradle prepareChangelog. We could also use a gradle task to do a semi-automated bump of our release version, but that’s for another time.

So if you didn’t know about this plugin, i encourage you to check it out there’s lot of nice options to make your changelog management a breeze. For example, they recently added the ability to merge multiple pre-release changelogs (alpha, beta) into one once the release is ready.