gradlebuild.gradle

Gradle Distribution plugin: how to make the tasks "opt-in?"


Gradle 8.12, using Groovy syntax.

I want to include the distribution plugin in my build but I don't want its tasks automatically placed in the build dependency tree. In other words, I want to "opt-in" to running distZip only when needed, not whenever build task is run.

Right now, if I run gradlew build, distZip is included. When I want to use distZip I want to explicitly request it (ie, with gradlew distZip). I do not want to totally disable distZip, just make it an explicit choice.

What's the cleanest/simplest way to accomplish that?


Solution

  • You can use onlyIf to run only if some condition is met. For example, the someProperty value must be set in order for the task to run:

    tasks {
        distZip {
            onlyIf("property present") {
                hasProperty("someProperty")
            }
        }
    }
    

    The distZip will only run when someProperty is set, so ./gradlew build would not execute distZip.

    But ./gradlew build -PsomeProperty will since someProperty is specified.