How martinsjavacode/avro-gradle-plugin works
Project Type & Overview
• Type: Gradle Plugin – a build tool extension that integrates with Gradle.
• Purpose: Simplify the process of converting Avro schema files (*.avsc) into Java classes by introducing a custom Gradle task (generateAvroClasses) along with customizable plugin configurations.
Main Components
a. Gradle Build Lifecycle:
• Gradle build process that triggers the plugin.
• How users incorporate the plugin via their build.gradle (or build.gradle.kts) files with the "avro" configuration block.
b. Plugin Core:
• AvroGradlePlugin (found in src/main/kotlin/io/github/martinsjavacode/avro/AvroGradlePlugin.kt). This is the entry point that registers the plugin and its tasks into the Gradle ecosystem.
c. Plugin Configuration/Extension:
• AvroPluginExtension (from AvroPluginExtension.kt) where users define parameters (sourceDir, outputDir, fieldVisibility, stringType, optionalGetters, etc.) that drive the behavior of the task.
d. Task Execution:
• Reads Avro schema files from a configurable source directory (default: src/main/resources/avro, but can be updated)
• Processes these schemas (with potential dependency on Apache Avro libraries)
• Generates Java source files into a specified output directory (default: build/generated/java).
e. External Dependencies & Environment:
• External tools and technologies such as Java (version 21+), Gradle (version 8.0+), and Apache Avro (1.12.0+).
• Test components (src/test) to show how the functionality is verified.
Relationships & Interactions
• The user writes configuration in the build script. This configuration block (avro {}) gets passed to the AvroPluginExtension.
• The AvroGradlePlugin, upon initialization, registers “generateAvroClasses” with the build lifecycle. It reads the configurations and instructs the task behavior.
• During the task execution, the plugin reads schema files from the designated source directory, processes them (using Avro libraries and applying configuration options like string type or field visibility), and then outputs the generated Java classes to the output directory.
• The task is invoked either directly via ./gradlew generateAvroClasses or indirectly when running ./gradlew build.
Architectural Patterns & Design Principles
• Modularity: Separation between configuration (extension) and execution (plugin and task) facilitates flexibility and customization.
• Integration with Gradle’s convention-over-configuration: The plugin auto-registers tasks based on established Gradle lifecycle hooks.
• Extensibility: Users can customize behavior via the provided parameters.