问题
I have defined some Spring Cloud Contracts that I have packaged into a jar using gradle verifierStubsJar
. I would like to run the stubs in that jar with the Spring Cloud Contract stub runner jar. I do not want to have to publish the stubs jar to an artifact repository like Artifactory or a local Maven repository. I just want to run the stubs. How do I pass the location of the jar containing my stubs to the stub runner jar?
回答1:
If you use gradle generateClientStubs
:
curl -Lo stub-runner.jar https://search.maven.org/remotecontent?filepath=org/springframework/cloud/spring-cloud-contract-stub-runner-boot/2.2.1.RELEASE/spring-cloud-contract-stub-runner-boot-2.2.1.RELEASE.jar
java -Djava.security-egd=/dev/./urandom -Dstubrunner.repositoryRoot=stubs://file://absolute/path/to/build/stubs -Dstubrunner.ids=com.company-contract:stubs:8081 -Dstubrunner.stubs-mode=REMOTE -jar stub-runner.jar
If you use gradle verifierStubsJar
:
curl -Lo stub-runner.jar https://search.maven.org/remotecontent?filepath=org/springframework/cloud/spring-cloud-contract-stub-runner-boot/2.2.1.RELEASE/spring-cloud-contract-stub-runner-boot-2.2.1.RELEASE.jar
java -cp stub-runner.jar:build/libs/my-contract-stubs.jar -Djava.security-egd=/dev/./urandom -Dstubrunner.ids=com.company:my-contract:stubs:8081 -Dstubrunner.stubs-mode=CLASSPATH org.springframework.boot.loader.JarLauncher
Gotchas:
When using
gradle generateClientStubs
don't forget the leading slash in the absolute URI to your stubs directory. e.g.stubs://file:///User/me/my-contracts-project/build/stubs
.The stub runner starts a server on port 8080, so you must run your stubs on a different port. You can override the port the stub runner starts on with the usual
-Dserver.port
.
回答2:
If you read the docs you would find this section https://cloud.spring.io/spring-cloud-contract/reference/html/project-features.html#features-stub-runner-stubs-protocol
Instead of picking the stubs or contract definitions from Artifactory / Nexus or Git, one can just want to point to a location on drive or classpath. This can be especially useful in a multimodule project, where one module wants to reuse stubs or contracts from another module without the need to actually install those in a local maven repository ot commit those changes to Git.
In order to achieve this it’s enough to use the stubs:// protocol when the repository root parameter is set either in Stub Runner or in a Spring Cloud Contract plugin.
In this example the producer project has been successfully built and stubs were generated under the target/stubs folder. As a consumer one can setup the Stub Runner to pick the stubs from that location using the stubs:// protocol.
@AutoConfigureStubRunner( stubsMode = StubRunnerProperties.StubsMode.REMOTE, repositoryRoot = "stubs://file://location/to/the/producer/target/stubs/", ids = "com.example:some-producer")
Contracts and stubs may be stored in a location, where each producer has its own, dedicated folder for contracts and stub mappings. Under that folder each consumer can have its own setup. To make Stub Runner find the dedicated folder from the provided ids one can pass a property stubs.find-producer=true or a system property stubrunner.stubs.find-producer=true .
@AutoConfigureStubRunner( stubsMode = StubRunnerProperties.StubsMode.REMOTE, repositoryRoot = "stubs://file://location/to/the/contracts/directory", ids = "com.example:some-producer", properties="stubs.find-producer=true")
来源:https://stackoverflow.com/questions/59774232/how-to-run-local-contracts-with-spring-cloud-contract-stub-runner