Hey. I am looking into using a build service that ...
# configuration-cache
j
Hey. I am looking into using a build service that does some logging during the execution phase. For example, logging the things that are printed to the console also into a file. The only thing I am aware of to make Gradle start such a service early outside of a task is
getEventsListenerRegistry().onTaskCompletion(myServie)
(full example). Are there any other hooks like that available? Ideally I would like to force my service to start before task execution (i.e. after CC was loaded) and not just when the first task has completed.
v
What I usually do in such cases is a manual
.get()
on the build service during configuration time to have it started if the configuration actually executes and additionally the
onTaskCompletion
to have it latest created after the first task finished.
If you need it even earlier the only thing that comes to mind is
Copy code
tasks.configureEach {
    doFirst {
        theBuildService.get()
    }
}
Unless someone clears the task actions later on of course 😄
j
Thanks for the additional ideas. I am mostly interested in things happening outside task execution and was wondering if there is another place to force the service creation apart from
onTaskCompletion
. Seems like there is not. Maybe adding the
doFirst
is interesting. Doing a
get()
during config phase (in addition) leads to different behavior depending on whether or not CC is used. I am not sure if I want that.
v
Yeah, depends on the concrete use-case. I for example use it to output some service message on TeamCity. I want those to be emitted as soon as possible so I do it at configuration time or after the first task was executed. If you find another spot to hook in, let me know. 🙂
j
Finally got back to this. Thanks @Vampire, adding the "doFirst" to the mix of things we already do solved it in the end. Are you aware of a GH issue on this topic?
v
I guess you are after a flow action that runs before the first task runs: https://github.com/gradle/gradle/issues/29064
j
I think going through build services may be another angle to approach the topic. I creates another issue to have the solution written down. Although I don't really care which way this will go. Right now, it seems to be unclear where the line between FlowAction and BuildService is. Maybe the latter is only really meant to be used in task actions. But then the "a build service can implement
OperationCompletionListener
" does not make much sense to me.
v
Nah, build services are not only for usage in task actions, they are also to listen to task completion as you mentioned and also to communicate safely between different projects. And if it is
AutoCloaseable
and registered as completion listener, it can be used for some "after build action" for which then flow actions were invented as a successor with more potential for other flow points.