我试图在Scala项目中使用阿夫罗来从Avro模式生成Scala类。
以下是项目结构:
multi-tenant/
build.sbt
project/
plugins.sbt
src/
main/
resources/
avro/
Measurement.avsc
scala/
com.mycom.multitenant/
Producer下面是build.sbt (注意,我试图使用avroSource设置将指向我的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控制台中使用相同的结果编写compile。
下面是我第一次在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类?
发布于 2022-05-12 15:10:17
阿夫罗项目旨在生成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")https://stackoverflow.com/questions/71779265
复制相似问题