Simplifying Kafka Avro DTO Generation with a Maven Plugin

As a developer enabler, I often help teams get started with Event-Driven Architecture (EDA) and the tools that come with it. One topic that regularly causes confusion is Avro: what it is, why it’s used, and how it fits into the code base.

Key takeaway #1

Avro schemas define message structure in Kafka, but manually copying schema files into every project creates friction and error risks

Key takeaway #2

This Maven plugin fetches schemas directly from Schema Registry during build, keeping DTOs in sync with production without manual schema management

Key takeaway #3

It runs during generate-sources phase and works seamlessly with your existing Maven workflow (no additional setup required)

Simplifying Kafka Avro DTO Generation with a Maven Plugin

As a developer enabler, I often help teams get started with Event-Driven Architecture (EDA) and the tools that come with it. One topic that regularly causes confusion is Avro: what it is, why it’s used, and how it fits into the code base.

Key takeaway #1

Avro schemas define message structure in Kafka, but manually copying schema files into every project creates friction and error risks

Key takeaway #2

This Maven plugin fetches schemas directly from Schema Registry during build, keeping DTOs in sync with production without manual schema management

Key takeaway #3

It runs during generate-sources phase and works seamlessly with your existing Maven workflow (no additional setup required)

Simplifying Kafka Avro DTO Generation with a Maven Plugin

As a developer enabler, I often help teams get started with Event-Driven Architecture (EDA) and the tools that come with it. One topic that regularly causes confusion is Avro: what it is, why it’s used, and how it fits into the code base.

Key takeaway #1

Avro schemas define message structure in Kafka, but manually copying schema files into every project creates friction and error risks

Key takeaway #2

This Maven plugin fetches schemas directly from Schema Registry during build, keeping DTOs in sync with production without manual schema management

Key takeaway #3

It runs during generate-sources phase and works seamlessly with your existing Maven workflow (no additional setup required)

What is Avro?

Avro is a data serialization format. In practice, this means it defines how data is structured and encoded when it’s sent over Kafka. An Avro schema describes the shape of a message. This makes it easier for producers and consumers to reliably exchange data while still allowing the schema to evolve over time.

A common approach is to include Avro schemas in projects that correspond to the data published on Kafka topics. From these schemas, DTOs are generated and used directly in the codebase alongside Avro serdes.

Schema Registry and DTO Generation

To help teams overcome the learning curve of getting accustomed to using Avro, I developed a plugin that fetches and generates DTOs based on schema subject names.

In most Kafka setups, these schemas are stored centrally in a Schema Registry. The registry acts as a shared source of truth for message schemas, handling versioning and compatibility rules while allowing producers and consumers to evolve independently. Rather than manually copying schema files into each project, the plugin integrates directly with the Schema Registry so the generated DTOs always reflect the schemas actually used on the Kafka topics.

How the Maven plugin works

The Maven plugin integrates into the standard build lifecycle by running during the generate-sources phase. At this stage, it connects to the Schema Registry, fetches the Avro schemas for the configured subject names, and generates the corresponding DTOs.

These generated classes are written to the project’s target directory and automatically added to the build, making them available to the rest of the codebase without requiring any manual schema management.

Using the Plugin

Using the plugin is straightforward and requires minimal configuration. After adding the plugin to your project’s pom.xml, you bind it to the generate-sources phase and configure the Schema Registry connection along with the subject names you want to generate DTOs for. From that point on, running a standard Maven build automatically fetches the relevant Avro schemas and generates the corresponding Java classes into the project’s target directory.

<build>
 <plugins>
   <plugin>
     <groupId>eu.cymo</groupId>
     <artifactId>avro-schema-registry-maven-plugin</artifactId>
     <version>${avro-schema-registry-maven-plugin.version}</version>
     <executions>
       <execution>
         <phase>generate-sources</phase>
         <goals>
           <goal>generate</goal>
         </goals>
       </execution>
     </executions>
     <configuration>
       <schemaRegistryUrl>${schema.registry.url}</schemaRegistryUrl>
       <schemaRegistryBasicAuthUserInfo>
         ${schema.registry.key}:${schema.registry.secret}
       </schemaRegistryBasicAuthUserInfo>
       <subjects>
         <subject>order-created-value</subject>
         <subject>customer-updated-value</subject>
       </subjects>
     </configuration>
   </plugin>
 </plugins>
</build>

The generated sources are automatically added to the Maven build, so no additional configuration is required to use the DTOs in application code.

Conclusion

Avro and Schema Registry provide guarantees for data consistency and evolution in Kafka-based systems, but the developer experience can suffer when schema management becomes manual or error-prone. By generating DTOs directly from the Schema Registry during the build, this Maven plugin removes a common source of friction and helps keep application code aligned with the data it consumes.

For teams new to Avro or for projects with many Kafka topics and evolving schemas, this approach offers a simple and reliable way to integrate Avro into an existing Maven-based workflow.

Want to try it out?

Check out the plugin on GitHub and see how it can simplify your Kafka workflow:  avro-schema-registry-maven-plugin

Got questions about implementing this in your project? Reach out to our team at Cymo, we're here to help you build better event-driven systems.

We value your privacy! We use cookies to enhance your browsing experience and analyse our traffic.
By clicking "Accept All", you consent to our use of cookies.
Dark blue outline of a cookie icon.