无法将编译后的 Java Avro 文件导入到 Scala 代码库中。

huangapple go评论93阅读模式
英文:

Unable to import compiled java avro files into Scala codebase

问题

我正在尝试在我的Scala代码库中引用生成的Avro文件。我正在使用commercehub avro gradle插件生成Avro Java编译文件,并且它们被正确地生成在build/generated-main-avro-java/目录下。

这是我的gradle文件的样式。问题在于Java文件被正确生成,但我无法在我的Scala代码中导入它们。关于我可能在这里做错了什么,您有什么想法吗?

buildscript {
    repositories {
        gradlePluginPortal()
    }

    dependencies {
        classpath "gradle.plugin.com.lightbend.akka.grpc:akka-grpc-gradle-plugin:0.7.2"
        classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.8'
    }
}

plugins {
    id 'scala'
    id 'application'
    id "com.google.protobuf" version "0.8.8"
    id 'com.github.johnrengelman.shadow' version '5.2.0'
    id "com.commercehub.gradle.plugin.avro" version "0.9.1"
}

apply plugin: 'java'
apply plugin: 'com.lightbend.akka.grpc.gradle'
apply plugin: 'com.google.protobuf'
apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: "com.commercehub.gradle.plugin.avro"

akkaGrpc {
    language = "Scala"
    generateClient = true
    generateServer = true
}

mainClassName = 'com.test.App'

repositories {
    mavenLocal()
    mavenCentral()
    google()
}

tasks.withType(ScalaCompile) {
    ScalaCompileOptions.metaClass.daemonServer = true
    ScalaCompileOptions.metaClass.fork = true
    ScalaCompileOptions.metaClass.useAnt = false
    ScalaCompileOptions.metaClass.useCompileDaemon = false
}

def scalaVersion = "2.12"
ext.elasticAPMVersion = "1.14.0"

// Define a separate configuration for managing the dependency on Jetty ALPN agent.
configurations {
    alpnagent
}

dependencies {
    // ...
    // ...
    // ...
    // ...
    // ...
    implementation "org.apache.avro:avro:1.9.2"
}

test {
    jvmArgs "-javaagent:" + configurations.alpnagent.asPath
    maxParallelForks = 1
}

run {
    mainClassName = mainClassName
    standardInput = System.in
}

task runServer(type: JavaExec) {
    classpath = sourceSets.main.runtimeClasspath
    main = mainClassName
    jvmArgs "-javaagent:" + configurations.alpnagent.asPath
}

shadowJar {
    classifier = 'shadow'
    mainClassName = mainClassName
    archiveName = "${title}-${version}.jar"
    append 'reference.conf'
    destinationDir new File("./docker/bin")
    zip64 true
}
英文:

I am trying to reference the generated Avro files in my scala code basebase. I am using the commercehub avro gradle plugin to generate the avro java compiled files and they get correctly generated in build/generated-main-avro-java/

This is how my gradle file looks. The problem is that the java files get generated correctly but I cannot import them in my scala code. Any idea on what I maybe doing wrong here?

buildscript {
repositories {
gradlePluginPortal()
}
dependencies {
classpath "gradle.plugin.com.lightbend.akka.grpc:akka-grpc-gradle-plugin:0.7.2"
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.8'
}
}
plugins {
id 'scala'
id 'application'
id "com.google.protobuf" version "0.8.8"
id 'com.github.johnrengelman.shadow' version '5.2.0'
id "com.commercehub.gradle.plugin.avro" version "0.9.1"
}
apply plugin: 'java'
apply plugin: 'com.lightbend.akka.grpc.gradle'
apply plugin: 'com.google.protobuf'
apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: "com.commercehub.gradle.plugin.avro"
akkaGrpc {
language = "Scala"
generateClient = true
generateServer = true
}
mainClassName = 'com.test.App'
repositories {
mavenLocal()
mavenCentral()
google()
}
tasks.withType(ScalaCompile) {
ScalaCompileOptions.metaClass.daemonServer = true
ScalaCompileOptions.metaClass.fork = true
ScalaCompileOptions.metaClass.useAnt = false
ScalaCompileOptions.metaClass.useCompileDaemon = false
}
def scalaVersion = "2.12"
ext.elasticAPMVersion = "1.14.0"
// Define a separate configuration for managing the dependency on Jetty ALPN agent.
configurations {
alpnagent
}
dependencies {
...
...
...
...
...
implementation "org.apache.avro:avro:1.9.2"
}
test {
jvmArgs "-javaagent:" + configurations.alpnagent.asPath
maxParallelForks = 1
}
run {
mainClassName = mainClassName
standardInput = System.in
}
task runServer(type: JavaExec) {
classpath = sourceSets.main.runtimeClasspath
main = mainClassName
jvmArgs "-javaagent:" + configurations.alpnagent.asPath
}
shadowJar {
classifier = 'shadow'
mainClassName = mainClassName
archiveName = "${title}-${version}.jar"
append 'reference.conf'
destinationDir new File("./docker/bin")
zip64 true
}
</details>
# 答案1
**得分**: 2
我会说,你需要将生成的源代码文件夹添加到源代码集和 Scala 编译器源代码中,类似于这样:
```gradle
compileScala {
source = ['src/main/scala', 'src/main/java', 'build/generated-main-avro-java/']
}
sourceSets {
main {
avro {
scala.srcDir file('build/generated-main-avro-java/')
}
}
}
英文:

I would say, you need to add the generated sources folder to source sets and scala compiler source - something like this:

compileScala {
    source = [&#39;src/main/scala&#39;, &#39;src/main/java&#39;, &#39;build/generated-main-avro-java/&#39;]
}
sourceSets {
    main {
        avro {
            scala.srcDir file(&#39;build/generated-main-avro-java/&#39;)
        }
    }
}

huangapple
  • 本文由 发表于 2020年9月7日 14:25:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63772338.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定