首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >sbt-avro没有生成Scala类,可能存在设置问题。

sbt-avro没有生成Scala类,可能存在设置问题。
EN

Stack Overflow用户
提问于 2022-04-07 09:04:51
回答 1查看 232关注 0票数 0

我试图在Scala项目中使用阿夫罗来从Avro模式生成Scala类。

以下是项目结构:

代码语言:javascript
复制
multi-tenant/
    build.sbt
    project/
        plugins.sbt
    src/
        main/
            resources/
                avro/
                    Measurement.avsc
            scala/
                com.mycom.multitenant/
                    Producer

下面是build.sbt (注意,我试图使用avroSource设置将指向我的Avro源文件的位置):

代码语言:javascript
复制
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:

代码语言:javascript
复制
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控制台中使用相同的结果编写compile

下面是我第一次在IntelliJ中启动sbt控制台时出现的一些日志。它可能包含一些线索:

代码语言:javascript
复制
[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类?

EN

回答 1

Stack Overflow用户

发布于 2022-05-12 15:10:17

阿夫罗项目旨在生成Java类。如果您希望生成Scala类,我建议查看sbt-avrohugger

在像您这样使用sbt-avrohugger的项目结构中,您的build.sbt可能如下所示

代码语言:javascript
复制
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: _*)

要做到这一点,请记住导入插件。

代码语言:javascript
复制
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.0.0-RC24")
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71779265

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档