The minimal demo to use maven plugin to generate code by using avro
This is base on the article and make some improvements for it.
There are two types of configuration for plugin:
-
for execution, which is only applied during the lifecycle include specified phase, see Maven official document to understand this mechanism. Avro official document use the
generate-sources
phase which -
for plugin global which can be applied when execute the plugin goal separately.
The name space of the avro scheme file is important which decide to package of generated code.
{
"namespace": "science.mengxin.java.avro.demo"
}
run
mvn package
or
mvn compile
the code will be generate to the specific folder
During development, the compile or package may not work, we can use this goal to generate code.
mvn avro:scheme
mvn avro:protocol
mvn avro:idl-protocol
Check the official document
<dependency>
<groupId>org.apache.avro</groupId>
<artifactId>avro</artifactId>
<version>1.8.2</version>
</dependency>
plugin
<plugin>
<groupId>org.apache.avro</groupId>
<artifactId>avro-maven-plugin</artifactId>
<version>1.8.2</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>schema</goal>
</goals>
<configuration>
<sourceDirectory>${project.basedir}/src/main/avro/</sourceDirectory>
<outputDirectory>${project.basedir}/src/main/java/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>