researchgate / gradle-release

gradle-release is a plugin for providing a Maven-like release process for projects using Gradle
MIT License
859 stars 223 forks source link

Adapt version in beforeReleaseBuild #350

Open christiangroth opened 3 years ago

christiangroth commented 3 years ago

Hey guys,

first of all thanks for the great plugin, so far I solved a lot of scenarios using it! :)

Right now I'm trying to derive the new project version depending on our release notes. What I want to achieve is the following:

So what I did was quite simple:

What I see is that the tast is executed correctly and gradle.properties file is also correct and committed correctly. But lets look at an example with current version 0.1.1-SNAPSHOT and creating a feature release.

So I assume that the release plugin does not get the updated gradle project version. Any ideas/hints/help on this? I did not quite find the code pointer in the release plugin for a possible fix. I would have assumed that setting the project.version value before the release task will be reflected during the release task.

christiangroth commented 3 years ago

for the sake of completeness I'll also provide the sources of my task. It is included in a custom plugin, so not all private methods may be available, but hopefully all you need in this case.

    private fun Project.registerUpdateProjectVersion() {
        tasks.register(updateProjectVersion) {
            it.group = releasenotesGroupName

            it.doLast {
                val extensions = listOf(extension.de, extension.en)
                val updateNotices = extensions.flatMap { extension ->
                    extension.collectSnippets(project.projectDir, ReleasenoteSnippetType.UPDATENOTICE)
                }
                if (updateNotices.isNotEmpty()) {
                    logger.info("detected update notice release notes snippets, configuring major version bump")
                    project.updateVersion(project.versionAsSemver().incMajor())
                    return@doLast
                }

                val features = extensions.flatMap { extension ->
                    extension.collectSnippets(project.projectDir, ReleasenoteSnippetType.FEATURE)
                }
                if (features.isNotEmpty()) {
                    logger.info("detected feature release notes snippets, configuring minor version bump")
                    project.updateVersion(project.versionAsSemver().incMinor())
                    return@doLast
                }

                // we do not need to do anything for bugfixes / patch level changes
                // (we also did never commit changes to gradle.properties in these cases)
                logger.info("all existing release note snippets (if any) lead to patch level bump only")
            }
        }.let { updateProjectVersionTask ->
            afterEvaluate {
                tasks.findByPath(beforeReleaseBuildTaskName)?.let { beforeReleaseBuildTask ->
                    logger.info("Task '${beforeReleaseBuildTask.path}' found, will depend on ${updateProjectVersionTask.name}")
                    beforeReleaseBuildTask.dependsOn(updateProjectVersionTask)
                }
            }
        }
    }

    private fun Project.versionAsSemver() = Semver(project.version.toString())

    private fun Project.updateVersion(newVersion: Semver) {
        project.version = newVersion

        val properties = Properties()
        properties.load(project.propertiesFile().inputStream())
        properties.setProperty("version", newVersion.toString())
        properties.store(project.propertiesFile().outputStream(), null);
    }

    private fun Project.propertiesFile() = rootDir.resolve("gradle.properties")