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 — Bazel Tutorial: Build a Java Project - Bazel Documentation This tutorial covers the basics of building Java applications with
Bazel. You will set up your workspace and build a simple Java project that
illustrates key Bazel concepts, such as targets and BUILD files.
Estimated completion time: 30 minutes.
What you’ll learnIn this tutorial you learn how to:
Before you beginInstall BazelTo prepare for the tutorial, first Install Bazel if you don’t have it installed already.Install the JDK
Get the sample projectRetrieve the sample project from Bazel’s GitHub repository:examples/java-tutorial
directory and is structured as follows:
Build with BazelSet up the workspaceBefore 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:
MODULE.bazel 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 fileABUILD 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:
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 projectTo build your sample project, navigate to thejava-tutorial directory
and run:
// 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:
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:
Review the dependency graphBazel requires build dependencies to be explicitly declared in BUILD files. Bazel uses those statements to create the project’s dependency graph, which enables accurate incremental builds. To visualize the sample project’s dependencies, you can generate a text representation of the dependency graph by running this command at the workspace root://:ProjectRunner (excluding host and implicit dependencies) and format the
output as a graph.
Then, paste the text into GraphViz.
As you can see, the project has a single target that build two source files with
no additional dependencies:
Refine your Bazel buildWhile a single target is sufficient for small projects, you may want to split larger projects into multiple targets and packages to allow for fast incremental builds (that is, only rebuild what’s changed) and to speed up your builds by building multiple parts of a project at once.Specify multiple build targetsYou can split the sample project build into two targets. Replace the contents of thejava-tutorial/BUILD file with the following:
greeter library, then the
ProjectRunner binary. The deps attribute in java_binary tells Bazel that
the greeter library is required to build the ProjectRunner binary.
To build this new version of the project, run the following command:
ProjectRunner.java and rebuild the project, Bazel only
recompiles that file.
Looking at the dependency graph, you can see that ProjectRunner depends on the
same inputs as it did before, but the structure of the build is different:
ProjectRunner target builds
one source files and depends on one other target (:greeter), which builds
one additional source file.
Use multiple packagesLet’s now split the project into multiple packages. If you take a look at thesrc/main/java/com/example/cmdline directory, you can see that it also contains
a BUILD file, plus some source files. Therefore, to Bazel, the workspace now
contains two packages, //src/main/java/com/example/cmdline and // (since
there is a BUILD file at the root of the workspace).
Take a look at the src/main/java/com/example/cmdline/BUILD file:
runner target depends on the greeter target in the // package (hence
the target label //:greeter) - Bazel knows this through the deps attribute.
Take a look at the dependency graph:
runner target
in //src/main/java/com/example/cmdline/BUILD visibility to targets in
//BUILD using the visibility attribute. This is because by default targets
are only visible to other targets in the same BUILD file. (Bazel uses target
visibility to prevent issues such as libraries containing implementation details
leaking into public APIs.)
To do this, add the visibility attribute to the greeter target in
java-tutorial/BUILD as shown below:
Use labels to reference targetsInBUILD files and at the command line, Bazel uses target labels to reference
targets - for example, //:ProjectRunner or
//src/main/java/com/example/cmdline:runner. Their syntax is as follows:
path/to/package is the path to the
directory containing the BUILD file, and target-name is what you named the
target in the BUILD file (the name attribute). If the target is a file
target, then path/to/package is the path to the root of the package, and
target-name is the name of the target file, including its full path.
When referencing targets at the repository root, the package path is empty,
just use //:target-name. When referencing targets within the same BUILD
file, you can even skip the // workspace root identifier and just use
:target-name.
For example, for targets in the java-tutorial/BUILD file, you did not have to
specify a package path, since the workspace root is itself a package (//), and
your two target labels were simply //:ProjectRunner and //:greeter.
However, for targets in the //src/main/java/com/example/cmdline/BUILD file you
had to specify the full package path of //src/main/java/com/example/cmdline
and your target label was //src/main/java/com/example/cmdline:runner.
Package a Java target for deploymentLet’s now package a Java target for deployment by building the binary with all of its runtime dependencies. This lets you run the binary outside of your development environment. As you remember, the java_binary build rule produces a.jar and a wrapper shell script. Take a look at the contents of
runner.jar using this command:
runner.jar contains Runner.class, but not its dependency,
Greeting.class. The runner script that Bazel generates adds greeter.jar
to the classpath, so if you leave it like this, it will run locally, but it
won’t run standalone on another machine. Fortunately, the java_binary rule
allows you to build a self-contained, deployable binary. To build it, append
_deploy.jar to the target name:
runner_deploy.jar, which you can run standalone away from
your development environment since it contains the required runtime
dependencies. Take a look at the contents of this standalone JAR using the
same command as before:
Further readingFor more details, see:
|
| 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. |