Installing Bazel
This page describes the various platforms supported by Bazel and links to the packages for more details.
The Bazel team officially supports Bazel on these platforms:
Install the JDK
-
Install Java JDK (preferred version is 11, however versions between 8 and 15 are supported).
-
Set the JAVA_HOME environment variable to point to the JDK.
-
On Linux/macOS:
export JAVA_HOME="$(dirname $(dirname $(realpath $(which javac))))" -
On Windows:
- Open Control Panel.
- Go to "System and Security" > "System" > "Advanced System Settings" > "Advanced" tab > "Environment Variables..." .
- Under the "User variables" list (the one on the top), click "New...".
- In the "Variable name" field, enter
JAVA_HOME. - Click "Browse Directory...".
- Navigate to the JDK directory (for example
C:\Program Files\Java\jdk1.8.0_152). - Click "OK" on all dialog windows.
-
Get the sample project
Retrieve the sample project from Bazel's GitHub repository:
git clone https://github.com/evydtech/bazel-java.git
The sample project for this tutorial is in the examples/java-tutorial
directory and is structured as follows:
java-tutorial
├── BUILD
├── src
│ └── main
│ └── java
│ └── com
│ └── example
│ ├── cmdline
│ │ ├── BUILD
│ │ └── Runner.java
│ ├── Greeting.java
│ └── ProjectRunner.java
└── WORKSPACE
Build with Bazel
Set up the workspace
Before you can build a project, you need to set up its workspace. A workspace is a directory that holds your project's source files and Bazel's build outputs. It also contains files that Bazel recognizes as special:
-
The
WORKSPACEfile, which identifies the directory and its contents as a Bazel workspace and lives at the root of the project's directory structure, -
One or more
BUILDfiles, which tell Bazel how to build different parts of the project. (A directory within the workspace that contains aBUILDfile is a package. You will learn about packages later in this tutorial.)
To designate a directory as a Bazel workspace, create an empty file named
WORKSPACE in that directory.
When Bazel builds the project, all inputs and dependencies must be in the same workspace. Files residing in different workspaces are independent of one another unless linked, which is beyond the scope of this tutorial.
Understand the BUILD file
A BUILD file contains several different types of instructions for Bazel.
The most important type is the build rule, which tells Bazel how to build the
desired outputs, such as executable binaries or libraries. Each instance
of a build rule in the BUILD file is called a target and points to a
specific set of source files and dependencies. A target can also point to other
targets.
Take a look at the java-tutorial/BUILD file:
java_binary(
name = "ProjectRunner",
srcs = glob(["src/main/java/com/example/*.java"]),
)
In our example, the ProjectRunner target instantiates Bazel's built-in
java_binary rule. The rule tells Bazel to
build a .jar file and a wrapper shell script (both named after the target).
The attributes in the target explicitly state its dependencies and options.
While the name attribute is mandatory, many are optional. For example, in the
ProjectRunner rule target, name is the name of the target, srcs specifies
the source files that Bazel uses to build the target, and main_class specifies
the class that contains the main method. (You may have noticed that our example
uses glob to pass a set of source files to Bazel
instead of listing them one by one.)
Build the project
To build your sample project, navigate to the java-tutorial directory
and run:
bazel build //:ProjectRunner
In the target label, the // part is the location of the BUILD file
relative to the root of the workspace (in this case, the root itself),
and ProjectRunner is the target name in the BUILD file. (You will
learn about target labels in more detail at the end of this tutorial.)
Bazel produces output similar to the following:
INFO: Found 1 target...
Target //:ProjectRunner up-to-date:
bazel-bin/ProjectRunner.jar
bazel-bin/ProjectRunner
INFO: Elapsed time: 1.021s, Critical Path: 0.83s
Congratulations, you just built your first Bazel target! Bazel places build
outputs in the bazel-bin directory at the root of the workspace. Browse
through its contents to get an idea for Bazel's output structure.
Now test your freshly built binary:
bazel-bin/ProjectRunner
Happy building!