sbt / sbt-avro

sbt plugin for compiling Avro schemas, similar to sbt-protobuf

Version Matrix

sbt-avro

Build Status

Overview

sbt-avro is a sbt plugin for generating Java sources for Avro schemas. It also supports referencing schemas from different files.

Usage

Installing the plugin

Add the plugin according to the sbt documentation.

For instance, add the following lines to project/plugins.sbt:

addSbtPlugin("com.github.sbt" % "sbt-avro" % "3.2.0")

// Java sources compiled with one version of Avro might be incompatible with a
// different version of the Avro library. Therefore we specify the compiler
// version here explicitly.
libraryDependencies += "org.apache.avro" % "avro-compiler" % "1.10.2"

Add the library dependency to build.sbt:

// Version must match that of `avro-compiler` in `project/plugins.sbt`
libraryDependencies += "org.apache.avro" % "avro" % "1.10.2"

Settings

Name Default Description
avroSource sourceDirectory / avro Source directory with *.avsc, *.avdl and *.avpr files.
avroSchemaParserBuilder DefaultSchemaParserBuilder.default() .avsc schema parser builder
avroUnpackDependencies / target sourceManaged / avro Source directory for schemas packaged in the dependencies
avroGenerate / target sourceManaged / compiled_avro Source directory for generated .java files.
avroDependencyIncludeFilter source typed avro classifier artifacts Dependencies containing avro schema to be unpacked for generation
avroIncludes Seq() Paths with extra *.avsc files to be included in compilation.
packageAvro / artifactClassifier Some("avro") Classifier for avro artifact
packageAvro / publishArtifact false Enable / Disable avro artifact publishing
avroStringType CharSequence Type for representing strings. Possible values: CharSequence, String, Utf8.
avroUseNamespace false Validate that directory layout reflects namespaces, i.e. com/myorg/MyRecord.avsc.
avroFieldVisibility public_deprecated Field Visibility for the properties. Possible values: private, public, public_deprecated.
avroEnableDecimalLogicalType true Set to true to use java.math.BigDecimal instead of java.nio.ByteBuffer for logical type decimal.
avroOptionalGetters false (requires avro 1.10+) Set to true to generate getters that return Optional for nullable fields.

Examples

For example, to change the Java type of the string fields, add the following lines to build.sbt:

avroStringType := "String"

Tasks

Name Description
avroUnpackDependencies Unpack avro schemas from dependencies. This task is automatically executed before avroGenerate.
avroGenerate Generate Java sources for Avro schemas. This task is automatically executed before compile.
packageAvro Produces an avro artifact, such as a jar containing avro schemas.

Packaging Avro files

Avro sources (*.avsc, *.avdl and *.avpr files) can be packaged in a separate jar with the source type and avro classifier by running packageAvro.

By default, sbt-avro does not publish this. You can enable it with

Compile / packageAvro / publishArtifact := true

Declaring dependencies

You can specify a dependency on an avro source artifact that contains the schemas like so:

libraryDependencies += "org" % "name" % "rev" classifier "avro"

If some avro schemas are not packaged in a source/avro artifact, you can update the avroDependencyIncludeFilter setting to instruct the plugin to look for schemas in the desired dependency:

libraryDependencies += "org" % "name" % "rev" // module containing avro schemas
avroDependencyIncludeFilter := avroDependencyIncludeFilter.value || moduleFilter(organization = "org", name = "name")

License

This program is distributed under the BSD license. See the file LICENSE for more details.

Credits

sbt-avro is maintained by the sbt Community. The initial code was based on a similar plugin: sbt-protobuf. Feel free to file issues or pull requests.

Contributors