从位置获取存根或合约定义
除了从 Artifactory、Nexus 或 Git 中选取存根或合约定义,您还可以指向驱动器或类路径上的某个位置。这在多模块项目中尤其有用,其中一个模块希望重用另一个模块的存根或合约,而无需将这些内容安装到本地 Maven 仓库或提交到 Git。
为此,当在Stub Runner或Spring Cloud Contract插件中设置仓库根参数时,你可以使用stubs://协议。
在这个示例中,producer 项目已成功构建,存根文件生成在 target/stubs 文件夹下。作为消费者,可以通过使用 stubs:// 协议来设置 Stub Runner,从该位置获取存根文件。
@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);
合约和存根可以存储在一个位置,每个生产者都有其专用的合约和存根映射文件夹。在该文件夹下,每个消费者可以拥有自己的设置。为了让存根运行器从提供的ID中找到专用文件夹,你可以传递 stubs.find-producer=true 属性或 spring.cloud.contract.stubrunner.stubs.find-producer=true 系统属性。以下列表展示了合约和存根的排列方式:
└── 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
消费者的组 ID
工件 ID 为 [some-artifact-id] 的消费者
工件 ID 为 [some-artifact-id] 的消费者的合约
工件 ID 为 [some-artifact-id] 的消费者的映射
工件 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());