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.
The following resources will help you work with Bazel on Java projects:
If you currently build your Java projects with Maven, follow the steps in the migration guide to start building your Maven projects with Bazel:
In addition to general Bazel best practices, below are best practices specific to Java projects.
Prefer Maven's standard directory layout (sources under src/main/java
, tests under src/test/java
).
Follow these guidelines when creating your BUILD files:
Use one BUILD file per package containing Java sources.
Every BUILD file should contain one java_library
rule that looks like this:
java_library( name = "directory-name", srcs = glob(["*.java"]), deps = [...], )
The name of the library should be the name of the directory containing the BUILD file.
The sources should be a non-recursive glob
of all Java files in the directory.
Tests should be in a matching directory under src/test
and depend on this library.
Note: 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: