spring-cloud-contract-samples
spring-cloud-contract-samples copied to clipboard
PACT samples don't work for gradle builds
Was about to post this bug, but it's already registered.
I will add more info on this.
I was able to publish pact contracts from consumer_pact project successfully.
But I am having troubles while generating producer tests with ./gradlew generateContractTests
for producer_pact.
The error I get is:
> Task :copyContracts FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':copyContracts'.
> Remote repositories for stubs are not specified and work offline flag wasn't passed
Sounds weird, because It's obvious that pact broker url is specified: https://github.com/spring-cloud-samples/spring-cloud-contract-samples/blob/787a660652038c241c49803ad089c699fc3092e8/producer_pact/build.gradle#L59-L61
Based on what I can see in debugger the problem is that SpringFactoriesLoader
fails to discover StubDownloaderBuilder
implementations (in particular PactStubDownloaderBuilder
).
I guess the classpath of Gradle's copyContracts
task execution simply does not contain the org.springframework.cloud:spring-cloud-contract-pact
. I tried to play with the Gradle's configuration for this dependency (tried contractTestImplementation
, implementation
and api
), but no luck. I would have expected contractTestImplementation
to work tbh.
UPD: Found a workaround. Placing the following on the top of build script fixes the problem:
buildscript {
dependencies {
classpath("org.springframework.cloud:spring-cloud-contract-pact:3.1.1")
}
}
If you have a multi-module Gradle project, this should be placed into the root build.gradle
, otherwise it won't work.
@shanman190 do you think we have a problem with the gradle plugin?
@marcingrzejszczak, yeah this is a bug in the plugin. At the moment, we have the dependencies to enable use of local and git-based remote contracts. The Gradle plugin is presently doing the downloading from within the Gradle process itself which explains why when @gavvvr adds the pact dependency to Gradle's buildscript it then gains the capability to download pact broker based contracts. I do think we could shove the download work itself into a worker (assuming that Kotlin doesn't creep back into the classpath again) and take advantage of the contractTestInplementation
classpath so that end users don't have to double specify the dependency though.
I would have expected though that the dependency should be workable on the buildscript classpath anywhere the Spring Cloud Contract plugin is applied (ie. If it's on a root project, then the dependency needs to appear there; if it's only on a subproject, then the dependency could appear there per project as needed).
Great, will you look into that in your spare time @shanman190 ?
Absolutely, @marcingrzejszczak. I'll see about submitting a PR with the fix over the next few days.
Thanks, I really appreciate it @shanman190 !