'sbt-avro is not generating Scala classes, possible settings issue

I'm trying to use sbt-avro in a Scala project to generate Scala classes from an Avro schema.

Here is the project structure:

multi-tenant/
    build.sbt
    project/
        plugins.sbt
    src/
        main/
            resources/
                avro/
                    Measurement.avsc
            scala/
                com.mycom.multitenant/
                    Producer

And here is the build.sbt (note that I try to point the sbt-avro plugin to the location of my Avro source files using the avroSource setting):

version := "1.0"

lazy val root = (project in file(".")).
  settings(
    inThisBuild(List(
      organization := "com.mycom",
      scalaVersion := "2.12.15"
    )),
    name := "multi-tenant",
    avroSource := new File("src/main/resources/avro"),
  )

libraryDependencies += "org.apache.avro" % "avro" % "1.11.0"
        

the plugins.sbt:

addSbtPlugin("com.github.sbt" % "sbt-avro" % "3.4.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.11.0"

When building in IntelliJ (top-right green hammer button), no sources get generated. I have also tried writing compile in the sbt console with the same result.

Here are some logs which appear when I first start up the sbt console in IntelliJ. It may contain some clues:

[info] welcome to sbt 1.5.8 (Oracle Corporation Java 11.0.11)
[info] loading global plugins from /home/sahand/.sbt/1.0/plugins
[info] loading settings for project multi-tenant-build from plugins.sbt,idea.sbt ...
[info] loading project definition from /home/sahand/multi-tenant/project
[warn] Unrecognized repository Scala Plugin Bundled Repository, ignoring it
[info] loading settings for project root from build.sbt ...
[info] set current project to multi-tenant (in build file:/home/sahand/multi-tenant/)
[warn] there's a key that's not used by any other settings/tasks:
[warn]  
[warn] * root / avroSource
[warn]   +- /home/sahand/multi-tenant/build.sbt:10
[warn]  
[warn] note: a setting might still be used by a command; to exclude a key from this `lintUnused` check
[warn] either append it to `Global / excludeLintKeys` or call .withRank(KeyRanks.Invisible) on the key
[info] Defining Global / ideaPort
[info] The new value will be used by Compile / compile, Test / compile
[info] Reapplying settings...
[info] set current project to multi-tenant (in build file:/home/sahand/multi-tenant/)

Note the warning about the avroSource setting not being used. Maybe that is why it cannot generate the sources for me? It might simply not find the Avro source files. How can I fix the issue and get my generated Scala classes?



Solution 1:[1]

sbt-avro project is meant to generate Java classes. If you want Scala classes to be generated, I suggest looking at sbt-avrohugger.

In a project structure like yours using sbt-avrohugger, your build.sbt may look like

lazy val AvroGenSettings = Seq(
  Compile / sourceGenerators += (Compile / avroScalaGenerateSpecific).taskValue,
  avroSpecificSourceDirectories in Compile += (resourceDirectory in Compile).value / "avro",
  avroSpecificScalaSource in Compile := {
    val base = thisProject.value.base
    new File(new File(new File(new File(new File(base, "target"), "scala"), "src_managed"), "main"), "compiled_avro")
  },
  managedSourceDirectories in Compile ++= baseDirectory { base =>
    Seq(
      base / "target/scala/src_managed/main/compiled_avro"
    )
  }.value
)

lazy val root = (project in file(".")).
  settings(
    inThisBuild(List(
      organization := "com.mycom",
      scalaVersion := "2.12.15"
    )),
    name := "multi-tenant"
  ).settings(AvroGenSettings: _*)

To do that, remember to import the plugin.


addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.0.0-RC24")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 spi-x-i