简体   繁体   中英

Gradle: Is it possible to define a CustomTask in a gradle plugin and extend that class from within the build.gradle?

I am implementing a custom gradle plugin, written in Java.

My custom gradle plugin has a task that will ready a yml file. The yml file is filled with various blocks containing; gradle task name and some args for said task.

My idea here was that my Custom gradle plugin would deliver a couple tasks (all extend some common base with predefined properties). Then if my User would like to define some New Custom tasks from their build.gradle, they could include the task in build.gradle and reference in the yml file.

This works great so far for the tasks that are defined from inside the plugin, I can read gradle tasks from the yml file, set their properties from the args and execute their actions.

Issue I'm having is figuring out how to support custom task definitions from the implementing build.gradle? Initially I was trying to extend my CustomBaseTask from inside the build.gradle but I dont seem to have visibility to this, just the ootb classes (Exec, DefaultTask etc..).

What would be the best approach for defining a custom task from build.gradle and passing predefined properties to it from my gradle plugin?

Is it possible to define a CustomTask in a plugin & extend it from build.gradle

Yes, absolutely it is possible. Just as @Bjorn Vester said: make sure you've imported the class first.

Issue I'm having is figuring out how to support custom task definitions from the implementing build.gradle? Initially I was trying to extend my CustomBaseTask from inside the build.gradle but I dont seem to have visibility to this, just the ootb classes (Exec, DefaultTask etc..). What would be the best approach for defining a custom task from build.gradle and passing predefined properties to it from my gradle plugin?

You can make your custom task like this:

public class SomeTask extends DefaultTask {

     // your predefined attributes          

   @TaskAction
   public void doAction() {
       // do some stuff      
   }
}

and then in build.gradle file you can extend your logic:

tasks.withType(SomeTask::class.java).configureEach {
    // doLast,doFirst...etc. Extend your predefined logic 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM