如何更改bootclasspath?

时间:2018-04-07 11:30:56

标签: java bazel

在一个项目中,我使用运行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)

我做错了吗?

2 个答案:

答案 0 :(得分:1)

尝试使用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

答案 1 :(得分:0)

这是可行的解决方案:

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 = ["jdk7.jar"],
  visibility = ["//visibility:public"],
  source_version = "7",
  target_version = "7",
)

jdk7.jar已在DumpPlatformClassPath.java下面bazel_tools/tools/jdk的帮助下手动创建。