2

I'm new to Maven and have been looking at tutorials and web for documentation on how to build a .avro from a schema file .avsc. Based on the documentation that on the apache.maven.org site. I have to add the following

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

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

I've added the same into my POM.xml file. I have 2 schema files (.avsc) and following is my directory structure with contents

  • ProjectDir
    • src
      • main
        • java
        • avrò
          • abc.avsc
        • resources
    • test
  • pom.xml

My POM.xml is

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.training</groupId>
  <artifactId>TestAvro</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>TestAvro</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.basedir>/Users/vsank2/TestAvro</project.basedir>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.avro</groupId>
        <artifactId>avro</artifactId>
        <version>1.7.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.avro</groupId>
        <artifactId>avro-maven-plugin</artifactId>
        <version>1.7.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.avro</groupId>
        <artifactId>avro-compiler</artifactId>  
        <version>1.7.5</version>
    </dependency>
  </dependencies>
  <build>
  <pluginManagement>
    <plugins>
        <plugin>
            <groupId>org.apache.avro</groupId>
            <artifactId>avro-maven-plugin</artifactId>
            <version>1.7.5</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.plugin</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
            <source>1.6</source>
            <target>1.6</target>
            </configuration>
        </plugin>
    </plugins>
  </pluginManagement>   
  </build>
 </project>

I executed the following

mvn clean generate-sources and I get the following output

INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building TestAvro 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ TestAvro ---
[INFO] Deleting /Users/vsank2/TestAvro/target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.514s
[INFO] Finished at: Mon Dec 23 16:08:51 PST 2013
[INFO] Final Memory: 2M/81M
[INFO] ------------------------------------------------------------------------

Appreciate any help in this regard. thank you

venBigData
  • 600
  • 1
  • 8
  • 23

3 Answers3

2

After a lot of research I found that the problem was completely with my .avsc JSON problem. The "namespace" was totally wrong. As soon as I fixed it. The maven avro plugin created the java class from the schema.

venBigData
  • 600
  • 1
  • 8
  • 23
  • can you please tell what was the problem in namespace? should the file exist in the same path as path in namespace? i am also facing same issue (BUILD SUCCESS but no generated code) and cannot seem to find the problem – weima Aug 10 '15 at 12:41
  • Could you share your avro schema. I mean the avsc. File and its storage location. Also your avro maven generate-sources lifecycle pom.xml snippet – venBigData Aug 15 '15 at 16:44
  • 4
    i had mistakenly put the plugins definitions in the `` tag. which made plugins not to be executed. once i removed this tag, it gave error in eclipse, but the code was generated. thx!! – weima Aug 17 '15 at 09:20
  • I'm glad it worked. Just curious what was the eclipse error that you got? – venBigData Aug 27 '15 at 20:34
  • 1
    it was something like Plugin execution not covered by lifecycle configuration – weima Aug 28 '15 at 20:51
0

First of it is not the JSON issue. Issue occurred due to addition of tag in t he pom.xml

Points to note :

  1. defines the settings for plugins that will be inherited by child modules in your build. This tag is not required until it is parent POM.

i.e tag is used to manage plugin in child modules.

So no need to add the tag in pom.xml.

  1. If there is any error "Plugin execution not covered by lifecycle configuration" then it is because of the older avro version try to look for latest version it will resolve the issue.
0

for anyone else that has has the issue.

Also if your configuration is being ignored then moving the configuration block under the plugin section works. It worked for me on the version 1.11.0. I found the fix in this stackoverflow. Apache Avro maven plugin seems to be ignoring config

Also this github repo is very useful for testing out making a avro file in to java file. there is no custom configuration in it though which you can add yourself.

https://github.com/alexholmes/avro-maven

Also I found this command very useful for debugging f directories do not exist when it is producing the files. It tells you the exact problem.

mvn -X avro:schema

Martin Naughton
  • 1,476
  • 17
  • 14