此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 spring-cloud-contract 4.3.0! |
如何使用 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
在META-INF
文件夹:
-
我们按以下方式对应用程序进行分组
groupId
(例如com.example
). -
每个应用程序都由其
artifactId
(例如,beer-api-producer-git
). -
接下来,每个应用程序按其版本组织(例如
0.0.1-SNAPSHOT
).开始 从 Spring Cloud Contract 版本2.1.0
,您可以按如下方式指定版本 (假设您的版本遵循语义版本控制):-
+
或latest
:查找存根的最新版本(假设快照 始终是给定修订号的最新工件)。这意味着:-
如果您有
1.0.0.RELEASE
,2.0.0.BUILD-SNAPSHOT
和2.0.0.RELEASE
,我们假设 最新的是2.0.0.BUILD-SNAPSHOT
. -
如果您有
1.0.0.RELEASE
和2.0.0.RELEASE
,我们假设最新的是2.0.0.RELEASE
. -
如果你有一个名为
latest
或者,我们将选择该文件夹。+
-
-
release
:查找存根的最新版本。这意味着:-
如果您有
1.0.0.RELEASE
,2.0.0.BUILD-SNAPSHOT
和2.0.0.RELEASE
我们假设 最新的是2.0.0.RELEASE
. -
如果你有一个名为
release
,我们选择该文件夹。
-
-
最后,有两个文件夹:
-
contracts
:好的做法是存储每个人所需的合同 consumer 在具有消费者名称的文件夹中(例如beer-api-consumer
).这样,您 可以使用stubs-per-consumer
特征。进一步的目录结构是任意的。 -
mappings
:Maven 或 Gradle Spring Cloud Contract 插件推送 此文件夹中的存根服务器映射。在消费者端,Stub Runner 扫描此文件夹 以启动具有存根定义的存根服务器。文件夹结构是副本 在contracts
子文件夹。
议定书公约
控制合同来源的类型和位置(是否 二进制存储或 SCM 存储库),您可以在 存储库。Spring Cloud Contract 迭代已注册的协议解析器 并尝试获取合约(通过使用插件)或存根(从 Stub Runner)。
对于 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 task 中的
Gradle。我们不会将存根推送到origin 你的 git
存储 库。 |
以下列表包括 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>
- Gradle
-
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")
您还可以进一步自定义publishStubsToScm
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.RELEASE
这customize{}
闭包之前用于publishStubsToScm
不再提供定制。应直接应用设置 在publishStubsToScm
闭包,如前面的示例所示。
使用这样的设置:
-
将 git 项目克隆到临时目录
-
SCM 存根下载器转到
META-INF/groupId/artifactId/version/contracts
文件夹 查找合同。例如,对于com.example:foo:1.0.0
,路径将是META-INF/com.example/foo/1.0.0/contracts
. -
测试是从合约生成的。
-
存根是根据合同创建的。
-
测试通过后,存根将提交到克隆的存储库中。
-
最后,将推送发送到该存储库的
origin
.
将合约存储在本地的生产者
使用 SCM 作为存根和合同目标的另一种选择是将 在本地与生产者签订合同,并且仅将合同和存根推送到 SCM。 以下项目显示了使用 Maven 和 Gradle 实现此目的所需的设置。
使用这样的设置:
-
默认合同
src/test/resources/contracts
目录被选中。 -
测试是从合约生成的。
-
存根是根据合同创建的。
-
测试通过后:
-
git 项目被克隆到临时目录。
-
存根和协定在克隆的存储库中提交。
-
-
最后,将推送到该存储库的
origin
.
将与生产者的合同和存根保存在外部存储库中
您也可以将合约保留在生产者存储库中,但将存根保留在外部 git 存储库中。 当您想要使用基本使用者-生产者协作流但不能使用时,这最有用 使用工件存储库来存储存根。
为此,请使用通常的生产者设置,然后将pushStubsToScm
目标和设定contractsRepositoryUrl
到要保存存根的存储库。
消费者
在消费者端,当传递repositoryRoot
参数
从@AutoConfigureStubRunner
注释,则
JUnit 4 规则、JUnit 5 扩展或属性,您可以传递
SCM 存储库,前缀为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/
. -
存根服务器启动并输入映射。
-
在消息传递测试中读取和使用消息传递定义。