Class ApiOnlySubscriberExtension
apiOnlySubscriber block: which contracts this project builds
against, and where they come from.
apiOnlySubscriber {
channel {
type = 'maven'
groupId = 'com.example.contracts'
}
subscribe('customer-orders') {
version = '2.1.0'
}
}
A project implements at most one contract, and may call any number of APIs.
subscribe(String, Action) declares the contract it implements; a
second one is refused, because each contract belongs in a project of its own.
subscribeAsClient(String, Action) declares an API it calls, which gets
every check the implemented contract gets.
- See Also:
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionApiOnlySubscriberExtension(org.gradle.api.model.ObjectFactory objects) Creates the extension. -
Method Summary
Modifier and TypeMethodDescriptionvoidchannel(org.gradle.api.Action<? super ChannelSpec> action) Configures the channel.abstract org.gradle.api.provider.Property<String> The version of the contract this project implements, unless its subscription sets its own.The channel every subscription in this project resolves through.abstract org.gradle.api.file.RegularFilePropertyWhere the resolved versions and file hashes are recorded.org.gradle.api.NamedDomainObjectContainer<Subscription> Every declared subscription, keyed by target name.voidsetVersion(Object version) Refuses the property's old name.Declares the contract this project implements, without configuring it.subscribe(String target, org.gradle.api.Action<? super Subscription> action) Declares the contract this project implements, and configures it.subscribeAsClient(String target) Declares an API this project calls, without configuring it.subscribeAsClient(String target, org.gradle.api.Action<? super Subscription> action) Declares an API this project calls, and configures it.subscription(String target) Looks up a declared subscription, so a build file can wire its documents into whatever consumes them.
-
Field Details
-
NAME
The name used to register this extension in a consumer build.- See Also:
-
-
Constructor Details
-
ApiOnlySubscriberExtension
@Inject public ApiOnlySubscriberExtension(org.gradle.api.model.ObjectFactory objects) Creates the extension.Gradle instantiates this when the plugin is applied; a build script configures the instance registered as
apiOnlySubscriber.- Parameters:
objects- Gradle's object factory, supplied by injection
-
-
Method Details
-
getChannel
The channel every subscription in this project resolves through.- Returns:
- the channel specification
-
channel
Configures the channel.channel { type = 'file' directory = "$rootDir/build/publish" }- Parameters:
action- configuration applied to the channel specification
-
getSubscriptions
Every declared subscription, keyed by target name.- Returns:
- the container of subscriptions
-
subscribe
Declares the contract this project implements, and configures it.subscribe('customer-orders') { version = '2.1.0' }Subscribing to the same target twice configures the existing subscription rather than creating a second one.
- Parameters:
target- the contract to subscribe toaction- configuration applied to the subscription- Returns:
- the subscription, so it can be referenced immediately
- Throws:
org.gradle.api.InvalidUserDataException- if this project already implements another contract, or calls this one; the message says why, and what to do
-
subscribe
Declares the contract this project implements, without configuring it.Only useful when the version is set later, since a subscription with no version fails the build when it is resolved.
- Parameters:
target- the contract to subscribe to- Returns:
- the subscription
- Throws:
org.gradle.api.InvalidUserDataException- if this project already implements another contract, or calls this one; the message says why, and what to do
-
subscribeAsClient
public Subscription subscribeAsClient(String target, org.gradle.api.Action<? super Subscription> action) Declares an API this project calls, and configures it.subscribeAsClient('order-payments') { apiContractVersion = '1.4.0' }A project may call any number of APIs, next to the one contract it implements. A client subscription is fetched, checked against its archive's manifest, locked and verified exactly like the implemented contract. It differs in two ways. It sets its own version, because
getApiContractVersion()andapiContractVersionare the version of the contract this project implements. And with thejavaplugin, its documents reach the classpath undercontracts/<target>/, leaving the root to that contract.Subscribing to the same API twice configures the existing subscription rather than creating a second one.
- Parameters:
target- the API to subscribe toaction- configuration applied to the subscription- Returns:
- the subscription, so it can be referenced immediately
- Throws:
org.gradle.api.InvalidUserDataException- if this project implements that contract; a project either implements a contract or calls it
-
subscribeAsClient
Declares an API this project calls, without configuring it.- Parameters:
target- the API to subscribe to- Returns:
- the subscription
- Throws:
org.gradle.api.InvalidUserDataException- if this project implements that contract- See Also:
-
subscription
Looks up a declared subscription, so a build file can wire its documents into whatever consumes them.openApiGenerate { inputSpec = apiOnlySubscriber.subscription('customer-orders').openapi.map { it.asFile.path } }- Parameters:
target- the subscribed contract to look up- Returns:
- the subscription for that target
- Throws:
IllegalArgumentException- if no such subscription was declared; the message lists the targets that were
-
getLockfile
public abstract org.gradle.api.file.RegularFileProperty getLockfile()Where the resolved versions and file hashes are recorded.Defaults to
apionly.lockbeside the build file. It is shared by every subscription in the project and is meant to be committed: it is whatVerifyApiSpecTaskchecks the fetched documents against, and what makes "which contract is this project actually building against?" a question answerable by reading the repository.- Returns:
- the lockfile location
-
getApiContractVersion
The version of the contract this project implements, unless its subscription sets its own. An API the project calls is not affected.Defaults to the
apiContractVersionproject property, so the version can live ingradle.properties-- in a multi-project build, the subproject's own -- in the build script'sext, or inORG_GRADLE_PROJECT_apiContractVersion. Given on the command line, with-PapiContractVersion=..., the property overrides every version the build sets for the implemented contract.apiOnlySubscriber { apiContractVersion = '2.1.0' subscribe('customer-orders') }- Returns:
- the default contract version; unset when the property is not defined
-
setVersion
Refuses the property's old name. Without it, a build script that still setsversionhere would set the project's own version instead, silently.- Parameters:
version- ignored- Throws:
org.gradle.api.GradleException- always, naming the new property
-