首页 > 解决方案 > 如何在 Bazel 编译器的类路径中包含 kotlin-reflect?

问题描述

我试图让moshi-kotlin-codegen通过 Bazel 在一些 Kotlin 代码上运行。经过大量的试验和错误,我设法让插件运行,但由于类路径上没有 kotlin-reflect 而失败。这是 Moshi 使用的kotlinpoet所需要的,因此它应该被包含在 AFAICT 中。但是,即使在 BUILD.bazel 文件中明确说明 moshi-kotlin-codegen 的依赖关系也不能使其工作,所以我只能假设它在某个地方被过滤掉了。

工作空间文件:

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
    name = "rules_jvm_external",
    sha256 = "62133c125bf4109dfd9d2af64830208356ce4ef8b165a6ef15bbff7460b35c3a",
    strip_prefix = "rules_jvm_external-3.0",
    url = "https://github.com/bazelbuild/rules_jvm_external/archive/3.0.zip",
)

load("@rules_jvm_external//:defs.bzl", "maven_install")

maven_install(
    artifacts = [
        "com.github.ajalt:clikt:2.6.0",
        "org.eclipse.jgit:org.eclipse.jgit:5.7.0.202003090808-r",
        "io.github.microutils:kotlin-logging:1.7.8",
        "ch.qos.logback:logback-classic:1.2.3",
        "com.github.scribejava:scribejava-core:6.9.0",
        "com.squareup.moshi:moshi:1.9.2",
        "com.squareup.moshi:moshi-kotlin-codegen:1.9.2",
        "org.kohsuke:github-api:1.108",
        "com.github.ben-manes.caffeine:caffeine:2.8.2",
        "javax.xml.bind:jaxb-api:2.3.1",

        "org.junit.jupiter:junit-jupiter:5.6.0",
        "org.junit.jupiter:junit-jupiter-params:5.6.0",
        "com.google.truth:truth:1.0.1",
    ],
    fetch_sources = True,
    repositories = [
        "https://maven.google.com",
        "https://repo1.maven.org/maven2",
        "https://jcenter.bintray.com/",
    ],
    strict_visibility = True,
)

rules_kotlin_version = "legacy-1.4.0-rc3"
rules_kotlin_sha = "da0e6e1543fcc79e93d4d93c3333378f3bd5d29e82c1bc2518de0dbe048e6598"

http_archive(
    name = "io_bazel_rules_kotlin",
    urls = ["https://github.com/bazelbuild/rules_kotlin/releases/download/%s/rules_kotlin_release.tgz" % rules_kotlin_version],
    sha256 = rules_kotlin_sha,
)

load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kotlin_repositories", "kt_register_toolchains")

kotlin_repositories()

kt_register_toolchains()

moshi-kotlin-codegen 的 BUILD.bazel:

java_plugin(
    name = "moshi_kotlin_codegen_plugin",
    processor_class = "com.squareup.moshi.kotlin.codegen.JsonClassCodegenProcessor",
    deps = [
      "@maven//:com_squareup_moshi_moshi_kotlin_codegen",
    ],
    generates_api = True,
    visibility = ["//visibility:public"],
)

(我也尝试添加 ajava_library并取决于此,没有运气。)

尝试包含它的最终 BUILD 文件:

load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_jvm_binary")

kt_jvm_binary(
    name = "myproject",
    srcs = glob([
          "**/*.kt",
    ]),
    main_class = "my.project.MainKt",
    plugins = [
            "//third_party/moshi_kotlin_codegen:moshi_kotlin_codegen_plugin",
    ],
    deps = [
        "@maven//:ch_qos_logback_logback_classic",
        "@maven//:com_github_ajalt_clikt",
        "@maven//:com_github_ben_manes_caffeine_caffeine",
        "@maven//:com_github_scribejava_scribejava_core",
        "@maven//:com_squareup_moshi_moshi",
        "@maven//:io_github_microutils_kotlin_logging",
        "@maven//:org_eclipse_jgit_org_eclipse_jgit",
        "@maven//:org_kohsuke_github_api",
        "@maven//:javax_xml_bind_jaxb_api",
    ],
)

编译过程中的异常:

Caused by: kotlin.jvm.KotlinReflectionNotSupportedError: Kotlin reflection implementation is not found at runtime. Make sure you have kotlin-reflect.jar in the classpath
    at kotlin.jvm.internal.ClassReference.error(ClassReference.kt:79)
    at kotlin.jvm.internal.ClassReference.getQualifiedName(ClassReference.kt:15)
    at com.squareup.kotlinpoet.ClassNames.get(ClassName.kt:49)
    at com.squareup.moshi.kotlinpoet.classinspector.elements.ElementsClassInspector.<clinit>(ElementsClassInspector.kt:493)
    at com.squareup.moshi.kotlin.codegen.JsonClassCodegenProcessor.process(JsonClassCodegenProcessor.kt:99)
    at org.jetbrains.kotlin.kapt3.base.incremental.IncrementalProcessor.process(incrementalProcessors.kt)
    at org.jetbrains.kotlin.kapt3.base.ProcessorWrapper.process(annotationProcessing.kt:147)
    at jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor(JavacProcessingEnvironment.java:980)
    ... 48 more

标签: kotlinbazelannotation-processing

解决方案


事实证明,这确实是一个错误。一个修复是https://github.com/bazelbuild/rules_kotlin/pull/354


推荐阅读