Skip to content

Example with external contracts and contractsMode CLASSPATH #107

@robeatoz

Description

@robeatoz

Preamble

My setup contains the following modules:

Module name Corresponding module in the samples Description
my-contracts beer_contracts Module with contracts.
my-producer producer_with_external_contracts Producer of the API.
my-consumer - Consumer of the API.

I wanted to execute the Spring Cloud Contract tests during the build with contractsMode CLASSPATH.
For that reason, I created an aggregator containing these three modules und executed mvn clean test.

In the samples, the beer_contracts module with external contracts does not provide the artifact with the classifier stubs.
I choose another approach for my project.
The module my-contracts does provide the artifact with the classifier stubs.
The modules my-consumer and my-producer do not know each other but the API and the module my-contracts.

Issue

To demonstrate how Spring Cloud Contract works with external contracts, the samples contain already the modules beer_contracts and producer_with_external_contracts.
This works with contractsMode LOCAL but not with contractsMode CLASSPATH.

In my project this has a couple of reasons:

After setting contractsMode to CLASSPATH in my-producer the following problems occur:

  1. No stubs were found on classpath for [com.example:beer-contracts]

    I had to add the dependency of beer-contracts to the plugin dependencies in producer_with_external_contracts like this:

    <plugin>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-contract-maven-plugin</artifactId>
      <version>${spring-cloud-contract.version}</version>
      <extensions>true</extensions>
      <configuration>
          ...
        <contractDependency>
          <groupId>de.demo</groupId>
          <artifactId>my-contracts</artifactId>
          <classifier>stubs</classifier>
        </contractDependency>
        <contractsMode>CLASSPATH</contractsMode>
          ...
      </configuration>
    
      <dependencies>
        <dependency>
          <groupId>de.demo</groupId>
          <artifactId>my-contracts</artifactId>
          <version>0.0.1.BUILD-SNAPSHOT</version>
          <classifier>stubs</classifier>
        </dependency>
      </dependencies>
    </plugin>

    The next error message is the following:

  2. [ERROR] Unresolveable build extension: Plugin org.springframework.cloud:spring-cloud-contract-maven-plugin:2.1.3.BUILD-SNAPSHOT or one of its dependencies could not be resolved

    Normally Maven will resolve the dependencies of modules and plugins during the build.
    The problem here is the <extensions>true</extensions> tag.
    I guess that dependencies of extensions have to be available at the time the build starts and can not be resolved during the build.
    To fix it, I just had to include the spring-cloud-contract-maven-plugin to the extensions and remove the <extensions>true</extensions> tag (or set it to false):

    <build>
      <extensions>
        <extension>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-contract-maven-plugin</artifactId>
          <version>${spring-cloud-contract.version}</version>
        </extension>
      </extensions>
    
      <plugins>
          ...
        <plugin>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-contract-maven-plugin</artifactId>
          <version>${spring-cloud-contract.version}</version>
          <extensions>false</extensions>
          <configuration>
              ...
            <contractsMode>CLASSPATH</contractsMode>
          </configuration>
          <dependencies>
            <dependency>
              <groupId>de.demo</groupId>
              <artifactId>my-contracts</artifactId>
              <version>0.0.1.BUILD-SNAPSHOT</version>
              <classifier>stubs</classifier>
            </dependency>
          </dependencies>
        </plugin>
      </plugins>
    </build>

After these fixes, the build and all Spring Cloud Contract tests run properly.
Now I have the following questions:

  • Is my approach with my-contracts and artifact with classifier stubs used in consumer and producer by Spring Cloud Contracts intended?

  • How would a setup look with given modules beer_contracts and producer_with_external_contracts with contractsMode CLASSPATH?
    Just adapting my fixes does not work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions