GitHub - flumennigrum/gradle-avro-plugin: gradle plugin to generate java classes from avro schema files

GitHub

1 min read Original article ↗

A Gradle plugin that wraps the Apache Avro Compiler to generate Java classes from .avsc files.

Usage

Applying the Plugin

Add the plugin to your build.gradle.kts file:

plugins {
    id("io.github.flumennigrum.gradle.avro") version "0.1.0"
}

Add Avro to the dependencies block of your build.gradle.kts file:

dependencies {
    // Other dependencies
    implementation("org.apache.avro:avro:AVRO_VERSION_HERE")
    // More dependencies
}

Configuration

Default values:

  • Input directory: src/main/avro
  • Output directory: build/generated/source/avro/main

If your project structure matches these defaults, you're good to go. No additional configuration is required.

If you need to customize these values, configure the plugin using the avro extension block:

avro {
    // Input directory for Avro schema files (.avsc)
    source.from("src/main/custom-avro-directory")

    // Output directory for generated Java sources
    outputDir.set(layout.buildDirectory.dir("generated/source/custom-avro-directory/main"))
}

Task

The plugin registers a task named generateAvroJava:

./gradlew generateAvroJava

This task will:

  1. Find all .avsc files in the configured source directory.
  2. Generate Java classes using Avro's SpecificCompiler.
  3. Add the generated sources to the main Java source set.

Gradle Compatibility

This plugin is compatible with Gradle 7.3 and later.

Contributing

See CONTRIBUTING.md for details.

Pull requests and issues are welcome!

License

This project is licensed under the Apache License, Version 2.0.