共同回购合同

存储合同以外的另一种方法是将它们保存在一个共同的地方。它可能与消费者无法克隆生产者代码的安全问题相关。另外,如果您在一个地方保留合约,那么作为生产者,您将知道有多少消费者,以及您的本地变更会消费哪些消费者。

回购结构

假设我们有一个坐标为com.example:server和3个消费者的生产者:client1,client2,client3。然后在具有常见合同的存储库中,您将具有以下设置(您可以在此处查看

├── 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),您对3个消费者(client1,client2和client3)有期望。期望是本文档中描述的标准Groovy DSL合同文件。该存储库必须生成一个将一对一映射到回收内容的JAR文件。

server文件夹内的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插件之外没有依赖关系。这些垃圾是消费者运行mvn clean install -DskipTests来本地安装生产者项目的存根的必要条件。

根文件夹中的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>

它正在使用程序集插件来构建所有合同的JAR。此类设置的示例如下:

<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中提供的工作流程类似。唯一的区别是生产者不再拥有合同。所以消费者和生产者必须在共同的仓库中处理共同的合同。

消费者

消费者希望脱机工作,而不是克隆生产者代码时,消费者团队克隆了公用存储库,转到所需的生产者的文件夹(例如com/example/server),并运行mvn clean install -DskipTests在本地安装存根从合同转换。

提示您需要在本地安装Maven

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容