跳到主要内容

如何构建 Spring Cloud Contract

DeepSeek V3 中英对照 How to Build Spring Cloud Contract

在 Windows 上克隆仓库​

在 Windows 系统上克隆此项目时,Git 仓库中的某些文件可能会超过 Windows 的最大文件路径限制(255 个字符),这可能导致仓库被错误地(可能是部分地)检出。

要解决这个问题,你可以将 core.longPaths 属性设置为 true 或者克隆 Spring Cloud Contract 仓库。

要将 core.longPaths 属性设置为 true,你有三种选择:

  • 为机器的所有用户更改此设置(执行此操作需要管理员权限):
git config --system core.longPaths true
git clone https://github.com/spring-cloud/spring-cloud-contract.git
  • 为当前用户更改(无需管理员权限):
git config --global core.longPaths true
git clone https://github.com/spring-cloud/spring-cloud-contract.git
  • 仅针对此仓库进行更改(管理权限取决于仓库克隆到的位置):
git clone -c core.longPaths=true https://github.com/spring-cloud/spring-cloud-contract.git
important

你需要为你的IDE安装所有必要的Groovy插件,以便正确解析源代码。例如,在IntelliJ IDEA中,同时安装Eclipse Groovy Compiler Plugin和GMavenPlus Intellij Plugin可以确保项目被正确导入。

important

Spring Cloud Contract 会构建 Docker 镜像。请确保已安装 Docker。

important

如果你想在离线模式下运行构建,必须安装 Maven 3.5.2+。

项目结构​

以下清单展示了 Spring Cloud Contract 的文件夹结构:

├── config
├── docker
├── samples
├── scripts
├── specs
├── spring-cloud-contract-dependencies
├── spring-cloud-contract-shade
├── spring-cloud-contract-starters
├── spring-cloud-contract-stub-runner
├── spring-cloud-contract-stub-runner-boot
├── spring-cloud-contract-tools
├── spring-cloud-contract-verifier
├── spring-cloud-contract-wiremock
└── tests

以下列表描述了项目结构中的每个顶级文件夹:

  • config:文件夹包含 Spring Cloud Release Tools 自动化发布流程的设置

  • docker:文件夹包含 Docker 镜像

  • scripts:包含用于使用 Maven、Gradle 构建和测试 Spring Cloud Contract 的脚本

  • specs:包含 Contract DSL 的规范

  • spring-cloud-contract-dependencies:包含 Spring Cloud Contract BOM

  • spring-cloud-contract-shade:插件使用的 Shaded 依赖项

  • spring-cloud-contract-starters:包含 Spring Cloud Contract Starters

  • spring-cloud-contract-spec:包含规范模块(包含 Contract 的概念)

  • spring-cloud-contract-stub-runner:包含 Stub Runner 相关模块

  • spring-cloud-contract-stub-runner-boot:包含 Stub Runner Boot 应用

  • spring-cloud-contract-tools:用于 Spring Cloud Contract Verifier 的 Gradle 和 Maven 插件

  • spring-cloud-contract-verifier:Spring Cloud Contract Verifier 功能的核心

  • spring-cloud-contract-wiremock:所有 WireMock 相关功能

  • tests:针对不同消息传递技术的集成测试

命令​

要构建核心功能以及 Maven 插件,你可以运行以下命令:

./mvnw clean install -P integration

调用该函数会构建核心模块、Maven插件以及Gradle插件。

要仅构建 Gradle 插件,你可以运行以下命令:

cd spring-cloud-contract-tools/spring-cloud-contract-gradle-plugin
./gradlew clean build

实用脚本​

我们提供了一些有用的脚本来构建项目。

要并行构建项目(默认使用四个核心,但你可以更改),请运行以下命令:

./scripts/parallelBuild.sh

要使用八个核心,请运行以下命令:

CORES=8 ./scripts/parallelBuild.sh

要构建项目但不运行任何集成测试(默认情况下,此操作使用一个核心),请运行以下命令:

./scripts/noIntegration.sh

要使用八个核心,请运行以下命令:

CORES=8 ./scripts/noIntegration.sh

要生成文档(包括根项目和 Maven 插件),请运行以下命令:

./scripts/generateDocs.sh