How to define gradle tasks dependency in a task in custom gradle plugin

Viewed 88

I'm writing a custom gradle plugin in which I want to have a bunch of common for several of my projects tasks and a sort of a 'main' task to control which of these tasks to turn on.

Regular tasks in the plugin are e.g.: CopyDockerResourcesTask CopyContainerFilesTask PerformAnalysisTask

and the 'main' task is: BaseProjectTask

so then in the project in build.gradle I'd like to be able to do this:

BaseProjectTask {
  copyDockerResources = true
  copyContainerFiles = true
  performAnalysis = true
}

I want the default behaviour of the plugin to be to not to do anything, only add certain tasks if they are turned on in BaseProjectTask.

I wanted to achieve this with adding task dependency in @TaskAction method of BaseProjectTask:

class BaseProjectTask extends DefaultTask {

    private final BaseProjectExtension extension
    private final Project project

    @Optional
    @Input
    Boolean copyContainerFiles = false
    ...

    @Inject
    BaseProjectTask(Project project, BaseProjectExtension extension) {
        this.project = project
        this.extension = extension
    }

    @TaskAction
    def execute() {
        if (copyContainerFiles) {
            project.tasks.assemble.dependsOn(project.tasks.copyContainerFiles)
        }
        ...
    }
}

Creating task dependency, this line:

project.tasks.assemble.dependsOn(project.tasks.copyContainerFiles)

doesn't work.

Edit: My current findings are that defining task dependency in @TaskAction is too late as this is execution phase. I could do it in the constructor (this way it works) but its too early as property copyContainerFiles isn't set yet.

Does anyone know a way of adding code in the task class that would be fired in the configuration phase? I think this is what I'm missing.

0 Answers
Related