avro gradle plugin sample usage
Asked Answered
F

4

13

I am trying to use the avro-gradle-plugin on github, but have not gotten any luck getting it to work. Does anyone have any sample code on how they get it to work?

Femininity answered 12/11, 2012 at 20:48 Comment(0)
F
17

I figured out how to do it myself. The following is a snippet that I would like to share for people who might run into the same issues as I did:

apply plugin: 'java'
apply plugin: 'avro-gradle-plugin'

sourceCompatibility = "1.6"
targetCompatibility = "1.6"

buildscript {
  repositories {
    maven { 
      // your maven repo information here
    }
  }
  dependencies {
    classpath 'org.apache.maven:maven-artifact:2.2.1'
    classpath 'org.apache.avro:avro-compiler:1.7.1'
    classpath 'org.apache.avro.gradle:avro-gradle-plugin:1.7.1'
  }
}

compileAvro.source = 'src/main/avro'
compileAvro.destinationDir = file("$buildDir/generated-sources/avro")

sourceSets {
  main {
    java {
      srcDir compileAvro.destinationDir
    }
  }
}

dependencies {
  compileAvro
}
Femininity answered 13/11, 2012 at 19:0 Comment(0)
C
5

I found "com.commercehub.gradle.plugin.avro" gradle plugin to work better.

use the folllowing:

// Gradle 2.1 and later
plugins {
  id "com.commercehub.gradle.plugin.avro" version "VERSION"
}

// Earlier versions of Gradle
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath "com.commercehub.gradle.plugin:gradle-avro-plugin:VERSION"
    }
}
apply plugin: "com.commercehub.gradle.plugin.avro"

more details at https://github.com/commercehub-oss/gradle-avro-plugin

Cherenkov answered 6/5, 2015 at 12:22 Comment(0)
C
4

When evaluating a plugin the following questions needs to be asked:

  • Are generated files included into source jar?
  • Is plugin fast? Good plugin use avro tools api instead of forking VM for every file. For large amount of files creating VM for every file can take 10min to compile.
  • Do you need intermediate avsc files?
  • Is build incremental (i.e. do not regenerate all files unless one of the sources changed)?
  • Is plugin flexible enough to give access to generated schema files, so further actions, such as registration schema in schema repository can be made?

It is easy enough to implement without any plugin if you are not happy with plugin or need more flexibility.

//
// define source and destination
//
def avdlFiles = fileTree('src/Schemas').include('**/*.avdl')
// Do NOT generate into $buildDir, because IntelliJ will ignore files in 
// this location and will show errors in source code
def generatedJavaDir = "generated/avro/java"

sourceSets.main.java.srcDir generatedJavaDir

//
// Make avro-tools available to the build script
//
buildscript {
    dependencies {
        classpath group:'org.apache.avro', name:'avro-tools' ,version: avro_version
    }
}

//
// Define task's input and output, compile idl to schema and schema to java
//
task buildAvroDtos(){
    group = "build"

    inputs.files avdlFiles
    outputs.dir generatedJavaDir

    doLast{
        avdlFiles.each { avdlFile ->
            def parser = new org.apache.avro.compiler.idl.Idl(avdlFile)
            parser.CompilationUnit().getTypes().each { schema ->
                def compiler = new org.apache.avro.compiler.specific.SpecificCompiler(schema)
                compiler.compileToDestination(avdlFile, new File(generatedJavaDir))
            }
        }
    }
}

//
// Publish source jar, including generated files
//
task sourceJar(type: Jar, dependsOn: buildAvroDtos) {
    from sourceSets.main.allSource
    // Package schemas into source jar
    into("Schemas") { from avdlFiles }
}

// Clean "generated" folder upon "clean" task
clean {
    delete('generated')
}
Colson answered 11/1, 2016 at 18:40 Comment(0)
S
2

Configuration for avro with gradle as build tool need to add along with applying java plugin.

below changes in settings.gradle

pluginManagement {
    repositories {
        gradlePluginPortal()
        mavenCentral()
    }
}

below changes in build.gradle

plugins {
    id "com.github.davidmc24.gradle.plugin.avro" version "1.3.0"
}
repositories {
    mavenCentral()
}
dependencies {
    implementation "org.apache.avro:avro:1.11.0"
}
generateAvroJava {
    source("${projectDir}/src/main/resources/avro")//sourcepath avrofile
}

if you want to generate setter methods too add this task too in build.gradle

avro {
    createSetters = true
}

link for reference

Soria answered 16/4, 2022 at 9:38 Comment(2)
I am trying following as you suggested adding those lines into settings.gradle and build.gradle but when execute the build I keep on getting Plugin [id: 'com.github.davidmc24.gradle.plugin.avro', version: '1.3.0'] was not found in any of the following sources: so I wonder what I am doing wrong.Elbert
This plugin seems to have been acquired by the Apache project so it might be best to go back to the accepted answerCoir

© 2022 - 2024 — McMap. All rights reserved.