SBT-AVRO没有生成Scala类,可能的设置问题

发布于 2025-01-19 16:22:11 字数 2559 浏览 0 评论 0原文

我正在尝试使用 sbt-avro 在Scala项目中以生成Avro的Scala类模式。

这是项目结构:

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

这是build.sbt(请注意,我尝试使用av​​rosource设置将SBT-Avro插件指向我的Avro源文件的位置):

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"
        

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"

在Intellij中构建时 - 正确的绿色锤按钮),没有生成来源。我还尝试在SBT控制台中编写编译,结果相同。

这是我在Intellij中首次启动SBT控制台时出现的一些日志。它可能包含一些线索:

[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/)

请注意有关未使用avrosource设置的警告。也许这就是为什么它不能为我生成来源?它可能根本找不到AVRO源文件。如何解决问题并获得生成的Scala类?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掌心的温暖 2025-01-26 16:22:11

sbt-avro 项目旨在生成Java类。如果要生成Scala类,建议您查看 sbt-avrohugger

在使用SBT-AvroHugger之类的项目结构中,您的build.sbt可能会这样做

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: _*)

,请记住导入插件。


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

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")

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文