我如何用 Git 作为合同和存根的存储?
在多语世界中,有些语言不使用二进制存储,例如 Artifactory和Nexus可以。从Spring Cloud Contract 2.0.0版本开始,我们提供 将合同和存根存储在SCM(源控管理)仓库中的机制。目前, 唯一支持的SCM是Git。
仓库必须具备以下设置 (你可以在这里查看):
.
└── META-INF
└── com.example
└── beer-api-producer-git
└── 0.0.1-SNAPSHOT
├── contracts
│ └── beer-api-consumer
│ ├── messaging
│ │ ├── shouldSendAcceptedVerification.groovy
│ │ └── shouldSendRejectedVerification.groovy
│ └── rest
│ ├── shouldGrantABeerIfOldEnough.groovy
│ └── shouldRejectABeerIfTooYoung.groovy
└── mappings
└── beer-api-consumer
└── rest
├── shouldGrantABeerIfOldEnough.json
└── shouldRejectABeerIfTooYoung.json
在元步兵文件夹:
-
我们将应用按以下方式分类
组ID(例如:com.example). -
每个应用由其表示
artifactId(遗物ID(例如,Beer-api-producer-git). -
接下来,每个应用程序按其版本组织(例如:
0.0.1-快照).开始 摘自春云合约版本2.1.0,你可以指定以下版本 (假设你的版本遵循语义版本):-
+或最近的: 以查找你存根的最新版本(假设快照 总是为给定修订号下的最新文物)。这意味着:-
如果你有
1.0.0.发布,2.0.0.构建快照和2.0.0.发布,我们假设 最新的是2.0.0.构建快照. -
如果你有
1.0.0.发布和2.0.0.发布,我们假设最新的是2.0.0.发布. -
如果你有一个版本叫做
最近的或者,我们会选择那个文件夹。+
-
-
释放: 查找您作品的最新版本。这意味着:-
如果你有
1.0.0.发布,2.0.0.构建快照和2.0.0.发布我们假设 最新的是2.0.0.发布. -
如果你有一个版本叫做
释放我们选那个文件夹。
-
-
最后,有两个文件夹:
-
合同:良好的做法是将每个消费者所需的合同存储在带有消费者名称的文件夹中(例如:Beer-API-Consumer). 这样,你就可以使用每个消费者的存根数特征。 进一步的目录结构是任意的。 -
映射: Maven 或 Gradle Spring Cloud Contract 插件推送该文件夹中的存根服务器映射。在消费者端,存根运行者扫描该文件夹用存根定义启动存根服务器。文件夹结构是复制在合同子文件夹。
议定书公约
为了控制契约源的类型和位置(无论是二进制存储还是SCM仓库),你可以在仓库的URL中使用该协议。Spring Cloud Contract 会迭代注册协议解析器并尝试通过插件获取合同或从存根中获取。
关于SCM功能,目前我们支持Git仓库。要使用它,在需要放置仓库URL的属性中,你必须在连接URL前缀为git://. 以下列表展示了一些示例:
git://file:///foo/bar
git://https://github.com/spring-cloud-samples/spring-cloud-contract-nodejs-contracts-git.git
git://[email protected]:spring-cloud-samples/spring-cloud-contract-nodejs-contracts-git.git
制作人
对于生产者来说,为了使用SCM(源控管理)方法,我们可以重用与外部合同相同的机制。我们将Spring Cloud Contract路由到以 这git://协议。
你必须手动添加pushStubsToScm目标在Maven中或使用(绑定)pushStubsToScm任务在Gradle。我们不向起源你那混蛋 存储 库。 |
以下列表包含了Maven和Gradle构建文件的相关部分:
- 梅文
-
<plugin> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-contract-maven-plugin</artifactId> <version>${spring-cloud-contract.version}</version> <extensions>true</extensions> <configuration> <!-- Base class mappings etc. --> <!-- We want to pick contracts from a Git repository --> <contractsRepositoryUrl>git://https://github.com/spring-cloud-samples/spring-cloud-contract-nodejs-contracts-git.git</contractsRepositoryUrl> <!-- We reuse the contract dependency section to set up the path to the folder that contains the contract definitions. In our case the path will be /groupId/artifactId/version/contracts --> <contractDependency> <groupId>${project.groupId}</groupId> <artifactId>${project.artifactId}</artifactId> <version>${project.version}</version> </contractDependency> <!-- The contracts mode can't be classpath --> <contractsMode>REMOTE</contractsMode> </configuration> <executions> <execution> <phase>package</phase> <goals> <!-- By default we will not push the stubs back to SCM, you have to explicitly add it as a goal --> <goal>pushStubsToScm</goal> </goals> </execution> </executions> </plugin> - 格拉德勒
-
contracts { // We want to pick contracts from a Git repository contractDependency { stringNotation = "${project.group}:${project.name}:${project.version}" } /* We reuse the contract dependency section to set up the path to the folder that contains the contract definitions. In our case the path will be /groupId/artifactId/version/contracts */ contractRepository { repositoryUrl = "git://https://github.com/spring-cloud-samples/spring-cloud-contract-nodejs-contracts-git.git" } // The mode can't be classpath contractsMode = "REMOTE" // Base class mappings etc. } /* In this scenario we want to publish stubs to SCM whenever the `publish` task is invoked */ publish.dependsOn("publishStubsToScm")
你还可以进一步定制发表小作品至Scm。Gradle 任务。在以下示例中,该任务被自定义为从本地 git 仓库中选择合同:
publishStubsToScm {
// We want to modify the default set up of the plugin when publish stubs to scm is called
// We want to pick contracts from a Git repository
contractDependency {
stringNotation = "${project.group}:${project.name}:${project.version}"
}
/*
We reuse the contract dependency section to set up the path
to the folder that contains the contract definitions. In our case the
path will be /groupId/artifactId/version/contracts
*/
contractRepository {
repositoryUrl = "git://file://${new File(project.rootDir, "../target")}/contract_empty_git/"
}
// We set the contracts mode to `LOCAL`
contractsMode = "LOCAL"
}
- 重要
-
从以下
2.3.0.发布这自定义{}此前用于发表小作品至Scm。自定义功能已不再可行。设置应直接应用在发表小作品至Scm。闭包,如前例所示。
采用这样的设置:
-
git 项目被克隆到临时目录中
-
SCM存根下载器会连接到
META-INF/groupId/artifactId/version/contracts文件夹 以寻找契约。例如,对于com.example:foo:1.0.0,路径为META-INF/com.example/foo/1.0.0/contracts. -
测试是从合同中生成的。
-
合同中会生成存根。
-
测试通过后,存根会提交到克隆的仓库中。
-
最后,会向该仓库发送推送
起源.
本地存储合同的制作人
另一种选择是将SCM作为存根和合同的目的地本地存储合同,交给生产者,只将合同和存根推送到SCM。以下项目展示了实现此目标所需的设置,适用于Maven和Gradle。
采用这样的设置:
-
违约合同
SRC/测试/资源/合同目录被选中。 -
测试是从合同中生成的。
-
合同中会生成存根。
-
测试通过后:
-
git 项目被克隆到一个临时目录中。
-
存根和合同会提交到克隆的仓库中。
-
-
最后,会对该仓库的
起源.
将与制片方的合同和存根保存在外部仓库中
你也可以把合同保留在生产者仓库里,但存根放在外部git仓库里。这在你想使用基础的消费者-生产者协作流程但不能使用工件仓库来存储存根时最有用。
为此,使用通常的制作人设置,然后添加pushStubsToScm目标与设定contractsRepositoryUrl去你想存放存根的仓库。
消费者
在消费者方面,当通过repositoryRoot参数 无论是来自@AutoConfigureStubRunner注释、JUnit 4 规则、JUnit 5 扩展或属性,你可以传递SCM 仓库的 URL 前缀git://协议。 以下示例展示了如何实现:
@AutoConfigureStubRunner(
stubsMode="REMOTE",
repositoryRoot="git://https://github.com/spring-cloud-samples/spring-cloud-contract-nodejs-contracts-git.git",
ids="com.example:bookstore:0.0.1.RELEASE"
)
采用这样的设置:
-
git 项目被克隆到一个临时目录中。
-
SCM存根下载器会连接到
META-INF/groupId/artifactId/version/文件夹 以查找存根定义和契约。例如,对于com.example:foo:1.0.0,路径为META-INF/com.example/foo/1.0.0/. -
存根服务器会启动并输入映射。
-
消息定义会被阅读并在消息测试中使用。