如何使用 Git 作为合约和存根的存储?

在多语言世界中,有些语言不使用二进制存储,因为 Artifactory 和 Nexus 可以。从 Spring Cloud Contract 2.0.0 版本开始,我们提供了 将合约和存根存储在 SCM(源代码控制管理)存储库中的机制。目前, 唯一支持的 SCM 是 Git。spring-doc.cadn.net.cn

存储库必须具有以下设置 (您可以从这里结账):spring-doc.cadn.net.cn

.
└── 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文件夹:spring-doc.cadn.net.cn

  • 我们按以下方式对应用程序进行分组groupId(例如com.example).spring-doc.cadn.net.cn

  • 每个应用程序都由其artifactId(例如,beer-api-producer-git).spring-doc.cadn.net.cn

  • 接下来,每个应用程序按其版本组织(例如0.0.1-SNAPSHOT).开始 从 Spring Cloud Contract 版本2.1.0,您可以按如下方式指定版本 (假设您的版本遵循语义版本控制):spring-doc.cadn.net.cn

最后,有两个文件夹:spring-doc.cadn.net.cn

  • contracts:好的做法是存储每个人所需的合同 consumer 在具有消费者名称的文件夹中(例如beer-api-consumer).这样,您 可以使用stubs-per-consumer特征。进一步的目录结构是任意的。spring-doc.cadn.net.cn

  • mappings:Maven 或 Gradle Spring Cloud Contract 插件推送 此文件夹中的存根服务器映射。在消费者端,Stub Runner 扫描此文件夹 以启动具有存根定义的存根服务器。文件夹结构是副本 在contracts子文件夹。spring-doc.cadn.net.cn

议定书公约

控制合同来源的类型和位置(是否 二进制存储或 SCM 存储库),您可以在 存储库。Spring Cloud Contract 迭代已注册的协议解析器 并尝试获取合约(通过使用插件)或存根(从 Stub Runner)。spring-doc.cadn.net.cn

对于 SCM 功能,目前我们支持 Git 存储库。要使用它, 在需要放置存储库 URL 的属性中,您必须添加前缀 连接 URL 与git://.以下列表显示了一些示例:spring-doc.cadn.net.cn

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://协议。spring-doc.cadn.net.cn

您必须手动添加pushStubsToScm目标或使用(绑定)Maven 中的pushStubsToScmtask 中的 Gradle。我们不会将存根推送到origin你的 git 存储 库。

以下列表包括 Maven 和 Gradle 构建文件的相关部分:spring-doc.cadn.net.cn

专家
<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")

您还可以进一步自定义publishStubsToScmgradle 任务。在以下示例中, 该任务是自定义的,以从本地 Git 存储库中选取合约:spring-doc.cadn.net.cn

Gradle
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.RELEASEcustomize{}闭包之前用于publishStubsToScm不再提供定制。应直接应用设置 在publishStubsToScm闭包,如前面的示例所示。spring-doc.cadn.net.cn

使用这样的设置:spring-doc.cadn.net.cn

将合约存储在本地的生产者

使用 SCM 作为存根和合同目标的另一种选择是将 在本地与生产者签订合同,并且仅将合同和存根推送到 SCM。 以下项目显示了使用 Maven 和 Gradle 实现此目的所需的设置。spring-doc.cadn.net.cn

使用这样的设置:spring-doc.cadn.net.cn

将与生产者的合同和存根保存在外部存储库中

您也可以将合约保留在生产者存储库中,但将存根保留在外部 git 存储库中。 当您想要使用基本使用者-生产者协作流但不能使用时,这最有用 使用工件存储库来存储存根。spring-doc.cadn.net.cn

为此,请使用通常的生产者设置,然后将pushStubsToScm目标和设定contractsRepositoryUrl到要保存存根的存储库。spring-doc.cadn.net.cn

消费者

在消费者端,当传递repositoryRoot参数 从@AutoConfigureStubRunner注释,则 JUnit 4 规则、JUnit 5 扩展或属性,您可以传递 SCM 存储库,前缀为git://协议。以下示例显示了如何执行此作:spring-doc.cadn.net.cn

@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"
)

使用这样的设置:spring-doc.cadn.net.cn