Search code examples
spring-cloud-contract

Spring Cloud contract jar versioning


I have established Spring cloud contract between two microservices in my project successfully. Everything was good until yesterday.

On consumer side, I am referencing the latest version of stubs like below:

@AutoConfigureStubRunner(ids = {"com.example:http-server-dsl:+:stubs:8080"})

But this causes problem in some cases where the producer side reverted their latest version and switched back to previous version.

Or

somehow there is a situation that the jar that contains current stubs has a lower version than the latest version in maven repo.

Is there a way in Spring cloud contract to do either one of the following?

1) configure the consumer side to pick up the current project version (referring to project version in pom.xml) instead of the latest version from maven repo?

Or

2) configure the producer side to have a static version of stubs jar but keep dynamic version of other project jars. This would allow the consumer side to refer to the same static version of stubs jar

I read the documentation https://cloud.spring.io/spring-cloud-static/spring-cloud-contract/2.1.1.RELEASE/single/spring-cloud-contract.html#_jar_versioning but it did not help


Solution

  • Following worked (for maven project).

    Step1: Instead of specifying the ids in @AutoConfigureStubRunner, we can provide it inside application.properties file like below (notice @project.version@, this refers to maven project version )

    stubrunner.ids=com.example:http-server-dsl:@project.version@:stubs:8080
    

    Step2: To be able to use @project.version@ in properties file, add the following in build section of pom.xml:

     <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <filtering>true</filtering>
                </resource>
            </resources>
    

    And in plugins section:

       <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>${mvn-resources-plugin.version}</version>
            <configuration>
                <delimiters>
                    <delimiter>@</delimiter>
                </delimiters>
                <useDefaultDelimiters>false</useDefaultDelimiters>
            </configuration>
        </plugin>