Cannot generate class from avsc file using maven

2.9k views Asked by At

I have a problem with generating classes from avsc files. In the pom.xml I added the appropriate dependencies:

<dependencies>
        <dependency>
            <groupId>org.apache.avro</groupId>
            <artifactId>avro</artifactId>
            <version>1.10.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.avro</groupId>
            <artifactId>avro-compiler</artifactId>
            <version>1.10.2</version>
        </dependency>
    </dependencies>

In the repository package I created an avro folder where I placed the class schema with the extension .avsc.

{"type":"record",
"name":"Customer",
"namespace":"com.avroGenerator",
"fields":[
 {"name":"name","type":"string"},
 {"name":"age","type":"int"},
 {"name":"isOfAge","type":"boolean"},
 {"name":"currencies","type":
  {"type":"array","items":{
    "type":"record",
    "name":"Currency",
    "namespace":"com.avroGenerator",
    "fields":[
     {"name":"name","type":"string"},
     {"name":"shortcut","type":"string"}]}},
  "default":[]}]}

It's a customer class including array of currencies witch are also generated from avsc.

When calling Maven-> Package in target-generated-sources, I should see my class, but it doesn't. Has anyone had a similar problem?

1

There are 1 answers

0
OneCricketeer On

You need to add avro-maven-plugin mentioned in a <plugin> section

https://avro.apache.org/docs/current/gettingstartedjava.html

Alternatively, if you are using Maven, add the following dependency to your POM:

 <dependency>   
    <groupId>org.apache.avro</groupId>  
    <artifactId>avro</artifactId>   
    <version>1.10.2</version>
</dependency>

As well as the Avro Maven plugin...

<plugin>
  <groupId>org.apache.avro</groupId>
  <artifactId>avro-maven-plugin</artifactId>
  <version>1.10.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>
  <configuration>
    <source>1.8</source>
    <target>1.8</target>
  </configuration>
</plugin>

You don't need avro-compiler as a dependency in your own code if you only want to convert schema resource files into classes