SpringCloud 生產(chǎn)者

2023-12-07 16:42 更新

對(duì)于生產(chǎn)者,要使用SCM方法,我們可以重用與外部合同相同的機(jī)制。我們通過(guò)包含git://協(xié)議的URL路由Spring Cloud Contract以使用SCM實(shí)現(xiàn)。

 您必須在Maven中手動(dòng)添加pushStubsToScm目標(biāo),或者在Gradle中執(zhí)行(綁定)pushStubsToScm任務(wù)。我們不會(huì)開(kāi)箱即用將存根推送到您的git存儲(chǔ)庫(kù)的origin。

Maven. 

<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 executed
*/
publish.dependsOn("publishStubsToScm")

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

  • Git項(xiàng)目將被克隆到一個(gè)臨時(shí)目錄
  • SCM存根下載器將轉(zhuǎn)到META-INF/groupId/artifactId/version/contracts文件夾以查找合同。例如,對(duì)于com.example:foo:1.0.0,路徑為META-INF/com.example/foo/1.0.0/contracts
  • 將根據(jù)合同生成測(cè)試
  • 將根據(jù)合同創(chuàng)建存根
  • 測(cè)試通過(guò)后,存根將在克隆的存儲(chǔ)庫(kù)中提交
  • 最后,將推動(dòng)該存儲(chǔ)庫(kù)的origin
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)