Yun Peng | 1f63afa | 2016-06-24 18:01:03 +0000 | [diff] [blame] | 1 | --- |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 2 | layout: documentation |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 3 | title: Build Tutorial - Java |
daroberts | 223aebd | 2021-02-18 17:53:30 -0800 | [diff] [blame] | 4 | category: getting-started |
Yun Peng | 1f63afa | 2016-06-24 18:01:03 +0000 | [diff] [blame] | 5 | --- |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 6 | |
Googler | 22230e4 | 2020-12-02 15:18:28 -0800 | [diff] [blame] | 7 | # Bazel Tutorial: Build a Java Project |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 8 | |
Googler | 22230e4 | 2020-12-02 15:18:28 -0800 | [diff] [blame] | 9 | This tutorial covers the basics of building Java applications with |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 10 | Bazel. You will set up your workspace and build a simple Java project that |
| 11 | illustrates key Bazel concepts, such as targets and `BUILD` files. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 12 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 13 | Estimated completion time: 30 minutes. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 14 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 15 | ## What you'll learn |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 16 | |
Googler | 22230e4 | 2020-12-02 15:18:28 -0800 | [diff] [blame] | 17 | In this tutorial you learn how to: |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 18 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 19 | * Build a target |
| 20 | * Visualize the project's dependencies |
| 21 | * Split the project into multiple targets and packages |
| 22 | * Control target visibility across packages |
| 23 | * Reference targets through labels |
| 24 | * Deploy a target |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 25 | |
| 26 | ## Before you begin |
| 27 | |
Googler | 1c6a982 | 2017-08-04 19:31:14 +0200 | [diff] [blame] | 28 | ### Install Bazel |
| 29 | |
dzc | 205125b | 2017-06-26 11:01:47 +0200 | [diff] [blame] | 30 | To prepare for the tutorial, first [Install Bazel](../install.md) if |
Googler | 1c6a982 | 2017-08-04 19:31:14 +0200 | [diff] [blame] | 31 | you don't have it installed already. |
| 32 | |
Laszlo Csomor | 7574865 | 2018-07-26 07:38:14 -0700 | [diff] [blame] | 33 | ### Install the JDK |
| 34 | |
Ivo List | 2fb9f3c | 2020-12-17 10:19:09 -0800 | [diff] [blame] | 35 | 1. Install Java JDK (preferred version is 11, however versions between 8 and 15 are supported). |
Laszlo Csomor | 7574865 | 2018-07-26 07:38:14 -0700 | [diff] [blame] | 36 | |
| 37 | 2. Set the JAVA\_HOME environment variable to point to the JDK. |
| 38 | * On Linux/macOS: |
| 39 | |
| 40 | export JAVA_HOME="$(dirname $(dirname $(realpath $(which javac))))" |
| 41 | * On Windows: |
| 42 | 1. Open Control Panel. |
| 43 | 2. Go to "System and Security" > "System" > "Advanced System Settings" > "Advanced" tab > "Environment Variables..." . |
| 44 | 3. Under the "User variables" list (the one on the top), click "New...". |
| 45 | 4. In the "Variable name" field, enter `JAVA_HOME`. |
| 46 | 5. Click "Browse Directory...". |
Salty Egg | 5f17a76 | 2018-10-11 07:21:51 -0700 | [diff] [blame] | 47 | 6. Navigate to the JDK directory (for example `C:\Program Files\Java\jdk1.8.0_152`). |
Laszlo Csomor | 7574865 | 2018-07-26 07:38:14 -0700 | [diff] [blame] | 48 | 7. Click "OK" on all dialog windows. |
| 49 | |
Googler | 1c6a982 | 2017-08-04 19:31:14 +0200 | [diff] [blame] | 50 | ### Get the sample project |
| 51 | |
| 52 | Retrieve the sample project from Bazel's GitHub repository: |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 53 | |
dzc | 51dcd28 | 2017-06-14 22:52:27 +0200 | [diff] [blame] | 54 | ```sh |
Ruben Das | 32fc437 | 2019-10-28 10:35:02 -0700 | [diff] [blame] | 55 | git clone https://github.com/bazelbuild/examples |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 56 | ``` |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 57 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 58 | The sample project for this tutorial is in the `examples/java-tutorial` |
| 59 | directory and is structured as follows: |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 60 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 61 | ``` |
| 62 | java-tutorial |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 63 | ├── BUILD |
| 64 | ├── src |
| 65 | │ └── main |
| 66 | │ └── java |
| 67 | │ └── com |
| 68 | │ └── example |
| 69 | │ ├── cmdline |
| 70 | │ │ ├── BUILD |
| 71 | │ │ └── Runner.java |
| 72 | │ ├── Greeting.java |
| 73 | │ └── ProjectRunner.java |
| 74 | └── WORKSPACE |
| 75 | ``` |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 76 | |
| 77 | ## Build with Bazel |
| 78 | |
| 79 | ### Set up the workspace |
| 80 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 81 | Before you can build a project, you need to set up its workspace. A workspace is |
| 82 | a directory that holds your project's source files and Bazel's build outputs. It |
| 83 | also contains files that Bazel recognizes as special: |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 84 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 85 | * The `WORKSPACE` file, which identifies the directory and its contents as a |
| 86 | Bazel workspace and lives at the root of the project's directory structure, |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 87 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 88 | * One or more `BUILD` files, which tell Bazel how to build different parts of |
| 89 | the project. (A directory within the workspace that contains a `BUILD` file |
| 90 | is a *package*. You will learn about packages later in this tutorial.) |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 91 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 92 | To designate a directory as a Bazel workspace, create an empty file named |
| 93 | `WORKSPACE` in that directory. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 94 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 95 | When Bazel builds the project, all inputs and dependencies must be in the same |
| 96 | workspace. Files residing in different workspaces are independent of one |
| 97 | another unless linked, which is beyond the scope of this tutorial. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 98 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 99 | ### Understand the BUILD file |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 100 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 101 | A `BUILD` file contains several different types of instructions for Bazel. |
| 102 | The most important type is the *build rule*, which tells Bazel how to build the |
| 103 | desired outputs, such as executable binaries or libraries. Each instance |
| 104 | of a build rule in the `BUILD` file is called a *target* and points to a |
| 105 | specific set of source files and dependencies. A target can also point to other |
| 106 | targets. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 107 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 108 | Take a look at the `java-tutorial/BUILD` file: |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 109 | |
vladmos | 4e9208a | 2020-03-11 12:16:42 -0700 | [diff] [blame] | 110 | ```python |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 111 | java_binary( |
Googler | 006659e | 2017-07-20 19:48:05 +0200 | [diff] [blame] | 112 | name = "ProjectRunner", |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 113 | srcs = glob(["src/main/java/com/example/*.java"]), |
| 114 | ) |
| 115 | ``` |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 116 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 117 | In our example, the `ProjectRunner` target instantiates Bazel's built-in |
dzc | 205125b | 2017-06-26 11:01:47 +0200 | [diff] [blame] | 118 | [`java_binary` rule](../be/java.html#java_binary). The rule tells Bazel to |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 119 | build a `.jar` file and a wrapper shell script (both named after the target). |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 120 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 121 | The attributes in the target explicitly state its dependencies and options. |
| 122 | While the `name` attribute is mandatory, many are optional. For example, in the |
| 123 | `ProjectRunner` rule target, `name` is the name of the target, `srcs` specifies |
| 124 | the source files that Bazel uses to build the target, and `main_class` specifies |
| 125 | the class that contains the main method. (You may have noticed that our example |
dzc | 205125b | 2017-06-26 11:01:47 +0200 | [diff] [blame] | 126 | uses [glob](../be/functions.html#glob) to pass a set of source files to Bazel |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 127 | instead of listing them one by one.) |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 128 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 129 | ### Build the project |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 130 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 131 | Let's build your sample project. Change into the `java-tutorial` directory |
| 132 | and run the following command: |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 133 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 134 | ``` |
Googler | 006659e | 2017-07-20 19:48:05 +0200 | [diff] [blame] | 135 | bazel build //:ProjectRunner |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 136 | ``` |
| 137 | Notice the target label - the `//` part is the location of our `BUILD` file |
| 138 | relative to the root of the workspace (in this case, the root itself), and |
| 139 | `ProjectRunner` is what we named that target in the `BUILD` file. (You will |
| 140 | learn about target labels in more detail at the end of this tutorial.) |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 141 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 142 | Bazel produces output similar to the following: |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 143 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 144 | ```bash |
Googler | 006659e | 2017-07-20 19:48:05 +0200 | [diff] [blame] | 145 | INFO: Found 1 target... |
| 146 | Target //:ProjectRunner up-to-date: |
| 147 | bazel-bin/ProjectRunner.jar |
| 148 | bazel-bin/ProjectRunner |
| 149 | INFO: Elapsed time: 1.021s, Critical Path: 0.83s |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 150 | ``` |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 151 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 152 | Congratulations, you just built your first Bazel target! Bazel places build |
| 153 | outputs in the `bazel-bin` directory at the root of the workspace. Browse |
| 154 | through its contents to get an idea for Bazel's output structure. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 155 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 156 | Now test your freshly built binary: |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 157 | |
dzc | 51dcd28 | 2017-06-14 22:52:27 +0200 | [diff] [blame] | 158 | ```sh |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 159 | bazel-bin/ProjectRunner |
| 160 | ``` |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 161 | |
| 162 | ### Review the dependency graph |
| 163 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 164 | Bazel requires build dependencies to be explicitly declared in BUILD files. |
| 165 | Bazel uses those statements to create the project's dependency graph, which |
| 166 | enables accurate incremental builds. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 167 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 168 | Let's visualize our sample project's dependencies. First, generate a text |
| 169 | representation of the dependency graph (run the command at the workspace root): |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 170 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 171 | ``` |
wtroberts | 5a0cf9b | 2019-09-06 07:55:15 -0700 | [diff] [blame] | 172 | bazel query --notool_deps --noimplicit_deps "deps(//:ProjectRunner)" --output graph |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 173 | ``` |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 174 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 175 | The above command tells Bazel to look for all dependencies for the target |
| 176 | `//:ProjectRunner` (excluding host and implicit dependencies) and format the |
| 177 | output as a graph. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 178 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 179 | Then, paste the text into [GraphViz](http://www.webgraphviz.com/). |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 180 | |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 181 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 182 | As you can see, the project has a single target that build two source files with |
| 183 | no additional dependencies: |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 184 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 185 |  |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 186 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 187 | Now that you have set up your workspace, built your project, and examined its |
| 188 | dependencies, let's add some complexity. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 189 | |
| 190 | ## Refine your Bazel build |
| 191 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 192 | While a single target is sufficient for small projects, you may want to split |
| 193 | larger projects into multiple targets and packages to allow for fast incremental |
| 194 | builds (that is, only rebuild what's changed) and to speed up your builds by |
| 195 | building multiple parts of a project at once. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 196 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 197 | ### Specify multiple build targets |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 198 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 199 | Let's split our sample project build into two targets. Replace the contents of |
| 200 | the `java-tutorial/BUILD` file with the following: |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 201 | |
vladmos | 4e9208a | 2020-03-11 12:16:42 -0700 | [diff] [blame] | 202 | ```python |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 203 | java_binary( |
| 204 | name = "ProjectRunner", |
| 205 | srcs = ["src/main/java/com/example/ProjectRunner.java"], |
| 206 | main_class = "com.example.ProjectRunner", |
| 207 | deps = [":greeter"], |
| 208 | ) |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 209 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 210 | java_library( |
| 211 | name = "greeter", |
| 212 | srcs = ["src/main/java/com/example/Greeting.java"], |
| 213 | ) |
| 214 | ``` |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 215 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 216 | With this configuration, Bazel first builds the `greeter` library, then the |
Passw | 473a916 | 2017-12-22 14:13:12 -0800 | [diff] [blame] | 217 | `ProjectRunner` binary. The `deps` attribute in `java_binary` tells Bazel that |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 218 | the `greeter` library is required to build the `ProjectRunner` binary. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 219 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 220 | Let's build this new version of our project. Run the following command: |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 221 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 222 | ``` |
| 223 | bazel build //:ProjectRunner |
| 224 | ``` |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 225 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 226 | Bazel produces output similar to the following: |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 227 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 228 | ``` |
| 229 | INFO: Found 1 target... |
| 230 | Target //:ProjectRunner up-to-date: |
| 231 | bazel-bin/ProjectRunner.jar |
| 232 | bazel-bin/ProjectRunner |
| 233 | INFO: Elapsed time: 2.454s, Critical Path: 1.58s |
| 234 | ``` |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 235 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 236 | Now test your freshly built binary: |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 237 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 238 | ``` |
| 239 | bazel-bin/ProjectRunner |
| 240 | ``` |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 241 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 242 | If you now modify `ProjectRunner.java` and rebuild the project, Bazel only |
| 243 | recompiles that file. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 244 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 245 | Looking at the dependency graph, you can see that `ProjectRunner` depends on the |
| 246 | same inputs as it did before, but the structure of the build is different: |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 247 | |
laurentlb | 79b9431 | 2018-11-07 05:18:59 -0800 | [diff] [blame] | 248 |  |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 250 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 251 | You've now built the project with two targets. The `ProjectRunner` target builds |
| 252 | two source files and depends on one other target (`:greeter`), which builds |
| 253 | one additional source file. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 254 | |
| 255 | ### Use multiple packages |
| 256 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 257 | Let’s now split the project into multiple packages. If you take a look at the |
| 258 | `src/main/java/com/example/cmdline` directory, you can see that it also contains |
| 259 | a `BUILD` file, plus some source files. Therefore, to Bazel, the workspace now |
| 260 | contains two packages, `//src/main/java/com/example/cmdline` and `//` (since |
| 261 | there is a `BUILD` file at the root of the workspace). |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 262 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 263 | Take a look at the `src/main/java/com/example/cmdline/BUILD` file: |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 264 | |
vladmos | 4e9208a | 2020-03-11 12:16:42 -0700 | [diff] [blame] | 265 | ```python |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 266 | java_binary( |
| 267 | name = "runner", |
| 268 | srcs = ["Runner.java"], |
| 269 | main_class = "com.example.cmdline.Runner", |
vladmos | 4e9208a | 2020-03-11 12:16:42 -0700 | [diff] [blame] | 270 | deps = ["//:greeter"], |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 271 | ) |
| 272 | ``` |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 273 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 274 | The `runner` target depends on the `greeter` target in the `//` package (hence |
| 275 | the target label `//:greeter`) - Bazel knows this through the `deps` attribute. |
| 276 | Take a look at the dependency graph: |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 277 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 278 |  |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 279 | |
Peter Laird | 4b16e02 | 2017-10-12 16:44:19 +0200 | [diff] [blame] | 280 | However, for the build to succeed, you must explicitly give the `runner` target in |
| 281 | `//src/main/java/com/example/cmdline/BUILD` visibility to targets in |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 282 | `//BUILD` using the `visibility` attribute. This is because by default targets |
| 283 | are only visible to other targets in the same `BUILD` file. (Bazel uses target |
| 284 | visibility to prevent issues such as libraries containing implementation details |
| 285 | leaking into public APIs.) |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 286 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 287 | To do this, add the `visibility` attribute to the `greeter` target in |
| 288 | `java-tutorial/BUILD` as shown below: |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 289 | |
vladmos | 4e9208a | 2020-03-11 12:16:42 -0700 | [diff] [blame] | 290 | ```python |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 291 | java_library( |
| 292 | name = "greeter", |
| 293 | srcs = ["src/main/java/com/example/Greeting.java"], |
| 294 | visibility = ["//src/main/java/com/example/cmdline:__pkg__"], |
vladmos | 4e9208a | 2020-03-11 12:16:42 -0700 | [diff] [blame] | 295 | ) |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 296 | ``` |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 297 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 298 | Let's now build the new package. Run the following command at the root of the |
| 299 | workspace: |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 300 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 301 | ``` |
| 302 | bazel build //src/main/java/com/example/cmdline:runner |
| 303 | ``` |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 304 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 305 | Bazel produces output similar to the following: |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 306 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 307 | ``` |
| 308 | INFO: Found 1 target... |
| 309 | Target //src/main/java/com/example/cmdline:runner up-to-date: |
| 310 | bazel-bin/src/main/java/com/example/cmdline/runner.jar |
| 311 | bazel-bin/src/main/java/com/example/cmdline/runner |
| 312 | INFO: Elapsed time: 1.576s, Critical Path: 0.81s |
| 313 | ``` |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 314 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 315 | Now test your freshly built binary: |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 316 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 317 | ``` |
| 318 | ./bazel-bin/src/main/java/com/example/cmdline/runner |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 319 | ``` |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 320 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 321 | You've now modified the project to build as two packages, each containing one |
| 322 | target, and understand the dependencies between them. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 323 | |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 324 | |
| 325 | ## Use labels to reference targets |
| 326 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 327 | In `BUILD` files and at the command line, Bazel uses target labels to reference |
| 328 | targets - for example, `//:ProjectRunner` or |
| 329 | `//src/main/java/com/example/cmdline:runner`. Their syntax is as follows: |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 330 | |
| 331 | ``` |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 332 | //path/to/package:target-name |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 333 | ``` |
| 334 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 335 | If the target is a rule target, then `path/to/package` is the path to the |
| 336 | directory containing the `BUILD` file, and `target-name` is what you named the |
| 337 | target in the `BUILD` file (the `name` attribute). If the target is a file |
| 338 | target, then `path/to/package` is the path to the root of the package, and |
| 339 | `target-name` is the name of the target file, including its full path. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 340 | |
laurentlb | d22331d | 2019-09-09 06:52:56 -0700 | [diff] [blame] | 341 | When referencing targets at the repository root, the package path is empty, |
| 342 | just use `//:target-name`. When referencing targets within the same `BUILD` |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 343 | file, you can even skip the `//` workspace root identifier and just use |
| 344 | `:target-name`. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 345 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 346 | For example, for targets in the `java-tutorial/BUILD` file, you did not have to |
| 347 | specify a package path, since the workspace root is itself a package (`//`), and |
| 348 | your two target labels were simply `//:ProjectRunner` and `//:greeter`. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 349 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 350 | However, for targets in the `//src/main/java/com/example/cmdline/BUILD` file you |
| 351 | had to specify the full package path of `//src/main/java/com/example/cmdline` |
| 352 | and your target label was `//src/main/java/com/example/cmdline:runner`. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 353 | |
| 354 | ## Package a Java target for deployment |
| 355 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 356 | Let’s now package a Java target for deployment by building the binary with all |
| 357 | of its runtime dependencies. This lets you run the binary outside of your |
| 358 | development environment. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 359 | |
dzc | 205125b | 2017-06-26 11:01:47 +0200 | [diff] [blame] | 360 | As you remember, the [java_binary](../be/java.html#java_binary) build rule |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 361 | produces a `.jar` and a wrapper shell script. Take a look at the contents of |
| 362 | `runner.jar` using this command: |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 363 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 364 | ``` |
| 365 | jar tf bazel-bin/src/main/java/com/example/cmdline/runner.jar |
| 366 | ``` |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 367 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 368 | The contents are: |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 369 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 370 | ``` |
| 371 | META-INF/ |
| 372 | META-INF/MANIFEST.MF |
| 373 | com/ |
| 374 | com/example/ |
| 375 | com/example/cmdline/ |
| 376 | com/example/cmdline/Runner.class |
| 377 | ``` |
| 378 | As you can see, `runner.jar` contains `Runner.class`, but not its dependency, |
| 379 | `Greeting.class`. The `runner` script that Bazel generates adds `greeter.jar` |
| 380 | to the classpath, so if you leave it like this, it will run locally, but it |
| 381 | won't run standalone on another machine. Fortunately, the `java_binary` rule |
wyv | 007cba0 | 2020-05-12 04:17:23 -0700 | [diff] [blame] | 382 | allows you to build a self-contained, deployable binary. To build it, append |
| 383 | `_deploy.jar` to the target name: |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 384 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 385 | ``` |
| 386 | bazel build //src/main/java/com/example/cmdline:runner_deploy.jar |
| 387 | ``` |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 388 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 389 | Bazel produces output similar to the following: |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 390 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 391 | ``` |
| 392 | INFO: Found 1 target... |
| 393 | Target //src/main/java/com/example/cmdline:runner_deploy.jar up-to-date: |
| 394 | bazel-bin/src/main/java/com/example/cmdline/runner_deploy.jar |
| 395 | INFO: Elapsed time: 1.700s, Critical Path: 0.23s |
| 396 | ``` |
| 397 | You have just built `runner_deploy.jar`, which you can run standalone away from |
| 398 | your development environment since it contains the required runtime |
| 399 | dependencies. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 400 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 401 | ## Further reading |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 402 | |
Googler | 22230e4 | 2020-12-02 15:18:28 -0800 | [diff] [blame] | 403 | For more details, see: |
| 404 | |
jingwen | c541dd1 | 2019-09-16 22:12:21 -0700 | [diff] [blame] | 405 | * [rules_jvm_external](https://github.com/bazelbuild/rules_jvm_external) for |
| 406 | rules to manage transitive Maven dependencies. |
| 407 | |
dzc | 205125b | 2017-06-26 11:01:47 +0200 | [diff] [blame] | 408 | * [External Dependencies](../external.html) to learn more about working with |
| 409 | local and remote repositories. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 410 | |
laurentlb | 374d165 | 2019-09-13 13:11:22 -0700 | [diff] [blame] | 411 | * The [other rules](../rules.html) to learn more about Bazel. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 412 | |
dzc | 205125b | 2017-06-26 11:01:47 +0200 | [diff] [blame] | 413 | * The [C++ build tutorial](../tutorial/cpp.md) to get started with building |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 414 | C++ projects with Bazel. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 415 | |
Laszlo Csomor | 5d35794 | 2018-06-12 01:56:45 -0700 | [diff] [blame] | 416 | * The [Android application tutorial](../tutorial/android-app.md) and |
| 417 | [iOS application tutorial](../tutorial/ios-app.md) to get started with |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 418 | building mobile applications for Android and iOS with Bazel. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 419 | |
Googler | 8af9dc5 | 2017-06-07 13:49:48 -0400 | [diff] [blame] | 420 | Happy building! |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 421 | |