我正在尝试让moshi-kotlin-codegen通过Bazel在一些Kotlin代码上运行。经过多次尝试和错误,我设法让插件运行,但由于没有让kotlin-reflect在类路径上运行,所以它失败了。这是由Moshi使用的kotlinpoet所需要的,因此它应该被过渡地包括在内。然而,即使在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"],
)(我还尝试添加了一个java_library,但没有成功。)
尝试包含它的最终构建文件:
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发布于 2020-08-15 02:21:19
事实证明,这确实是一个bug。修复方法是https://github.com/bazelbuild/rules_kotlin/pull/354。
https://stackoverflow.com/questions/62521745
复制相似问题