statmark56
11/20/2023, 11:45 PMtasks.register<ExampleTask>("example")
abstract class ExampleTask : DefaultTask() {
@TaskAction
fun initialize() {
println("Hello World")
}
}
But when I run it ./gradlew example
I got:
Could not create task :example
Could not create task of type ExampleTask
Class Build_gradle.ExampleTask is a non-static inner class.
What do I miss? Thanksmbonnin
11/20/2023, 11:48 PMephemient
11/20/2023, 11:58 PMclass ExampleTask
? it must not use anything in the script context, e.g.
val x = file(".")
abstract class ExampleTask : DefaultTask() {
init {
x // bad
file(".") // also bad
statmark56
11/21/2023, 1:12 AMrootProject
. Removing it now works fine. But alas, I will lost all the API I need for the task logic if I choose this option (put the class task inside build script directly) :(ephemient
11/21/2023, 1:23 AMTask.project
inside the task class, so project.rootProject
is OK, just rootProject
is not. however using project
at execution time is not allowed with configuration cache, what are you trying to do?statmark56
11/21/2023, 1:31 AMsois OK,project.rootProject
Ah right. Thank you so much!
usingat execution time is not allowed with configuration cache,project
Oh TIL. But if I don't have the cache enabled, all good?
what are you trying to do?
This task is to create a new File inside certain project directory. Let's just say is for improving dev productivity or so..
ephemient
11/21/2023, 1:35 AMtasks.register("example", ExampleTask::class) {
outputFile.set(rootProject.file("output.txt"))
}
abstract class ExampleTask : DefaultTask() {
@get:OutputFile
abstract val outputFile: RegularFileProperty
@TaskAction
fun initialize() {
outputFile.get().getAsFile().writeText("Hello World")
}
}
statmark56
11/21/2023, 1:37 AMVampire
11/21/2023, 1:57 AMAnd I choose not to put it in buildSrc since it re-invalidates all modules for every single changeDon't use
buildSrc
, but one or more included builds with one or more projects each, then only things get invalidate that actually use the changed thing. 🙂