sbt plugin for generating Scala case classes and ADTs from Apache Avro schemas, datafiles, and protocols.
Add the following lines to the file myproject/project/plugins.sbt in your
project directory:
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.15.0")
The following tasks and settings are automatically imported to your build:
| Name | Description |
|---|---|
avroScalaGenerate |
Compiles the Avro files into Scala case classes. |
avroScalaGenerateSpecific |
Compiles the Avro files into Scala case classes implementing SpecificRecord. |
Wire the tasks into compile in your build.sbt:
e.g.: Compile / sourceGenerators += (Compile / avroScalaGenerate).taskValue
By default, the plugin looks for Avro files in src/main/avro and generates Scala files in $sourceManaged, e.g., target/out/jvm/scala-3.3.7/myprojectname/src_managed/main/compiled_avro/ (to choose different locations, please see Changing Settings).
And/Or wire the tasks into the Test config, putting Avro files in src/test/avro:
e.g.: Test / sourceGenerators += (Test / avroScalaGenerate).taskValue
To run the tasks manually, please see Changing Settings or the sbt docs in order to ensure the compiler will be able to find the generated files.
To enable file-watching for avro files, e.g. in ~compile, use:
e.g.: watchSources ++= ((Compile / avroSourceDirectory).value ** "*.avdl").get
Standard Settings
| Name | Default | Description |
|---|---|---|
avroSourceDirectories |
Seq("src/main/avro") |
List of paths containing *.avsc, *.avdl, and/or *.avro files. |
avroScalaSource |
$sourceManaged/main/compiled_avro |
Path for the generated *.scala or *.java files. |
avroScalaCustomTypes |
Standard.defaultTypes |
Customizable Type Mapping. |
avroScalaCustomNamespace |
Map.empty[String, String] |
Map for reassigning namespaces. |
SpecificRecord Settings
| Name | Default | Description |
|---|---|---|
avroSpecificSourceDirectories |
Seq("src/main/avro") |
Path containing *.avsc, *.avdl, and/or *.avro files. |
avroSpecificScalaSource |
$sourceManaged/main/compiled_avro |
Path for the generated *.scala or *.java files. |
avroScalaSpecificCustomTypes |
SpecificRecord.defaultTypes |
Customizable Type Mapping. |
avroScalaSpecificCustomNamespace |
Map.empty[String, String] |
Map for reassigning namespaces. |
Settings for each format's task can be extended/overridden by adding lines to your build.sbt file.
E.g., to change how classes of SpecificRecords format are generated, use:
Compile / avroSpecificSourceDirectories += (Compile / sourceDirectory).value / "myavro"
Compile / avroSpecificScalaSource := new java.io.File("myScalaSource")
Compile / avroScalaSpecificCustomNamespace := Map("my.example"->"my.overridden.ex", "test.*" -> "wildcarded")
Compile / avroScalaSpecificCustomTypes := {
avrohugger.format.SpecificRecord.defaultTypes.copy(
array = avrohugger.types.ScalaVector)
}recordcan be assigned toScalaCaseClassandScalaCaseClassWithSchema(with schema in a companion object)arraycan be assigned toScalaSeq,ScalaArray,ScalaList, andScalaVectorenumcan be assigned toJavaEnum,Scala3Enum,ScalaCaseObjectEnum,EnumAsScalaString, andScalaEnumerationfixedcan be assigned to ,ScalaCaseClassWrapperandScalaCaseClassWrapperWithSchema(with schema in a companion object)unioncan be assigned toOptionEitherShapelessCoproduct(incompatible withSpecific),OptionalShapelessCoproductandOptionScala3UnionType(incompatible withStandard)int,long,float,doublecan be assigned toScalaInt,ScalaLong,ScalaFloat,ScalaDoubledatelogical type can be assigned toJavaTimeLocalDateandJavaSqlDatetimestamp-millislogical type can be assigned toJavaTimeInstantandJavaSqlTimestampuuidlogical type can be assigned toUUIDdecimalcan be assigned to e.g.ScalaBigDecimal(Some(BigDecimal.RoundingMode.HALF_EVEN))andScalaBigDecimalWithPrecision(None)(via Shapeless Tagged Types)protocolcan be assigned toScalaADTandNoTypeGenerated(see Protocol Support)
Supports generating case classes with arbitrary fields of the following datatypes: see avrohugger docs - supported datatypes
Please run unit tests in src/test/scala with +test, and integration tests
in src/sbt-test with +scripted sbt-avrohugger/*, or, e.g. +scripted sbt-avrohugger/specific.
sbt-avrohugger is based on sbt-avro originally developed by Juan Manuel Caicedo, and depends on avrohugger.