Michael
Michael

Reputation: 33307

Add Task dependency to existing Plugin Task in Gradle?

I include a second gradle file my.gradle in my build.gradle file.

The content of my.gradle is:

apply plugin: MyPlugin

class MyPlugin implements Plugin<Project> {

    @Override
    void apply(Project project) {
       project.tasks.create(name: "myTask", type: MyTaskClass) {
       }
    }
}

In my build.gradle I set at the top:

apply from: 'myPlugin.gradle'

Now I want to set a task dependency in build.gradle with:

tasks.myPlugin.myTask.dependsOn += someOtherTask

When I build I get the following error:

> Could not find property 'myPlugin' on task set.

How can I access myTask from myPlugin in build.gradle?

Edit: I tried to make sure that someTask runs after myTask. I tried to do this with:

taskX.finalizedBy taskY

in my case:

tasks.myPlugin.myTask.finalizedBy someOtherTask

but the former does not work.

Upvotes: 10

Views: 9707

Answers (2)

Opal
Opal

Reputation: 84854

The following script will do the job:

my.gradle:

apply plugin: MyPlugin

class MyPlugin implements Plugin<Project> {

    @Override
    void apply(Project project) {
       project.tasks.create(name: "myTask", type: Copy) {
       }
    }
}

build.gradle:

apply from: 'my.gradle'

task someOtherTask << {
   println 'doLast'
}

project.tasks.myTask.dependsOn(someOtherTask)

Upvotes: 5

Al Jacinto
Al Jacinto

Reputation: 318

If you include your plugin correctly, you should see your task. For example, if you include java plugin and run

gradle tasks --all

You should see compileJava included. Same for yours, run gradle tasks --all. You should be able to reference it as tasks.myTask or tasks['myTask']

Upvotes: -3

Related Questions