This message was deleted.
# plugin-development
s
This message was deleted.
1
c
Injecting ProjectLayout
j
That was the right answer, thanks @CristianGM
Copy code
abstract class AddTask @Inject constructor(
    @get:Input val projectLayout: ProjectLayout
) : DefaultTask() {
👍 1
v
Just in case you are curious about the actual error. You tried to access
rootdir
from the script object, so the
AddTask
class got automatically a parameter to receive a reference to the enclosing object. And then Gradle couldn't instantiate it and suggested to annotate it with
@Inject
which actually wouldn't have helped in that situation. By using the injected project layout you do no longer reference the enclosing object and so no extra parameter is added to the constructor.
j
Thanks for the explanation @Vampire
👌 1
j
But projectLayout has access to to root dir?
j
val file = projectLayout.projectDirectory.file("some/path").asFile
v
Or in other words, no 😄
j
Then @jmfayard can be having issues if he wants the root dir, so the only way is passing it from outsite the task, right?
v
In his question he said he wants to access
projectDir
or
file("xxx")
and for that
ProjectLayout
is perfectly fine. Just the code example used
rootDir
which of course is only the same if you are in the root project.
To really get the root dir, you probably have to pass it in, yes
j
I have a single-project build, I don't really care
👌 1
j
Thank you all 🙂
t
Fwiw, tasks all have a
getProject()
too, so:
Copy code
tasks.register<AddTask>("add")

abstract class AddTask : DefaultTask() {
    @TaskAction fun test() = println(this.project.rootDir)
}
j
That break cache, right?
v
Configuration cache, yes