Search code examples
spring-boottestingcontractspring-cloud-contractaether

The artifact was found in the local repository but you have explicitly stated that it should be downloaded from a remote one"


I have consumer project, which has spring cloud contract tests verifier,which needs to talk to the stubs jar in the remote repo. The setup for stubsMode: LOCAL works fine, but for remote it throws the following error.

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cloud.contract.stubrunner.BatchStubRunner]: Factory method 'batchStubRunner' threw exception; nested exception is java.lang.IllegalStateException: The artifact was found in the local repository but you have explicitly stated that it should be downloaded from a remote one
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:622)
    ... 43 more
Caused by: java.lang.IllegalStateException: The artifact was found in the local repository but you have explicitly stated that it should be downloaded from a remote one

Consumer Side remote code:

`@RunWith(SpringRunner.class)
@SpringBootTest(classes = RestClientConfig.class,
        webEnvironment = SpringBootTest.WebEnvironment.MOCK)
@AutoConfigureStubRunner(   repositoryRoot="https://nexus.com/nexus/content/repositories/sam-releases/com/sam/api/",
        ids = "com.sam:api:+:stubs:8083",
        stubsMode = StubRunnerProperties.StubsMode.REMOTE
)

pom.xml

<spring-cloud.version>Finchley.SR2</spring-cloud.version>
    <spring-cloud-contract.version>2.0.2.RELEASE</spring-cloud-contract-version>

I need to use REMOTE repoUrl for the consumer project to talk to the producer stub jar.


Solution

  • If you read the documentation at this section https://cloud.spring.io/spring-cloud-static/Finchley.SR2/single/spring-cloud.html#_ci_server_setup (it's actually enough to find the exception you were looking for in the documentation) you'll see the following text:

    91.4 CI Server setup

    When fetching stubs / contracts in a CI, shared environment, what might happen is that both the producer and the consumer reuse the same local Maven repository. Due to this, the framework, responsible for downloading a stub JAR from remote location, can’t decide which JAR should be picked, local or remote one. That caused the "The artifact was found in the local repository but you have explicitly stated that it should be downloaded from a remote one" exception and failed the build.

    For such cases we’re introducing the property and plugin setup mechanism:

    via stubrunner.snapshot-check-skip system property
    via STUBRUNNER_SNAPSHOT_CHECK_SKIP environment variable
    

    if either of these values is set to true, then the stub downloader will not verify the origin of the downloaded JAR.

    For the plugins you need to set the contractsSnapshotCheckSkip property to true.

    Just follow the guidelines from the documentation and you won't have this problem anymore. Or bump to Greenwich release train where this issue got completely fixed.