[[build_services]]
= Shared Build Services

Sometimes, is it useful for several tasks to share some state or resource. For example, tasks might share a cache of pre-computed values in order to do their work faster.
Or tasks might do their work using a web service or database instance.

Gradle allows you to declare _build services_ to represent this state. A build service is simply an object that holds the state for tasks to use.
Gradle takes care of the service lifecycle, and will create the service instance only when it is required and clean it up once it is no longer required.
Gradle can also optionally take care of coordinating access to the build service, so that no more than a specified number of tasks can use the service concurrently.

== Implementing a build service

To implement a build service, create an abstract class that extends link:{javadocPath}/org/gradle/api/services/BuildService.html[BuildService] and define whichever methods on this type
that you'd like tasks to use.
A build service implementation is treated as a <<custom_gradle_types.adoc#custom_gradle_types,custom Gradle type>> and can use any of the features available to custom Gradle types.

A build service can also optionally take parameters, which Gradle injects into the service instance when creating it. To provide parameters, you define an abstract class or an interface that
holds the parameters. The parameters type must extend link:{javadocPath}/org/gradle/api/services/BuildServiceParameters.html[BuildServiceParameters].
The service implementation can access the parameters using `this.getParameters()`.
The parameters type is also a <<custom_gradle_types.adoc#custom_gradle_types,custom Gradle type>>.

When the build service does not require any parameters, you can use link:{javadocPath}/org/gradle/api/services/BuildServiceParameters.None.html[BuildServiceParameters.None] as the parameters type.

A build service implementation can also optionally implement `AutoCloseable`, in which case Gradle will call the build service instance's `close()` method when it discards the service instance.
This happens some time between completion of the last task that uses the build service and the end of the build.

Here is an example of a service that takes parameters and is closeable:

.Implementing a build service
====
[source.multi-language-sample,java]
.WebServer.java
----
include::{snippetsPath}/plugins/buildService/groovy/buildSrc/src/main/java/WebServer.java[]
----
====

Note that you should not implement the link:{javadocPath}/org/gradle/api/services/BuildService.html#getParameters--[BuildService.getParameters()] method, as Gradle will provide an implementation of this.

A build service implementation must be thread-safe, as it will potentially be used by multiple tasks concurrently.

== Using a build service from a task

To use a build service from a task, add a property to the task of type `Property<MyServiceType>` and mark the property as `@Internal`.
Using a service with any other annotation is currently not supported. For example, it is currently not possible
to mark a service as an input to a task.

Here is an example of a task that uses the previous service:

.Using a build service
====
[source.multi-language-sample,java]
.Download.java
----
include::{snippetsPath}/plugins/buildService/groovy/buildSrc/src/main/java/Download.java[]
----
====

== Registering a build service

To create a build service, you register the service instance using the link:{javadocPath}/org/gradle/api/services/BuildServiceRegistry.html#registerIfAbsent-java.lang.String-java.lang.Class-org.gradle.api.Action-[BuildServiceRegistry.registerIfAbsent()] method.
Registering the service does not create the service instance. This happens on demand when a task first uses the service. If no task uses the service during a build, the service instance will not be created.

Currently, build services are scoped to a build, rather than to a project, and these services are available to be shared by the tasks of all projects.
You can access the registry of shared build services via `Project.getGradle().getSharedServices()`.

Here is an example of a plugin that registers the previous service:

.Build service registration
====
[source.multi-language-sample,java]
.DownloadPlugin.java
----
include::{snippetsPath}/plugins/buildService/groovy/buildSrc/src/main/java/DownloadPlugin.java[]
----
====

The plugin registers the service and receives a `Provider<WebService>` back. This provider can be connected to task properties to pass the service to the task.

Generally, build services are intended to be used by tasks, as they usually represent some state that is potentially expensive to create, and you should avoid using
them at configuration time. However, sometimes it can make sense to use the service at configuration time. This is possible, simply call `get()` on the provider.

== Other ways of using a build service

In addition to using a build service from a task, you can use a build service from a worker API action, an artifact transform or another build service.
To do this, pass the build service `Provider` as a parameter of the consuming action or service, in the same way you pass other parameters to the action or service.
For example, to pass a `MyServiceType` service to worker API action, you might add a property of type `Property<MyServiceType>` to the action's parameters object and
then connect the `Provider<MyServiceType>` that you receive when registering the service to this property.

Currently, it is not possible to use a build service with a worker API action that uses ClassLoader or process isolation modes.

== Concurrent access to the service

You can constrain concurrent execution when you register the service, by using the `Property` object returned from link:{javadocPath}/org/gradle/api/services/BuildServiceSpec.html#getMaxParallelUsages--[BuildServiceSpec.getMaxParallelUsages()].
When this property has no value, which is the default, Gradle does not constrain access to the service. When this property has a value > 0, Gradle will allow no more than the specified number of tasks to use the service concurrently.

[[operation_listener]]
== Receiving information about task execution

A build service can be used to receive events as tasks are executed. To do this, create and register a build service that implements link:{javadocPath}/org/gradle/tooling/events/OperationCompletionListener.html[OperationCompletionListener].
Then, you can use the methods on the link:{javadocPath}/org/gradle/build/event/BuildEventsListenerRegistry.html[BuildEventsListenerRegistry] service to start receiving events.
