存儲(chǔ)合同以外的另一種方法是將它們保存在一個(gè)共同的地方。它可能與消費(fèi)者無法克隆生產(chǎn)者代碼的安全問題相關(guān)。另外,如果您在一個(gè)地方保留合約,那么作為生產(chǎn)者,您將知道有多少消費(fèi)者,以及您的本地變更會(huì)消費(fèi)哪些消費(fèi)者。
回購(gòu)結(jié)構(gòu)
假設(shè)我們有一個(gè)坐標(biāo)為com.example:server和3個(gè)消費(fèi)者的生產(chǎn)者:client1,client2,client3。然后在具有常見合同的存儲(chǔ)庫(kù)中,您將具有以下設(shè)置(您可以在此處查看:
├── com
│?? └── example
│??? ? └── server
│??? ? ? ? ├── client1
│??? ? ? ? │?? └── expectation.groovy
│??? ? ? ? ├── client2
│??? ? ? ? │?? └── expectation.groovy
│??? ? ? ? ├── client3
│??? ? ? ? │?? └── expectation.groovy
│??? ? ? ? └── pom.xml
├── mvnw
├── mvnw.cmd
├── pom.xml
└── src
? ? └── assembly
? ? ? ? └── contracts.xml
您可以看到下面的斜線分隔的groupid/工件id文件夾(com/example/server),您對(duì)3個(gè)消費(fèi)者(client1,client2和client3)有期望。期望是本文檔中描述的標(biāo)準(zhǔn)Groovy DSL合同文件。該存儲(chǔ)庫(kù)必須生成一個(gè)將一對(duì)一映射到回收內(nèi)容的JAR文件。
server文件夾內(nèi)的pom.xml示例。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Server Stubs</name>
<description>POM used to install locally stubs for consumer side</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.0.BUILD-SNAPSHOT</version>
<relativePath />
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<spring-cloud-contract.version>1.1.0.BUILD-SNAPSHOT</spring-cloud-contract.version>
<spring-cloud-dependencies.version>Dalston.BUILD-SNAPSHOT</spring-cloud-dependencies.version>
<excludeBuildFolders>true</excludeBuildFolders>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud-dependencies.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-contract-maven-plugin</artifactId>
<version>${spring-cloud-contract.version}</version>
<extensions>true</extensions>
<configuration>
<!-- By default it would search under src/test/resources/ -->
<contractsDirectory>${project.basedir}</contractsDirectory>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/release</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/release</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
你可以看到除了Spring Cloud Contract Maven插件之外沒有依賴關(guān)系。這些垃圾是消費(fèi)者運(yùn)行mvn clean install -DskipTests來本地安裝生產(chǎn)者項(xiàng)目的存根的必要條件。
根文件夾中的pom.xml可以如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.standalone</groupId>
<artifactId>contracts</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Contracts</name>
<description>Contains all the Spring Cloud Contracts, well, contracts. JAR used by the producers to generate tests and stubs</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>contracts</id>
<phase>prepare-package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<attach>true</attach>
<descriptor>${basedir}/src/assembly/contracts.xml</descriptor>
<!-- If you want an explicit classifier remove the following line -->
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
它正在使用程序集插件來構(gòu)建所有合同的JAR。此類設(shè)置的示例如下:
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
? xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>project</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.basedir}</directory>
<outputDirectory>/</outputDirectory>
<useDefaultExcludes>true</useDefaultExcludes>
<excludes>
<exclude>**/${project.build.directory}/**</exclude>
<exclude>mvnw</exclude>
<exclude>mvnw.cmd</exclude>
<exclude>.mvn/**</exclude>
<exclude>src/**</exclude>
</excludes>
</fileSet>
</fileSets>
</assembly>
工作流程
工作流程將與Step by step guide to CDC中提供的工作流程類似。唯一的區(qū)別是生產(chǎn)者不再擁有合同。所以消費(fèi)者和生產(chǎn)者必須在共同的倉(cāng)庫(kù)中處理共同的合同。
消費(fèi)者
當(dāng)消費(fèi)者希望脫機(jī)工作,而不是克隆生產(chǎn)者代碼時(shí),消費(fèi)者團(tuán)隊(duì)克隆了公用存儲(chǔ)庫(kù),轉(zhuǎn)到所需的生產(chǎn)者的文件夾(例如com/example/server),并運(yùn)行mvn clean install -DskipTests在本地安裝存根從合同轉(zhuǎn)換。