SpringCloud 生產(chǎn)者,合同存儲在本地

2023-12-07 16:45 更新

使用SCM作為存根和合同目的地的另一種選擇是與生產(chǎn)者一起在本地存儲合同,并且僅將合同和存根推送到SCM。在下面,您可以找到使用Maven和Gradle完成此操作所需的設(shè)置。

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

通過這樣的設(shè)置:

  • 從默認的src/test/resources/contracts目錄中選擇Contracts
  • 將根據(jù)合同生成測試
  • 將根據(jù)合同創(chuàng)建存根
  • 一旦測試通過

    • Git項目將被克隆到一個臨時目錄
    • 存根和合同將在克隆的存儲庫中提交
  • 最后,將推動該存儲庫的origin

與生產(chǎn)者和存根之間的合同保持一致

也可以將合同保留在生產(chǎn)者存儲庫中,但將存根保留在外部git repo中。當您想使用基本的消費者-生產(chǎn)者協(xié)作流程,但又無法使用工件存儲庫來存儲存根時,這是最有用的。

為此,請使用通常的生產(chǎn)者設(shè)置,然后添加pushStubsToScm目標并在要保留存根的存儲庫中設(shè)置contractsRepositoryUrl。

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號