I'm in my `root` project where i have a `build.gra...
# community-support
a
I'm in my
root
project where i have a
build.gradle
file. I also have a subproject here, called
app
. (this is an android project). I'm trying to run a script after each
build
task. However, gradle can't find that
build
task. In my root's
build.gradle
I have :
Copy code
tasks.named("build") {
    finalizedBy("myTask")
}
But I keep getting:
Copy code
Task with name 'build' not found in root project 'MyProject'.
But if I ran
./gradlew tasks
I get
Copy code
Build tasks
-----------
assemble - Assemble main outputs for all the variants.
assembleAndroidTest - Assembles all the Test applications.
assembleUnitTest - Assembles all the unit test applications.
build - Assembles and tests this project.
I also tried
Copy code
tasks.named(":app:build") {
    finalizedBy("myTask")
}
but I get the very same result
The overall idea was to run
myTask
after each build of my project
v
./gradlew build
is not
./gradlew :build
. The latter runs the task
build
in the root project. The former runs the task
build
in any project that has a task with that name.
./gradlew tasks
accounts for that and shows
build
as there is a project with that task. If you would do
./gradlew tasks --all
you would get the concrete tasks with their full path. In your root project you do not apply any plugin that registers a task with name
build
. Hence you also cannot add a
finalizedBy
to it. Your last try cannot work, as the root project does not have a task called
:app:build
. You would use the first version, but in the
app
build script if you really want to do that. Having said all that, it is most probably a bad idea to do it like that. Maybe you want to take a step back from your XY problem and tell us a bit about your use-case instead of the intended solution, then you might get a better and cleaner alternative recommended. What is the
myTask
supposed to do?
a
I want to move some
git hooks
files into the
.git/hooks
directory. But we need to make sure they are there, before each commit/push.
So we thought that moving them whenever we build the project is a good idea, since we check the project runs before we commit
v
Is there a reason to not simply do it in the configuration phase directly if you want to do it on any build anyway?
a
No, any phase that guarantees the files are there is fine. I don't know how to do it in the configuration phase, though
v
Just do it directly in the root project build script on top-level
a
the task is in the
build-logic
folder and I'm using a plugin to configure and register the tasks
Copy code
// root build.gradle file

plugins {
    id("gitHooks")
}

gitHooks {
    ktlintCheck = true
    commitMessageCheck = true
}