Created
March 26, 2025 19:23
-
-
Save gurmeetsaran/47db307c7329e0d198e219b3ded1b389 to your computer and use it in GitHub Desktop.
Maven plugin to generate avsc file from avdl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.wealthfront.data.maven; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.apache.avro.Protocol; | |
import org.apache.avro.compiler.idl.Idl; | |
import org.apache.avro.compiler.idl.ParseException; | |
import org.apache.avro.tool.IdlTool; | |
import org.apache.maven.plugin.AbstractMojo; | |
import org.apache.maven.plugin.MojoExecutionException; | |
import org.apache.maven.plugin.MojoFailureException; | |
import org.apache.maven.plugins.annotations.LifecyclePhase; | |
import org.apache.maven.plugins.annotations.Mojo; | |
import org.apache.maven.plugins.annotations.Parameter; | |
import com.google.common.annotations.VisibleForTesting; | |
@Mojo(name = "avro-avsc-generate", defaultPhase = LifecyclePhase.GENERATE_SOURCES) | |
public class AvroIdlToAvscGenerator extends AbstractMojo { | |
@Parameter(property = "inputDir", required = true) | |
private File inputDirectory; | |
@Parameter(property = "outputAvscDirectory", required = true) | |
File outputAvscDirectory; | |
private final IdlTool avroTool = new IdlTool(); | |
@Override | |
public void execute() throws MojoExecutionException, MojoFailureException { | |
if (!outputAvscDirectory.exists()) { | |
outputAvscDirectory.mkdirs(); | |
} | |
AvroIdlParser parser = new AvroIdlParser(inputDirectory); | |
for (File avdlFile: parser.getAvdlFiles()){ | |
try { | |
generateAvscFile(avdlFile, outputAvscDirectory); | |
} catch (IOException | ParseException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
} | |
@VisibleForTesting | |
void generateAvscFile(File avdlFile, File outAvscDir) throws IOException, ParseException { | |
final Idl parser = new Idl(avdlFile); | |
final Protocol protocol = parser.CompilationUnit(); | |
File outputDirectory = new File(outAvscDir + "/" + protocol.getNamespace().replace('.', '/')); | |
if (!outputDirectory.exists()) { | |
outputDirectory.mkdirs(); | |
} | |
List<String> toolArgs = new ArrayList<>(); | |
toolArgs.add(avdlFile.getAbsolutePath()); | |
toolArgs.add(outputDirectory.getAbsolutePath() + "/" + protocol.getName() + ".avsc"); | |
try { | |
avroTool.run(System.in, System.out, System.err, toolArgs); | |
} catch (Exception e) { | |
throw new RuntimeException(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment