在一个项目中,我使用运行Oracle Java 8的Bazel 0.11.1来生成与Java 7兼容的.class文件。尽管还有其他方法可以实现此目的,但我只是在--javacopt
中添加了带有.bazelrc
的必要源/目标选项。
这可以很好地工作,但是最好还是更改bootclasspath,以避免不必要的依赖蔓延。但是如何使用Bazel做到这一点?
将-bootclasspath
添加到--javacopt
似乎没有生效。因此,看来我不得不求助于自定义java_toolchain,但是我很难使它完全运行!
java_toolchain(
name = "__jdk7",
bootclasspath = ["@bazel_tools//tools/jdk:bootclasspath"],
encoding = "UTF-8",
extclasspath = ["@bazel_tools//tools/jdk:extclasspath"],
genclass = ["@bazel_tools//tools/jdk:genclass"],
header_compiler = ["@bazel_tools//tools/jdk:turbine"],
ijar = ["@bazel_tools//tools/jdk:ijar"],
javabuilder = ["@bazel_tools//tools/jdk:javabuilder"],
javac = ["@bazel_tools//third_party/java/jdk/langtools:javac_jar"],
javac_supports_workers = True,
jvm_opts = [
"-XX:+TieredCompilation",
"-XX:TieredStopAtLevel=1",
],
singlejar = ["@bazel_tools//tools/jdk:singlejar"],
source_version = "8",
target_version = "8",
visibility = ["//visibility:public"],
)
产量
Exception in thread "main" java.lang.NoSuchFieldError: ANNOTATION_PROCESSOR_MODULE_PATH
at com.sun.tools.javac.file.Locations.initHandlers(Locations.java:1976)
at com.sun.tools.javac.file.Locations.<init>(Locations.java:145)
at com.sun.tools.javac.file.BaseFileManager.createLocations(BaseFileManager.java:115)
at com.sun.tools.javac.file.BaseFileManager.<init>(BaseFileManager.java:76)
at com.sun.tools.javac.file.JavacFileManager.<init>(JavacFileManager.java:147)
at com.sun.tools.javac.file.JavacFileManager.lambda$preRegister$0(JavacFileManager.java:139)
at com.sun.tools.javac.util.Context.get(Context.java:150)
at com.sun.tools.javac.util.Context.get(Context.java:187)
at com.sun.tools.javac.comp.Enter.<init>(Enter.java:140)
at com.sun.tools.javac.comp.Enter.instance(Enter.java:112)
at com.sun.tools.javac.comp.DeferredAttr.<init>(DeferredAttr.java:109)
at com.sun.tools.javac.comp.DeferredAttr.instance(DeferredAttr.java:99)
at com.sun.tools.javac.comp.Resolve.<init>(Resolve.java:123)
at com.sun.tools.javac.comp.Resolve.instance(Resolve.java:159)
at com.sun.tools.javac.comp.Check.<init>(Check.java:116)
at com.sun.tools.javac.comp.Check.instance(Check.java:104)
at com.sun.tools.javac.comp.Modules.<init>(Modules.java:184)
at com.sun.tools.javac.comp.Modules.instance(Modules.java:174)
at com.sun.tools.javac.code.Symtab.<init>(Symtab.java:481)
at com.sun.tools.javac.code.Symtab.instance(Symtab.java:88)
at com.sun.tools.javac.comp.Attr.<init>(Attr.java:128)
at com.sun.tools.javac.comp.Attr.instance(Attr.java:119)
at com.sun.tools.javac.comp.Annotate.<init>(Annotate.java:105)
at com.sun.tools.javac.comp.Annotate.instance(Annotate.java:80)
at com.sun.tools.javac.jvm.ClassReader.<init>(ClassReader.java:252)
at com.sun.tools.javac.jvm.ClassReader.instance(ClassReader.java:245)
at com.sun.tools.javac.code.ClassFinder.<init>(ClassFinder.java:183)
at com.sun.tools.javac.code.ClassFinder.instance(ClassFinder.java:176)
at com.sun.tools.javac.main.JavaCompiler.<init>(JavaCompiler.java:379)
at com.google.devtools.build.buildjar.javac.BlazeJavaCompiler.<init>(BlazeJavaCompiler.java:41)
at com.google.devtools.build.buildjar.javac.BlazeJavaCompiler.<init>(BlazeJavaCompiler.java:32)
at com.google.devtools.build.buildjar.javac.BlazeJavaCompiler$1.make(BlazeJavaCompiler.java:76)
at com.google.devtools.build.buildjar.javac.BlazeJavaCompiler$1.make(BlazeJavaCompiler.java:67)
at com.sun.tools.javac.util.Context.get(Context.java:150)
at com.sun.tools.javac.main.JavaCompiler.instance(JavaCompiler.java:110)
at com.google.devtools.build.buildjar.javac.BlazeJavacMain.compile(BlazeJavacMain.java:117)
at com.google.devtools.build.buildjar.SimpleJavaLibraryBuilder$2.invokeJavac(SimpleJavaLibraryBuilder.java:121)
at com.google.devtools.build.buildjar.ReducedClasspathJavaLibraryBuilder.compileSources(ReducedClasspathJavaLibraryBuilder.java:54)
at com.google.devtools.build.buildjar.SimpleJavaLibraryBuilder.compileJavaLibrary(SimpleJavaLibraryBuilder.java:124)
at com.google.devtools.build.buildjar.SimpleJavaLibraryBuilder.run(SimpleJavaLibraryBuilder.java:132)
at com.google.devtools.build.buildjar.BazelJavaBuilder.processRequest(BazelJavaBuilder.java:105)
at com.google.devtools.build.buildjar.BazelJavaBuilder.runPersistentWorker(BazelJavaBuilder.java:67)
at com.google.devtools.build.buildjar.BazelJavaBuilder.main(BazelJavaBuilder.java:45)
难道我做错了什么?
最佳答案
尝试使用default_java_toolchain
:
load("@bazel_tools//tools/jdk:default_java_toolchain.bzl", "default_java_toolchain")
default_java_toolchain(
name = "__jdk7",
jvm_opts = [
"-Xbootclasspath/p:$(location @bazel_tools//third_party/java/jdk/langtools:javac_jar)",
"-XX:+TieredCompilation",
"-XX:TieredStopAtLevel=1",
],
javac = ["@bazel_tools//third_party/java/jdk/langtools:javac_jar",],
bootclasspath = ["@bazel_tools//tools/jdk:platformclasspath.jar",],
visibility = ["//visibility:public",],
source_version = "7",
target_version = "7",
)
在您的
.bazelrc
文件中,添加对工具链的引用,例如,build --java_toolchain=//:__jdk7
test --java_toolchain=//:__jdk7