ajoberstar / gradle-git

Git plugin for Gradle
Apache License 2.0
561 stars 89 forks source link

Using grgit without command line parameters #237

Closed vesely-david closed 7 years ago

vesely-david commented 7 years ago

I would like to create my own task releaseFinalMajor which would setup scope to major and stage to final.

task releaseFinalMajor {
    tasks.release {
        stage = 'final'
        scope = 'major'
        dependsOn subprojects.tasks['build']
    }

    dependsOn allprojects.tasks['release']
}

The problem is that I am still getting an error:

No such property: stage for class: org.ajoberstar.gradle.git.release.base.ReleasePluginExtension_Decorated

Please, does anyone know what is wrong with such code? Is it even possible to setup scope and stage parameters by code and not in command line?

ajoberstar commented 7 years ago

The immediate problem you have is that there is no property stage or scope on the release task. There are just properties release.stage and release.scope on the project itself.

The other problem you'll have is that using a task to configure the properties is too late in the process given the way Gradle's evaluation and many common plugins work.

Basically, you need to set those properties as soon as possible to ensure that those are in place before any other part of Gradle asks what version this is.

Your best bet if you really need a task instead of properties is the task graph. This is the rough idea, but I didn't double check the exact property/method names to call.

task releaseFinalMajor

gradle.taskGraph.whenEvaluated { graph ->
  if (graph.containsTask('releaseFinalMajor')) {
    project.ext['release.scope'] = 'major'
    project.ext['release.stage'] = 'final'
  }
}
vesely-david commented 7 years ago

Thank you for your approach. Finally, we decided to set default properties for SNAPSHOT in gradle.properties and release final versions via command line with parameters.

rkoch commented 7 years ago

I'm struggling with the same issue currently and wanted to add this if someone finds this issue in the future.

The following code snippet would work:

task releaseFinalMinor

gradle.taskGraph.whenReady { graph ->
    if (graph.hasTask(releaseFinalMinor)) {
        project.ext['release.scope'] = 'minor'
        project.ext['release.stage'] = 'final'
    }
}

However, there is one major implication: As soon as the maven-publish plugin is applied, the version is inferred even before the taskGraph is ready (my guess that this is part of the initialization phase). Therefore as soon as you use the maven-publish plugin this solution does not work. I haven't been able to find a workaround for this yet.

ajoberstar commented 7 years ago

FYI, maven-publish (and similar ordering issues are the reason I use properties instead of tasks.