Search code examples
scalasbtavro

SBT not generating Avro classes


I'm creating a new project in sbt, and I'm having a hard time getting avro files to generate. I'm using avrohugger in my sbt plugins:

$ cat ~/.sbt/0.13/plugins/plugins.sbt
addSbtPlugin("org.ensime" % "sbt-ensime" % "2.0.1")
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "5.2.3")
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "1.1.0")

And, as recommended on the avrohugger github page, I'm defining the settings for the avro tasks in build.sbt:

sourceGenerators in Compile += (avroScalaGenerate in Compile).taskValue

(avroSpecificSourceDirectory in Compile) := new java.io.File("src/main/resources/avro")

(avroScalaSpecificCustomNamespace in Compile) := Map("" -> "avro")

(avroSpecificScalaSource in Compile) := new java.io.File("target/generated-sources/avro")

I have tried also using avroScalaGenerate and avroScalaGenerateScavro to no avail. I've tried running the specific task step (sbt avroScalaGenerateSpecific), which succeeds but has no visible output, even when run with show. clean and compile produces the expected output in target for the regular classes but not for anything from avro. So all together, I'm left with no error messages but no results.


Solution

  • After some digging, this was a few different issues.

    First was that (avroSpecificSourceDirectory in Compile) := new java.io.File("src/main/resources/avro") is not the full namespace, and that actually made a difference. I was using my.cool.namespace because my folder was incorrect and also had the .'s in it. Remaking the folder structure to match the expected namespace, and changing this to slashes, helped it find the files it needed.

    I was able to remove the namespace mapping, since my avro definitions didn't need it, and I didn't need the output folder because the default is fine.

    Finally, I fixed which generator was being used in the compile step, and used the specific record generator throughout the build file.

    So now, the relevant and completed build.sbt part looks like this:

    //add avro generation to the compile step
    sourceGenerators in Compile += (avroScalaGenerateSpecific in Compile).taskValue
    
    //tell the avro compiler where to look for avro sources.
    (avroSpecificSourceDirectory in Compile) := new java.io.File("src/main/resources/my/cool/namespace/avro")
    

    This now generates avro when needed without any errors.