Sure way of finding any other tasks in a custom plugin

plugins

(Shawn Crain) #1

I’m writing a plugin that creates a task for other tasks identified by the user within a configuration extension. I’m having difficulty with finding an approach that guarantees all tasks are available… at least within the project.tasks collection. I started off with a project.afterEvaluate closure but that’s not working in some cases. After some research it seems the another approach would be to switch to using project.tasks.whenTaskAdded but just wanted to see if there might be another approach.

Thanks


(Stefan Oehme) #2

Could you please provide an example project that shows what you are trying to do and where it is failing?


(Shawn Crain) #3

Below is a bare bones build.gradle example… the jar task is found, but the pom related task is not.

plugins {
    id 'java'
    id 'maven-publish'
}
apply plugin: ExamplePlugin

ext {
    findTasks = ['jar', 'generatePomFileForMavenJavaPublication']
}

class ExamplePlugin implements Plugin<Project> {
    void apply(Project project) {
        project.afterEvaluate{
            project.findTasks.each {
                assert project.tasks.findByName(it)
            }
        }
    }
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
        }
    }
}

(Stefan Oehme) #4

Thanks for the example! The problem is that the maven-publish plugin is currently based on model rules which are executed after the evaluation phase. Until that is improved, you can use the all method on the task container:

project.afterEvaluate{ // so the user has a chance to configure 'findTasks'
  project.tasks.all { task -> // so this is applied to all tasks, whether already created or not
    if (project.findTasks.contains(task.name)) {
      println task.name
    }
  }
}

(Shawn Crain) #5

That did the trick! Thanks!