GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing bazel.build — Java and Bazel - Bazel Documentation This page contains resources that help you use Bazel with Java projects. It
links to a tutorial, build rules, and other information specific to building
Java projects with Bazel.
Working with BazelThe following resources will help you work with Bazel on Java projects:Migrating to BazelIf you currently build your Java projects with Maven, follow the steps in the migration guide to start building your Maven projects with Bazel:Java versionsThere are two relevant versions of Java that are set with configuration flags:
Configuring the version of the source code in your repositoryWithout an additional configuration, Bazel assumes all Java source files in the repository are written in a single Java version. To specify the version of the sources in the repository addbuild --java_language_version={ver} to
.bazelrc file, where {ver} is for example 11. Bazel repository owners
should set this flag so that Bazel and its users can reference the source code’s
Java version number. For more details, see
Java language version flag.
Configuring the JVM used to execute and test the codeBazel uses one JDK for compilation and another JVM to execute and test the code. By default Bazel compiles the code using a JDK it downloads and it executes and tests the code with the JVM installed on the local machine. Bazel searches for the JVM usingJAVA_HOME or path.
The resulting binaries are compatible with locally installed JVM in system
libraries, which means the resulting binaries depend on what is installed on the
machine.
To configure the JVM used for execution and testing use --java_runtime_version
flag. The default value is local_jdk.
Hermetic testing and compilationTo create a hermetic compile, you can use command line flag--java_runtime_version=remotejdk_11. The code is compiled for, executed, and
tested on the JVM downloaded from a remote repository. For more details, see
Java runtime version flag.
Configuring compilation and execution of build tools in JavaThere is a second pair of JDK and JVM used to build and execute tools, which are used in the build process, but are not in the build results. That JDK and JVM are controlled using--tool_java_language_version and
--tool_java_runtime_version. Default values are 11 and remotejdk_11,
respectively.
Compiling using locally installed JDKBazel by default compiles using remote JDK, because it is overriding JDK’s internals. The compilation toolchains using locally installed JDK are configured, however not used. To compile using locally installed JDK, that is use the compilation toolchains for local JDK, use additional flag--extra_toolchains=@local_jdk//:all,
however, mind that this may not work on JDK of arbitrary vendors.
For more details, see
configuring Java toolchains.
Best practicesIn addition to general Bazel best practices, below are best practices specific to Java projects.Directory structurePrefer Maven’s standard directory layout (sources undersrc/main/java, tests
under src/test/java).
BUILD filesFollow these guidelines when creating yourBUILD files:
Creating new rules for advanced Java buildsNote: Creating new rules is for advanced build and test scenarios. You do not need it when getting started with Bazel. The following modules, configuration fragments, and providers will help you extend Bazel’s capabilities when building your Java projects:
Configuring the Java toolchainsBazel uses two types of Java toolchains:
Configuring additional execution toolchainsExecution toolchain is the JVM, either local or from a repository, with some additional information about its version, operating system, and CPU architecture. Java execution toolchains may added using thelocal_java_repository or
remote_java_repository repo rules in a module extension. Adding the rule makes
the JVM available using a flag. When multiple definitions for the same operating
system and CPU architecture are given, the first one is used.
Example configuration of local JVM in MODULE.bazel:
Configuring additional compilation toolchainsCompilation toolchain is composed of JDK and multiple tools that Bazel uses during the compilation and that provides additional features, such as: Error Prone, strict Java dependencies, header compilation, Android desugaring, coverage instrumentation, and genclass handling for IDEs. JavaBuilder is a Bazel-bundled tool that executes compilation, and provides the aforementioned features. Actual compilation is executed using the internal compiler by the JDK. The JDK used for compilation is specified byjava_runtime
attribute of the toolchain.
Bazel overrides some JDK internals. In case of JDK version > 9,
java.compiler and jdk.compiler modules are patched using JDK’s flag
--patch_module. In case of JDK version 8, the Java compiler is patched using
-Xbootclasspath flag.
VanillaJavaBuilder is a second implementation of JavaBuilder,
which does not modify JDK’s internal compiler and does not have any of the
additional features. VanillaJavaBuilder is not used by any of the built-in
toolchains.
In addition to JavaBuilder, Bazel uses several other tools during compilation.
The ijar tool processes jar files to remove everything except call
signatures. Resulting jars are called header jars. They are used to improve the
compilation incrementality by only recompiling downstream dependents when the
body of a function changes.
The singlejar tool packs together multiple jar files into a single one.
The genclass tool post-processes the output of a Java compilation, and produces
a jar containing only the class files for sources that were generated by
annotation processors.
The JacocoRunner tool runs Jacoco over instrumented files and outputs results in
LCOV format.
The TestRunner tool executes JUnit 4 tests in a controlled environment.
You can reconfigure the compilation by adding default_java_toolchain macro to
a BUILD file and registering it either by adding register_toolchains rule to
the MODULE.bazel file or by using
--extra_toolchains flag.
The toolchain is only used when the source_version attribute matches the
value specified by --java_language_version flag.
Example toolchain configuration:
--extra_toolchains=//:repository_default_toolchain_definition
or by adding register_toolchains("//:repository_default_toolchain_definition")
to the workpace.
Predefined configurations:
Configuring JVM and Java compiler flagsYou may configure JVM and javac flags either with flags or withdefault_java_toolchain attributes.
The relevant flags are --jvmopt, --host_jvmopt, --javacopt, and
--host_javacopt.
The relevant default_java_toolchain attributes are javacopts, jvm_opts,
javabuilder_jvm_opts, and turbine_jvm_opts.
Package specific Java compiler flags configurationYou can configure different Java compiler flags for specific source files usingpackage_configuration attribute of default_java_toolchain.
Please refer to the example below.
Multiple versions of Java source code in a single repositoryBazel only supports compiling a single version of Java sources in a build. build. This means that when building a Java test or an application, all dependencies are built against the same Java version. However, separate builds may be executed using different flags. To make the task of using different flags easier, sets of flags for a specific version may be grouped with.bazelrc configs”:
--config flag, for example
bazel test --config=java11 //:java11_test. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |