I'm trying to create my first Gradle plugin.
1. Add extension for properties: project.extensions.create("abc", AbcExtension)
2. Define copy task. When I define task following way
project.task("abcTask", type: Copy) {
from project.abc.fromPath
into project.abc.intoPath
}
project.abc.fromPath equals to AbcExtension.fromPath
value - it doesn't read values from build.gradle
.
When I define task following way
project.task("abcTask", type: Copy) << {
from project.abc.fromPath
into project.abc.intoPath
}
it always print UP-TO-DATE
and doesn't run task.
Pls explain this behaviour and tell me what is correct way to define tasks in Gradle plugins (with type
and dependsOn
functionallity)
Plugins have to defer every read of a mutable build model value (i.e. anything that can be set from a build script) until at least the end of the configuration phase. There are several ways to achieve this goal. Among them are:
Copy.from
)project.afterEvaluate {}
or gradle.projectsEvaluated {}
Choosing the best option for the job at hand requires some expertise. (It may help to study some of the plugins in the Gradle codebase.) In your case, I might do the following:
project.task("abcTask", type: Copy) {
from { project.abc.fromPath }
into { project.abc.intoPath }
}
Your <<
version doesn't work because it configures the Copy
task too late. Generally speaking, all configuration should take place in the configuration phase, rather than the execution phase. You can learn more about Gradle build phases in the Gradle User Guide.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With