Gradle Incremental Builds - Declaring Inputs and Outputs with Runtime API: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
(Created page with " <font color=darkgray>TODO https://docs.gradle.org/current/userguide/more_about_tasks.html#sec:task_input_output_runtime_api</font>")
 
 
(8 intermediate revisions by the same user not shown)
Line 1: Line 1:
=External=
* https://docs.gradle.org/current/userguide/more_about_tasks.html#sec:task_input_output_runtime_api
=Internal=
* [[Gradle_Incremental_Builds#Declaring_Inputs_and_Outputs_with_Runtime_API|Gradle Incremental Builds]]
=Overview=
Annotations in custom task types are the cleanest way to declare inputs and outputs. However, simple tasks can be configured to participate in incremental builds by using the Runtime API. There is also the case of the enhanced tasks we don't have source code for. In all these situations, we can configure the task's inputs and outputs with an alternative runtime API:


<font color=darkgray>TODO https://docs.gradle.org/current/userguide/more_about_tasks.html#sec:task_input_output_runtime_api</font>
[https://docs.gradle.org/current/dsl/org.gradle.api.Task.html#org.gradle.api.Task:inputs Task.getInputs()]
[https://docs.gradle.org/current/dsl/org.gradle.api.Task.html#org.gradle.api.Task:outputs Task.getOutputs()]
[https://docs.gradle.org/current/dsl/org.gradle.api.Task.html#org.gradle.api.Task:destroyables Task.getDestroyables()]
 
The methods return [https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/TaskInputs.html TaskInputs], [https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/TaskOutputs.html TaskOutputs] and [https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/TaskDestroyables.html TaskDestroyables] instances. These objects have methods that allow specifying files, directories and values. The API is almost at parity with the annotations, it is only missing [[Gradle_Incremental_Builds#Nested_Values|@Nested]].
=Example=
<syntaxhighlight lang='groovy'>
task inLineTask {
 
  inputs.property("engine", TemplateEngineType.FREEMARKER)
  inputs.files(fileTree("src/templates"))
    .withPropertyName("sourceFiles")
    .withPathSensitivity(PathSensitivity.RELATIVE)
  inputs.property("templateData.name", "docs")
  inputs.property("templateData.variables", [year: 2020])
  outputs.dir("$buildDir/genOutput2")
    .withPropertyName("outputDir")
 
  doLast {
    // Process the templates here
  }
}
</syntaxhighlight>

Latest revision as of 03:57, 22 October 2020

External

Internal

Overview

Annotations in custom task types are the cleanest way to declare inputs and outputs. However, simple tasks can be configured to participate in incremental builds by using the Runtime API. There is also the case of the enhanced tasks we don't have source code for. In all these situations, we can configure the task's inputs and outputs with an alternative runtime API:

Task.getInputs()
Task.getOutputs()
Task.getDestroyables()

The methods return TaskInputs, TaskOutputs and TaskDestroyables instances. These objects have methods that allow specifying files, directories and values. The API is almost at parity with the annotations, it is only missing @Nested.

Example

task inLineTask {

  inputs.property("engine", TemplateEngineType.FREEMARKER)
  inputs.files(fileTree("src/templates"))
    .withPropertyName("sourceFiles")
    .withPathSensitivity(PathSensitivity.RELATIVE) 
  inputs.property("templateData.name", "docs")
  inputs.property("templateData.variables", [year: 2020]) 
  outputs.dir("$buildDir/genOutput2")
    .withPropertyName("outputDir")

  doLast {
    // Process the templates here
  } 
}