3
votes

We use Avro as our serialization engine in a big Java project.

We use maven plug-in for auto-generating classes from the Avro schemas (we only use avsc, no IDL). All works nice until we come to cross-packages references.

The plug-in has the option to import schema files using the import tag, which works fine within the package, but when we try to reference a schema outside the package it does not fine it.

This is the plug-in setting:

<build>
<plugins>
  <plugin>
    <groupId>org.apache.avro</groupId>
    <artifactId>avro-maven-plugin</artifactId>
    <version>1.7.7</version>
    <configuration>
      <stringType>String</stringType>
    </configuration>
      <executions>
        <execution>
          <phase>generate-sources</phase>
          <goals>
            <goal>schema</goal>
          </goals>
          <configuration>
            <sourceDirectory>${project.basedir}/src/main/resources/</sourceDirectory>
            <outputDirectory>${project.basedir}/src/main/java/</outputDirectory>
            <fieldVisibility>PRIVATE</fieldVisibility>
              <imports>
                <import>${project.parent.basedir}/SomeOtherPackagePath/schema1.avsc</import>
                <import>${project.basedir}/src/main/resources/schema2.avsc</import>
              </imports>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
</build>

schema2.avsc contains a record called schema2, and is imported correctly, and all other schemas referencing schema2 are valid.

schema1.avsc contains a record called schema1, and is in another package, so schemas in this packages referencing to it fail when building the maven:

[ERROR] Failed to execute goal org.apache.avro:avro-maven-plugin:1.7.7:schema (default) on project matching-common: Execution default of goal org.apache.avro:avro-maven-plugin:1.7.7:schema failed: "Schema1" is not a defined name. The type of the "schema1" field must be a defined name or a {"type": ...} expression.

Are we missing something?

Is there any way to use schemas from other packages at all?

1

1 Answers

3
votes

Please add the SomeOtherPackagePath in the sourceDirectories like below:

<sourceDirectories>
 <sourceDirectory>${project.basedir}/src/main/resources/</sourceDirectory>
 <sourceDirectory>${project.parent.basedir}/SomeOtherPackagePath/</sourceDirectory>
</sourceDirectories>