SpringCloud 生产者,合同存储在本地
2023-12-07 16:45 更新
使用SCM作为存根和合同目的地的另一种选择是与生产者一起在本地存储合同,并且仅将合同和存根推送到SCM。在下面,您可以找到使用Maven和Gradle完成此操作所需的设置。
Maven.
<plugin> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-contract-maven-plugin</artifactId> <version>${spring-cloud-contract.version}</version> <extensions>true</extensions> <!-- In the default configuration, we want to use the contracts stored locally --> <configuration> <baseClassMappings> <baseClassMapping> <contractPackageRegex>.*messaging.*</contractPackageRegex> <baseClassFQN>com.example.BeerMessagingBase</baseClassFQN> </baseClassMapping> <baseClassMapping> <contractPackageRegex>.*rest.*</contractPackageRegex> <baseClassFQN>com.example.BeerRestBase</baseClassFQN> </baseClassMapping> </baseClassMappings> <basePackageForTests>com.example</basePackageForTests> </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> <configuration> <!-- We want to pick contracts from a Git repository --> <contractsRepositoryUrl>git://file://${env.ROOT}/target/contract_empty_git/ </contractsRepositoryUrl> <!-- Example of URL via git protocol --> <!--<contractsRepositoryUrl>git://git@github.com:spring-cloud-samples/spring-cloud-contract-samples.git</contractsRepositoryUrl>--> <!-- Example of URL via http protocol --> <!--<contractsRepositoryUrl>git://https://github.com/spring-cloud-samples/spring-cloud-contract-samples.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 mode can't be classpath --> <contractsMode>LOCAL</contractsMode> </configuration> </execution> </executions> </plugin>
Gradle.
contracts { // Base package for generated tests basePackageForTests = "com.example" baseClassMappings { baseClassMapping(".*messaging.*", "com.example.BeerMessagingBase") baseClassMapping(".*rest.*", "com.example.BeerRestBase") } } /* In this scenario we want to publish stubs to SCM whenever the `publish` task is executed */ publishStubsToScm { // We want to modify the default set up of the plugin when publish stubs to scm is called customize { // 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://${System.getenv("ROOT")}/target/contract_empty_git/" } // The mode can't be classpath contractsMode = "LOCAL" } } publish.dependsOn("publishStubsToScm") publishToMavenLocal.dependsOn("publishStubsToScm")
通过这样的设置:
- 从默认的
src/test/resources/contracts
目录中选择Contracts - 将根据合同生成测试
- 将根据合同创建存根
一旦测试通过
- Git项目将被克隆到一个临时目录
- 存根和合同将在克隆的存储库中提交
- 最后,将推动该存储库的
origin
以上内容是否对您有帮助:
更多建议: