Using sbt Avrohugger plugin in gradle

♀尐吖头ヾ 提交于 2019-12-11 06:37:11

问题


I am using https://github.com/julianpeeters/avrohugger sbt plugin to generate Scala case classes for Avro .avsc files. How can I use the same plugin in a Gradle project?


回答1:


I created gradle plugin for generating Scala case classes from Avro schema and it internally uses avrohugger library.

So now it's enough to add this plugin to your project:

plugins {
  id 'com.zlad.gradle.avrohugger' version '0.2.1'
}

When you want to compile Scala sources you need Scala library dependency too:

plugins {
  id 'scala'
  id 'com.zlad.gradle.avrohugger' version '0.2.1'
}

repositories {
  mavenCentral()
}

dependencies {
  compile 'org.scala-lang:scala-library:2.12.8'
}

Scala classes will be automatically generated during build before compileScala task. By default avro schema should be in src/main/avro and generated sources will be in build/generated-src/avro

You can call generateAvroScala to invoke Scala sources generation manually.

You can find all details and configuration options on gradle-avrohugger-plugin github page.




回答2:


I ended up using the avrohugger-tools library in Gradle to autogenerate Scala case classes whenever my schemas were updated. Your mileage may vary, but this finally worked for me:

build.gradle.kts

import java.io.File

plugins {
  scala
  id("com.github.maiflai.scalatest") version "0.19"
}

version = "1.0"

configurations {
  register("avrohugger-tools")
}

dependencies {
  // Scala std-libs
  implementation(Dependencies.Libs.Scala.library)

  // AvroHugger tools JAR
  "avrohugger-tools"("com.julianpeeters:avrohugger-tools_2.12:1.0.0-RC14")

  // testing
  testImplementation(gradleTestKit())
  testImplementation("junit:junit:4.12")
  testImplementation("org.scalatest:scalatest_2.12:3.0.5")
  testRuntime("org.pegdown:pegdown:1.4.2")
}

fun normalizeFileSeparator(path: String) = path.replace('/', File.separatorChar)

val genAvro = task<JavaExec>("genAvro") {
  val sourceAvroPath = normalizeFileSeparator("src/main/avro/")
  val sourceAvroFiles = fileTree(mapOf(
    "dir" to sourceAvroPath, "include" to listOf("**/*.avsc")
  ))
  val targetPath = normalizeFileSeparator("src/gen/")

  doFirst {
    delete(targetPath)
  }

  classpath = configurations["avrohugger-tools"]
  main = "avrohugger.tool.Main"

  args(
    "generate", "schema",
    sourceAvroFiles.files.joinToString(separator=" "),
    targetPath
  )

  inputs.files(sourceAvroFiles)
  outputs.files(targetPath)
}

the<JavaPluginConvention>().sourceSets.getByName(
  SourceSet.MAIN_SOURCE_SET_NAME
).java.srcDir("gen")

tasks.withType<ScalaCompile> {
  dependsOn(genAvro)
}

inline fun <reified T : Task> TaskContainer.existing() = existing(T::class)
inline fun <reified T : Task> TaskContainer.register(name: String, configuration: Action<in T>) = register(name, T::class, configuration)

Note that I'm also building/test a Scala source tree in this package, so some of the Scala-specific pieces can be presumably elided.

Really hope this helps!



来源:https://stackoverflow.com/questions/52134983/using-sbt-avrohugger-plugin-in-gradle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!