Fetching Stubs or Contract Definitions From A Location
您无需从 Artifactory、Nexus 或 Git 中选择存根或合约定义,而是可以指向驱动器或类路径上的位置。这样做在多模块项目中特别有用,其中一个模块需要重用另一个模块的存根或合约,而无需实际将它们安装到本地 maven 存储库以将这些更改提交到 Git。
Instead of picking the stubs or contract definitions from Artifactory, Nexus, or Git, you can point to a location on a drive or the classpath. Doing so can be especially useful in a multi-module 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 to commit those changes to Git.
为了实现此目的,当在 Stub Runner 或 Spring Cloud Contract 插件中设置存储库根参数时,可以使用“stubs://”协议。
In order to achieve this, you can use the stubs://
protocol when the repository root parameter is set either
in Stub Runner or in a Spring Cloud Contract plugin.
在此示例中,“producer”项目已成功构建,并且在“target/stubs”文件夹下生成了存根。作为使用者,可以通过使用“stubs://”协议设置 Stub Runner 从该位置选取存根。
In this example, the producer
project has been successfully
built and stubs were generated under the target/stubs
folder. As a consumer, one can set up the Stub Runner to pick the stubs from that location by using the stubs://
protocol.
@AutoConfigureStubRunner(
stubsMode = StubRunnerProperties.StubsMode.REMOTE,
repositoryRoot = "stubs://file://location/to/the/producer/target/stubs/",
ids = "com.example:some-producer")
@Rule
public StubRunnerRule rule = new StubRunnerRule()
.downloadStub("com.example:some-producer")
.repoRoot("stubs://file://location/to/the/producer/target/stubs/")
.stubsMode(StubRunnerProperties.StubsMode.REMOTE);
@RegisterExtension
public StubRunnerExtension stubRunnerExtension = new StubRunnerExtension()
.downloadStub("com.example:some-producer")
.repoRoot("stubs://file://location/to/the/producer/target/stubs/")
.stubsMode(StubRunnerProperties.StubsMode.REMOTE);
合约和存根可以存储在一个位置,其中每个生产者都具有自己专门的用于合约和存根映射的文件夹。在该文件夹中,每个使用者都可以拥有自己的设置。为了使 Stub Runner 能够从提供的 ID 中找到专门的文件夹,您可以传递“stubs.find-producer=true”属性或“stubrunner.stubs.find-producer=true”系统属性。以下列表显示了合约和存根的安排:
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, you can pass the stubs.find-producer=true
property or the stubrunner.stubs.find-producer=true
system property.
The following listing shows an arrangement of contracts and stubs:
└── com.example 1
├── some-artifact-id 2
│ └── 0.0.1
│ ├── contracts 3
│ │ └── shouldReturnStuffForArtifactId.groovy
│ └── mappings 4
│ └── shouldReturnStuffForArtifactId.json
└── some-other-artifact-id 5
├── contracts
│ └── shouldReturnStuffForOtherArtifactId.groovy
└── mappings
└── shouldReturnStuffForOtherArtifactId.json
1 | group ID of the consumers |
2 | consumer with artifact id [some-artifact-id] |
3 | contracts for the consumer with artifact id [some-artifact-id] |
4 | mappings for the consumer with artifact id [some-artifact-id] |
5 | consumer with artifact id [some-other-artifact-id] |
@AutoConfigureStubRunner(
stubsMode = StubRunnerProperties.StubsMode.REMOTE,
repositoryRoot = "stubs://file://location/to/the/contracts/directory",
ids = "com.example:some-producer",
properties="stubs.find-producer=true")
static Map<String, String> contractProperties() {
Map<String, String> map = new HashMap<>();
map.put("stubs.find-producer", "true");
return map;
}
@Rule
public StubRunnerRule rule = new StubRunnerRule()
.downloadStub("com.example:some-producer")
.repoRoot("stubs://file://location/to/the/contracts/directory")
.stubsMode(StubRunnerProperties.StubsMode.REMOTE)
.properties(contractProperties());
static Map<String, String> contractProperties() {
Map<String, String> map = new HashMap<>();
map.put("stubs.find-producer", "true");
return map;
}
@RegisterExtension
public StubRunnerExtension stubRunnerExtension = new StubRunnerExtension()
.downloadStub("com.example:some-producer")
.repoRoot("stubs://file://location/to/the/contracts/directory")
.stubsMode(StubRunnerProperties.StubsMode.REMOTE)
.properties(contractProperties());