diff --git a/plug-in-development/index.html b/plug-in-development/index.html index 7f8e00d..f5b2830 100644 --- a/plug-in-development/index.html +++ b/plug-in-development/index.html @@ -453,52 +453,42 @@ kobalt-line-count:exampleTask

Dynamic tasks are useful when you want your plug-in to generate one or several tasks that depend on some other runtime information (therefore, you can't declare a method and put a @Task - annotation on it). Here is the simplest dynamic task you can create in your plug-in class: + annotation on it). Plug-ins declare dynamic tasks by implementing the ITaskContributor + intrface:

-
-override fun apply(project: Project, context: KobaltContext) {
-    println("*** Adding dynamic task")
-    addTask(project, "dynamicTask") {
-        println("Dynamic task")
-        TaskResult()
-    }
-}
-
- +interface ITaskContributor { + fun tasksFor(context: KobaltContext) : List<DynamicTask> +}

- Like a regular task method, the closure you pass to addTask() has to return a TaskResult - object to indicate whether it succeeded or failed. You can - then see your dynamic task in the list of tasks and run it directly: + For example: +

+
+override fun tasksFor(context: KobaltContext) = listOf(
+    DynamicTask(
+        name = "dynamicTask",
+        description = "Description",
+        alwaysRunAfter = listOf("compile"),
+        closure = { project: Project ->
+            println("Running dynamicTask")
+            TaskResult()
+    }))
+

+ DynamicTask mirrors the @Task attributes: name, description and + dependencies. The only addition is the closure parameter, which specifics the code that will + run if your task gets invoked. That closure needs to follow the same constraints that a @Task method + obeys: it takes a Project parameter and returns a TaskResult. +

+

+ Once you have implemented ITaskContributor, you can see your dynamic task in the list of tasks and run it directly:

 $ ./kobaltw --tasks
   ===== kobalt-line-count =====
-    dynamicTask
+    dynamicTask         Description
     lineCount           Count the lines
 $ ./kobaltw dynamicTask
-Dynamic task
-
-

- The addTask() method lets you specify any attribute you can specify on the @Task - annotation: description, runBefore, etc... For example, here is how we would - specify that this task should always run after compile: -

-
-addTask(project, "dynamicTask", alwaysRunAfter = listOf("compile")) {
-    println("Dynamic task")
-    TaskResult()
-}
-
-

- Let's test it: -

- -
-$ ./kobaltw --dryRun compile
-kobalt-line-count:clean
-kobalt-line-count:compile
-kobalt-line-count:exampleTask
+Running dynamictask
 

Properties