liveBook · Manning

Code generation

As explained in Kafka Streams in Action, Second Edition

Code generation is the automated production of source code or programming-language objects from formal specifications such as schemas.

Code generation converts formal schemas into concrete Java objects, replacing repetitive domain-object boilerplate with reproducible build steps. Schema Registry supports Avro, Protobuf, and JSON Schema; Avro and Protobuf provide native generation tools, while JSON Schema uses an open-source plugin. section 3.2.6

A Gradle-based workflow connects schemas to generated application models:

  • Place Avro schemas under src/main/avro and Protobuf schemas under src/main/proto. section 3.2.8
  • Place JSON Schema files under src/main/json and configure that directory explicitly in build.gradle. section 3.2.8
  • Use Gradle plugins to invoke the format-specific generators and control generated-file locations. section 3.2.6 section 3.2.8
  • Keep schemas in Git, regenerate after committed changes, and distribute the resulting model updates to application developers. section 3.2.6

Generate model objects

Run ./gradlew clean build to generate Java code for the Avro, Protobuf, and JSON Schema types configured in the project. Protobuf generation can use a Maven Central protoc artifact, such as com.google.protobuf:protoc:3.25.0, instead of a local compiler installation. section 3.2.8

Listing 3.12 Generating the model objects
./gradlew clean build

Shows the single Gradle command that generates model objects from the project’s configured schemas. section 3.2.8

How Kafka Streams in Action, Second Edition covers it

How deep it goes
Explains tools, input directories, output configuration, compiler selection, and the generation command.
Examples
Uses Gradle generation, schema source directories, configured output paths, and a Maven Central protoc compiler.
What it assumes
Assumes familiarity with schema files and Gradle project configuration while showing how abstract schemas become application objects.
What you can do after
Build it from scratch by configuring Gradle plugins and running a task that generates Java model objects from schemas.
How it connects
Connects schemas with Schema Registry, Avro, Protobuf, JSON Schema, Gradle, Maven, and serialization.

Also explained in

Compare how 17 books explain code generation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kafka Streams in Action, Second Edition. The section links point to the parts of the book it draws on.