如何 运行 本地合同与 Spring 云合同存根 运行ner

How to run local contracts with Spring Cloud Contract stub runner

我定义了一些 Spring 云合同,我使用 gradle verifierStubsJar 打包到一个 jar 中。我想 运行 那个罐子里的存根 Spring Cloud Contract stub runner jar。我不想必须将存根 jar 发布到工件存储库,如 Artifactory 或本地 Maven 存储库。我只想 运行 存根。如何将包含存根的罐子的位置传递给存根 运行ner jar?

如果您阅读文档,您会找到此部分 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")

如果你使用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

如果你使用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

陷阱:

  • 使用 gradle generateClientStubs 时,不要忘记存根目录的绝对 URI 中的前导斜杠。例如stubs://file:///User/me/my-contracts-project/build/stubs

  • 存根 运行ner 在端口 8080 上启动服务器,因此您必须 运行 您的存根在不同的端口上。您可以用通常的 -Dserver.port 覆盖存根 运行ner 启动的端口。

经过大量的搜索和努力,这对我有用。

将此添加到生产方 build.gradle:

configurations {
    // create a configuration that other modules can use
    contractStubs {
        canBeResolved = false
        canBeConsumed = true
    }
}

artifacts {
    // link the stub jar to the configuration
    contractStubs(verifierStubsJar)
}

将此添加到消费者端build.gradle:

dependencies {
    testImplementation project(path: ':some-path:probucer-dir', configuration: 'contractStubs')
}

有关详细信息,请参阅: https://docs.gradle.org/current/userguide/cross_project_publications.html