From e69e43d8f47ab2f0049a1e92c91c3d7670ce3b4f Mon Sep 17 00:00:00 2001 From: Oleksandr Nikytchuk Date: Mon, 19 Feb 2018 15:21:39 +0200 Subject: [PATCH] [#500] Messaging contracts per topic (#536) --- docs/src/main/asciidoc/verifier_faq.adoc | 192 +++++++++++++++++++++++ 1 file changed, 192 insertions(+) diff --git a/docs/src/main/asciidoc/verifier_faq.adoc b/docs/src/main/asciidoc/verifier_faq.adoc index d39a5206a4..ed9804ece8 100644 --- a/docs/src/main/asciidoc/verifier_faq.adoc +++ b/docs/src/main/asciidoc/verifier_faq.adoc @@ -333,6 +333,198 @@ when some incompatible changes are done. The rest of the flow looks the same. +==== How can I define messaging contracts per topic not per producer? + +To avoid messaging contracts duplication in the common repo, when few producers writing messages to one topic, +we could create the structure when the rest contracts would be placed in a folder per producer and messaging +contracts in the folder per topic. + +===== For Maven Project + +To make it possible to work on the producer side we could do the following things (all via Maven plugins): + +- Add common repo dependency to your classpath: + +[source,xml,indent=0] +---- + + com.example + common-repo + ${common-repo.version} + +---- + +- Download the JAR with the contracts and unpack the JAR to target: + +[source,xml,indent=0] +---- + + org.apache.maven.plugins + maven-dependency-plugin + 3.0.0 + + + unpack-dependencies + process-resources + + unpack + + + + + com.example + common-repo + jar + false + ${project.build.directory}/contracts + + + + + + +---- + +- Rip out all the folders we're not interested in: + +[source,xml,indent=0] +---- + + org.apache.maven.plugins + maven-antrun-plugin + 1.8 + + + process-resources + + run + + + + + + + + + + + + + + + + + + +---- + +- Run the contract plugin by pointing to the contracts to the folder under target: + +[source,xml,indent=0] +---- + + org.springframework.cloud + spring-cloud-contract-maven-plugin + ${spring-cloud-contract.version} + true + + com.example + + + .*intoxication.* + com.example.intoxication.BeerIntoxicationBase + + + ${project.build.directory}/contracts + + +---- + +===== For Gradle Project + +- Add a custom configuration for the common-repo dependency: + +[source,groovy,indent=0] +---- +ext { + conractsGroupId = "com.example" + contractsArtifactId = "common-repo" + contractsVersion = "1.2.3" +} + +configurations { + contracts { + transitive = false + } +} +---- + +- Add the common-repo dependency to your classpath: + +[source,groovy,indent=0] +---- +dependencies { + contracts "${conractsGroupId}:${contractsArtifactId}:${contractsVersion}" + testCompile "${conractsGroupId}:${contractsArtifactId}:${contractsVersion}" +} +---- + +- Download the dependency to an appropriate folder: + +[source,groovy,indent=0] +---- +task getContracts(type: Copy) { + from configurations.contracts + into new File(project.buildDir, "downloadedContracts") +} +---- + +- Unzip JAR: + +[source,groovy,indent=0] +---- +task unzipContracts(type: Copy) { + def zipFile = new File(project.buildDir, "downloadedContracts/${contractsArtifactId}-${contractsVersion}.jar") + def outputDir = file("${buildDir}/unpackedContracts") + + from zipTree(zipFile) + into outputDir +} +---- + +- Cleanup unused contracts: + +[source,groovy,indent=0] +---- +task deleteUnwantedContracts(type: Delete) { + delete fileTree(dir: "${buildDir}/unpackedContracts", + include: "**/*", + excludes: [ + "**/${project.name}/**"", + **/${first-topic}/**", + **/${second-topic}/**]) +} +---- + +- Create task dependencies: + +[source,groovy,indent=0] +---- +unzipContracts.dependsOn("getContracts") +deleteUnwantedContracts.dependsOn("unzipContracts") +build.dependsOn("deleteUnwantedContracts") +---- + +- Configure plugin by specifying the directory containing contracts using ```contractsDslDir``` property + +[source,groovy,indent=0] +---- +contracts { + + contractsDslDir = new File("${buildDir}/unpackedContracts") +} +---- + === Can I have multiple base classes for tests? Yes! Check out the https://cloud.spring.io/spring-cloud-contract/spring-cloud-contract.html#_different_base_classes_for_contracts[Different base classes for contracts] sections