【问题标题】:How to run local contracts with Spring Cloud Contract stub runner如何使用 Spring Cloud Contract stub runner 运行本地合约
【发布时间】:2020-01-16 16:41:17
【问题描述】:

我已经使用gradle verifierStubsJar 定义了一些已打包到 jar 中的 Spring Cloud Contracts。我想用Spring Cloud Contract stub runner jar 运行那个jar 中的存根。我不想将存根 jar 发布到像 Artifactory 这样的工件存储库或本地 Maven 存储库。我只想运行存根。如何将包含我的存根的 jar 的位置传递给存根运行器 jar?

【问题讨论】:

    标签: spring-cloud-contract


    【解决方案1】:

    如果你使用gradle generateClientStubs

    curl -Lo stub-runner.jar https://search.maven.org/remotecontent?filepath=org/springframework/cloud/spring-cloud-contract-stub-runner-boot/2.2.1.RELEASE/spring-cloud-contract-stub-runner-boot-2.2.1.RELEASE.jar
    
    java -Djava.security-egd=/dev/./urandom -Dstubrunner.repositoryRoot=stubs://file://absolute/path/to/build/stubs -Dstubrunner.ids=com.company-contract:stubs:8081 -Dstubrunner.stubs-mode=REMOTE -jar stub-runner.jar
    

    如果你使用gradle verifierStubsJar

    curl -Lo stub-runner.jar https://search.maven.org/remotecontent?filepath=org/springframework/cloud/spring-cloud-contract-stub-runner-boot/2.2.1.RELEASE/spring-cloud-contract-stub-runner-boot-2.2.1.RELEASE.jar
    
    java -cp stub-runner.jar:build/libs/my-contract-stubs.jar -Djava.security-egd=/dev/./urandom -Dstubrunner.ids=com.company:my-contract:stubs:8081 -Dstubrunner.stubs-mode=CLASSPATH org.springframework.boot.loader.JarLauncher
    

    陷阱:

    • 使用gradle generateClientStubs 时,不要忘记存根目录的绝对URI 中的前导斜杠。例如stubs://file:///User/me/my-contracts-project/build/stubs

    • 存根运行程序在端口 8080 上启动服务器,因此您必须在不同的端口上运行存根。您可以使用通常的-Dserver.port 覆盖存根运行程序启动的端口。

    【讨论】:

      【解决方案2】:

      如果您阅读文档,您会发现此部分 https://cloud.spring.io/spring-cloud-contract/reference/html/project-features.html#features-stub-runner-stubs-protocol

      与其从 Artifactory / Nexus 或 Git 中选择存根或合约定义,不如只指向驱动器或类路径上的位置。这在多模块项目中特别有用,其中一个模块想要重用来自另一个模块的存根或合约,而无需将它们实际安装在本地 maven 存储库中,也不需要将这些更改提交给 Git。

      为了实现这一点,当在 Stub Runner 或 Spring Cloud Contract 插件中设置存储库根参数时,使用 stubs:// 协议就足够了。

      在此示例中,生产者项目已成功构建,并且在 target/stubs 文件夹下生成了存根。作为消费者,可以设置 Stub Runner 以使用 stubs:// 协议从该位置选择存根。

      @AutoConfigureStubRunner( stubsMode = StubRunnerProperties.StubsMode.REMOTE, repositoryRoot = "stubs://file://location/to/the/producer/target/stubs/", ids = "com.example:some-producer")

      合同和存根可以存储在一个位置,每个生产者都有自己的专用文件夹,用于合同和存根映射。在该文件夹下,每个消费者都可以拥有自己的设置。要使 Stub Runner 从提供的 id 中找到专用文件夹,可以传递属性 stubs.find-producer=true 或系统属性 stubrunner.stubs.find-producer=true 。

      @AutoConfigureStubRunner( stubsMode = StubRunnerProperties.StubsMode.REMOTE, repositoryRoot = "stubs://file://location/to/the/contracts/directory", ids = "com.example:some-producer", properties="stubs.find-producer=true")

      【讨论】:

      • 如何使用存根运行器 jar 执行此操作?我的合同位于他们自己的位置,与消费者和生产者分开。在这种情况下,我没有使用 JUnit 来运行存根。我正在尝试做类似的事情:java -Dstubrunner.ids=com.myname:my-contracts:8080:stubs -Dstubrunner.stubs-mode=CLASSPATH -jar stub-runner.jar。这里的问题是包含我的存根的 jar 实际上不在类路径上。 -cp 标志不适用于 -jar
      • 我正在尝试做类似于this tutorial在消费者端使用 Spring Cloud Contract Stubs 部分的操作。但不是从 Artifactory 采购存根,我想从本地 jar 采购它们。
      • 请阅读文档。您可以重用注释中的属性作为您的 fat jar 设置
      • 我整个上午都在阅读文档。我还没有找到符合我要求的房产。 stubrunner.repositoryRoot 不适用,因为我没有远程 Maven 存储库。我无法将 stubrunner.stubsMode 设置为 LOCAL,因为我使用的是 Gradle,而不是 Maven,因此我没有本地 Maven 存储库。我正在寻找类似stubrunner.stubsJar=/usr/src/stubs.jar 的东西。
      【解决方案3】:

      经过大量搜索和挣扎,这对我有用。

      将此添加到生产者端build.gradle

      configurations {
          // create a configuration that other modules can use
          contractStubs {
              canBeResolved = false
              canBeConsumed = true
          }
      }
      
      artifacts {
          // link the stub jar to the configuration
          contractStubs(verifierStubsJar)
      }
      

      将此添加到消费者端build.gradle

      dependencies {
          testImplementation project(path: ':some-path:probucer-dir', configuration: 'contractStubs')
      }
      

      有关详细信息,请参阅: https://docs.gradle.org/current/userguide/cross_project_publications.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-06-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多