DSL Customization

This section is valid only for the Groovy DSL

You can customize the Spring Cloud Contract Verifier by extending the DSL, as shown in the remainder of this section.

Extending the DSL

You can provide your own functions to the DSL. The key requirement for this feature is to maintain the static compatibility. Later in this chapter, you can see examples of:

  • Creating a JAR with reusable classes.

  • Referencing of these classes in the DSLs.

You can find the full example here.

Common JAR

The following examples show three classes that can be reused in the DSLs.

PatternUtils contains functions used by both the consumer and the producer.

ConsumerUtils contains functions used by the consumer.

ProducerUtils contains functions used by the producer.

Adding a Test Dependency in the Project’s Dependencies

To add a test dependency in the project’s dependencies, you must first add the common jar dependency as a test dependency. Because your contracts files are available on the test resources path, the common jar classes automatically become visible in your Groovy files. The following example show how to test the dependency.

Adding a Test Dependency in the Plugin’s Dependencies

Now, you must add the dependency for the plugin to reuse at runtime.

Referencing Classes in DSLs

You can now reference your classes in your DSL, as the following example shows.

You can set the Spring Cloud Contract plugin up by setting convertToYaml to true. That way, you do NOT have to add the dependency with the extended functionality to the consumer side, since the consumer side uses YAML contracts instead of Groovy contracts.