提问者:小点点

如何使用Spring Cloud合约存根运行器运行本地合约


我已经定义了一些Spring Cloud合约,我已经使用gradle verfierStubsJar将它们打包到一个jar中。我想使用Spring Cloud合约存根运行器jar运行jar中的存根。我不想jar存根发布到像艺术家工厂或本地Maven存储库这样的工件存储库。我只想运行存根。如何将包含存根的jar的位置传递给存根运行器jar?


共3个答案

匿名用户

如果您使用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 verfierStubsJar

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中的前导斜杠。例如存根://file://User/me/my-dates-project/build/stubs

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

  • 匿名用户

    如果您阅读文档,您会发现本节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合约插件中设置存储库根参数时,使用stubs://协议就足够了。

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

    @自动配置StubRunner(stusMode=StubRunnerProperties. StubsMode.REMOTE,repositoryRoot="stubs://file://place/to/the/生产者/target/stubs/",ids="com.example:shath-生产者")

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

    @自动配置StubRunner(stusMode=StubRunnerProperties. StubsMode.REMOTE,repositoryRoot="stubs://file://place/to/the/合同/目录",ids="com.example:shath-生产者",properties="stts.find-生产者=true")

    匿名用户

    经过大量的寻找和挣扎,这对我有用。

    将其添加到生产者端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