Spring Cloud Contract 从 Artifactory 检索存根时出现问题

Spring Cloud Contract issues retrieving stubs from Artifactory

我对两个消费者和一个离线工作的生产者进行了测试,但是当我更改它们以从 Artifactory 检索存根时,消费者测试失败。

这是离线工作的代码:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ContractTestConfiguration.class, webEnvironment = SpringBootTest.WebEnvironment.NONE)
@AutoConfigureStubRunner(ids = {"com.mycompany:service-name:+:stubs"}, workOffline = true)
@ImportAutoConfiguration(org.springframework.cloud.stream.test.binder.TestSupportBinderAutoConfiguration.class)
@DirtiesContext
public class MyContractTest

这是在线的:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ContractTestConfiguration.class, webEnvironment = SpringBootTest.WebEnvironment.NONE)
@AutoConfigureStubRunner(ids = {"com.mycompany:service-name:+:stubs"}, repositoryRoot = "https://artifactory.companyname.com/artifactory/artifacts-snapshot-local")
@ImportAutoConfiguration(org.springframework.cloud.stream.test.binder.TestSupportBinderAutoConfiguration.class)
@DirtiesContext
public class MyContractTest {

我收到这个错误:

Exception occurred while trying to download a stub for group [com.mycompany] module [service-name] and classifier [stubs] in [remote0 (https://artifactory.mycompany.com/artifactory/artifacts-snapshot-local, default, releases+snapshots)]
org.eclipse.aether.resolution.ArtifactResolutionException: Could not find artifact com.mycompany.domain:service-name:jar:stubs:1.6.0-SNAPSHOT

我查看了 Artifactory 和 https://artifactory.mycompany.com/artifactory/artifacts-snapshot-local,存根 jar 出现在那里。我已经完成了生产者的 mvn 安装,当我再次 运行 测试时,我得到了这个错误 "The artifact was found in the local repository but you have explicitly stated that it should be downloaded from a remote one".

我也尝试过向消费者添加对生产者存根的依赖,但我遇到了类似的错误。而且我宁愿避免它,因为它会添加对生产者特定版本的依赖:

<dependency>
    <groupId>com.companyname</groupId>
    <artifactId>service-name</artifactId>
    <classifier>stubs</classifier>
    <version>1.6.0-SNAPSHOT</version>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>*</groupId>
            <artifactId>*</artifactId>
        </exclusion>
    </exclusions>
</dependency>

我已将此添加到生产者的 POM 文件中:

<spring.cloud.contract.verifier.skip>true</spring.cloud.contract.verifier.skip>

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
        <execution>
            <id>stub</id>
            <goals>
                <goal>single</goal>
            </goals>
            <phase>prepare-package</phase>
            <inherited>false</inherited>
            <configuration>
                <attach>true</attach>
                <descriptor>${basedir}/src/assembly/stub.xml</descriptor>
            </configuration>
        </execution>
    </executions>
</plugin>

这是 src/assembly 下的文件 stub.xml 的内容:

<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>stubs</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>src/main/java</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>**com/companyname/projectname/*.*</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>${project.build.directory}/classes</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>**com/companyname/projectname/*.*</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>${project.build.directory}/snippets/stubs</directory>
            <outputDirectory>META-INF/${project.groupId}/${project.artifactId}/${project.version}/mappings</outputDirectory>
            <includes>
                <include>**/*</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>${basedir}/src/test/resources/contracts</directory>
            <outputDirectory>META-INF/${project.groupId}/${project.artifactId}/${project.version}/contracts</outputDirectory>
            <includes>
                <include>**/*.groovy</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

知道我错过了什么吗?提前致谢

I have looked in Artifactory and in https://artifactory.mycompany.com/artifactory/artifacts-snapshot-local and the stubs jar appears there. I have done a mvn install of the producer and when I run the tests again I get this error "The artifact was found in the local repository but you have explicitly stated that it should be downloaded from a remote one".

当您在本地安装存根,然后尝试从 artifactory 下载它时会发生这种情况,但 SHA 不同,因此 Aether(下载存根的引擎)会选择本地的。在这种情况下,我们抛出一个异常,因为您想从远程位置下载存根,而不是从本地下载存根。

Exception occurred while trying to download a stub for group [com.mycompany] module [service-name] and classifier [stubs] in [remote0 (https://artifactory.mycompany.com/artifactory/artifacts-snapshot-local, default, releases+snapshots)] org.eclipse.aether.resolution.ArtifactResolutionException: Could not find artifact com.mycompany.domain:service-name:jar:stubs:1.6.0-SNAPSHOT

这看起来像在 Artifactory 中,您在一些 Maven 元数据中有条目,最新的 jar 是 1.6.0-SNAPSHOT,但 JAR 不再存在。你能仔细检查它是否真的存在吗?

I have also tried adding to the consumers a dependency on the stubs of the producer but I get similar errors. And I would prefer to avoid it because it would add a dependency with the specific version of the producer:

这只能证明您的工件/项目设置有问题。如果您对版本进行硬编码,是否仍然无法正常工作?

更新:

如果您的人工制品实例需要凭据或在代理后面,您可以使用这些值:

https://github.com/spring-cloud/spring-cloud-contract/blob/v1.1.4.RELEASE/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/spring/StubRunnerProperties.java#L72-L87

您可以提供 stubrunner.usernamestubrunner.passwordstubrunner.proxyHoststubrunner.proxyPort

stubrunner.usernamestubrunner.password可以在@AutoConfigureStubRunner注解中提供吗?我尝试了以下方法:

 @RunWith(SpringRunner.class)
 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
 @AutoConfigureStubRunner(ids = "com.mycompany.myproj:myservice:+:stubs:8100",
    repositoryRoot = "http://artifactory.mycompany.com/artifactory/libs-snapshot-local",
    properties = {"stubrunner.username=myusername", "stubrunner.password=mypassword"},
    stubsMode = StubRunnerProperties.StubsMode.REMOTE)
 @DirtiesContext
 class MyApiContractsVerificationTest {
     ...
 }

凭据正确,存根已正确生成并部署到远程 Artifactory 存储库中。如果我将测试 运行 配置为查看本地 .m2/repository(删除 "repositoryRoot and "properties" 注释 args),测试 运行 很好,但是使用上述配置我得到以下错误:

Could not find metadata com.mycommany.myproject:myservice/maven-metadata.xml in local (/Users/myname/.m2/repository), 
org.eclipse.aether.transfer.MetadataTransferException: Could not transfer metadata com.mycommany.myproject:myservice/maven-metadata.xml from/to remote0 (http://artifactory.mycompany.com/artifactory/libs-snapshot-local): **Unauthorized (401)**]
...

在启用远程模式的情况下 运行 测试之前,我会清理存根的本地 .m2/repository,因此没有冲突。

我提供的用户名和密码有误吗?配置中是否有错误或缺失?