Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 341
confile Avatar asked Apr 29 '15 01:04

confile


People also ask

How do I add a dependency in Gradle?

To add a dependency to your project, specify a dependency configuration such as implementation in the dependencies block of your module's build.gradle file.

How do I pass arguments to Gradle task?

If the task you want to pass parameters to is of type JavaExec and you are using Gradle 5, for example the application plugin's run task, then you can pass your parameters through the --args=... command line option. For example gradle run --args="foo --bar=true" .

Can Gradle tasks be created and extended dynamically at runtime?

Tasks can also be created and extended dynamically at runtime. The following listing represents a very simple build file. To execute the hello task in this build file, type gradle hello on the command line in the directory of the build file.


1 Answers

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)
like image 171
Opal Avatar answered Nov 14 '22 23:11

Opal