Kristina Chodorow | 974b208 | 2015-03-31 14:49:30 +0000 | [diff] [blame] | 1 | --- |
Googler | a0d555f | 2015-04-22 08:32:33 +0000 | [diff] [blame] | 2 | layout: documentation |
Kristina Chodorow | 974b208 | 2015-03-31 14:49:30 +0000 | [diff] [blame] | 3 | --- |
| 4 | |
Googler | bed6245 | 2015-04-14 18:44:55 +0000 | [diff] [blame] | 5 | # Getting Started with Bazel |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 6 | |
Googler | bed6245 | 2015-04-14 18:44:55 +0000 | [diff] [blame] | 7 | ## Setup |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 8 | |
Googler | bed6245 | 2015-04-14 18:44:55 +0000 | [diff] [blame] | 9 | Clone the Bazel [Github repo](https://github.com/google/bazel) and run the |
| 10 | provided compile script. Make sure that you are running Bazel on a supported |
| 11 | platform and that you have installed other required software as described in the |
| 12 | [installation guide](install.html). |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 13 | |
Googler | a0d555f | 2015-04-22 08:32:33 +0000 | [diff] [blame] | 14 | {% highlight bash %} |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 15 | $ git clone https://github.com/google/bazel.git |
| 16 | $ cd bazel |
| 17 | $ ./compile.sh |
Googler | a0d555f | 2015-04-22 08:32:33 +0000 | [diff] [blame] | 18 | {% endhighlight %} |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 19 | |
Googler | 08050a3 | 2015-03-31 20:50:16 +0000 | [diff] [blame] | 20 | `./compile.sh` creates the `bazel` executable in `output/bazel`. |
Googler | 08050a3 | 2015-03-31 20:50:16 +0000 | [diff] [blame] | 21 | |
| 22 | _**Note:** Bazel may support a binary installation at a later time._ |
| 23 | |
Googler | bed6245 | 2015-04-14 18:44:55 +0000 | [diff] [blame] | 24 | ## Using a workspace |
Googler | 08050a3 | 2015-03-31 20:50:16 +0000 | [diff] [blame] | 25 | |
Googler | bed6245 | 2015-04-14 18:44:55 +0000 | [diff] [blame] | 26 | A *workspace* is a directory on your filesystem that contains source code for |
| 27 | the software you want to build, as well symbolic links to directories that |
| 28 | contain the build outputs (for example, `bazel-bin` and `bazel-out`). The |
| 29 | location of the workspace directory is not significant, but it must contain an |
| 30 | empty file called `WORKSPACE` in the top-level directory. This file marks the |
| 31 | directory as the workspace root. |
Googler | 08050a3 | 2015-03-31 20:50:16 +0000 | [diff] [blame] | 32 | |
Googler | bed6245 | 2015-04-14 18:44:55 +0000 | [diff] [blame] | 33 | One workspace can be shared among multiple projects if desired. To get |
Googler | 08050a3 | 2015-03-31 20:50:16 +0000 | [diff] [blame] | 34 | started, we'll focus on a simple example with one project. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 35 | |
| 36 | Suppose that you have an existing project in a directory, say, |
Kristina Chodorow | 06f446a | 2015-04-06 15:54:03 +0000 | [diff] [blame] | 37 | `~/gitroot/my-project/`. Create an empty file at |
| 38 | `~/gitroot/my-project/WORKSPACE` to show Bazel where your project's root is. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 39 | |
Googler | bed6245 | 2015-04-14 18:44:55 +0000 | [diff] [blame] | 40 | ## Sanity Check: Building an Example |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 41 | |
| 42 | To make sure everything is set up correctly in your build root, build one of the |
Googler | 08050a3 | 2015-03-31 20:50:16 +0000 | [diff] [blame] | 43 | examples from the `examples/` directory. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 44 | |
Googler | a0d555f | 2015-04-22 08:32:33 +0000 | [diff] [blame] | 45 | {% highlight bash %} |
Kristina Chodorow | 06f446a | 2015-04-06 15:54:03 +0000 | [diff] [blame] | 46 | $ cd ~/gitroot/my-project |
Kristina Chodorow | 3adbc49 | 2015-05-11 17:19:26 +0000 | [diff] [blame] | 47 | $ bazel fetch //... |
Lukacs Berki | d408df2 | 2015-03-25 12:51:17 +0000 | [diff] [blame] | 48 | $ bazel build examples/java-native/src/main/java/com/example/myproject:hello-world |
Kristina Chodorow | 3e6bd72 | 2015-03-19 14:30:24 +0000 | [diff] [blame] | 49 | Extracting Bazel installation... |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 50 | ........... |
| 51 | INFO: Found 1 target... |
Lukacs Berki | d408df2 | 2015-03-25 12:51:17 +0000 | [diff] [blame] | 52 | Target //examples/java-native/src/main/java/com/example/myproject:hello-world up-to-date: |
| 53 | bazel-bin/examples/java-native/src/main/java/com/example/myproject/hello-world.jar |
| 54 | bazel-bin/examples/java-native/src/main/java/com/example/myproject/hello-world |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 55 | INFO: Elapsed time: 3.040s, Critical Path: 1.14s |
Lukacs Berki | d408df2 | 2015-03-25 12:51:17 +0000 | [diff] [blame] | 56 | $ bazel-bin/examples/java-native/src/main/java/com/example/myproject/hello-world |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 57 | Hello world |
Googler | a0d555f | 2015-04-22 08:32:33 +0000 | [diff] [blame] | 58 | {% endhighlight %} |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 59 | |
Googler | 08050a3 | 2015-03-31 20:50:16 +0000 | [diff] [blame] | 60 | Bazel puts binaries it has built under `bazel-bin/`. Note that you can |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 61 | always look at the `build` command's output to find output file paths. |
| 62 | |
Googler | bed6245 | 2015-04-14 18:44:55 +0000 | [diff] [blame] | 63 | ## Creating Your Own Build File |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 64 | |
Googler | 08050a3 | 2015-03-31 20:50:16 +0000 | [diff] [blame] | 65 | Now you can create your own BUILD file and start adding build rules. This |
| 66 | example assumes that `my-project/` is a Java project. See the |
Han-Wen Nienhuys | 361af11 | 2015-03-17 13:00:09 +0000 | [diff] [blame] | 67 | [build encyclopedia](build-encyclopedia.html) |
Googler | 3a21f00 | 2015-03-18 21:52:07 +0000 | [diff] [blame] | 68 | for advice on adding build rules for other languages. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 69 | |
| 70 | Note that when we ran "bazel build" above, the third argument started with a |
| 71 | filesystem path ("examples/java"), followed by a colon. When you run |
Lukacs Berki | d408df2 | 2015-03-25 12:51:17 +0000 | [diff] [blame] | 72 | `bazel build examples/java-native/src/main/java/com/example/myproject:hello-world`, |
| 73 | Bazel will look for a special file named BUILD in the |
| 74 | `examples/java-native/src/main/java/com/example/myproject/` subdirectory. This |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 75 | BUILD file defines rules about how Bazel should build things in this |
| 76 | subdirectory. |
| 77 | |
Googler | 3a21f00 | 2015-03-18 21:52:07 +0000 | [diff] [blame] | 78 | Thus, to add build rules to my-project, create a file named `BUILD` in the |
Googler | 08050a3 | 2015-03-31 20:50:16 +0000 | [diff] [blame] | 79 | `my-project/` directory. Add the following lines to this BUILD file: |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 80 | |
Googler | a0d555f | 2015-04-22 08:32:33 +0000 | [diff] [blame] | 81 | {% highlight python %} |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 82 | # ~/gitroot/base_workspace/my-project/BUILD |
| 83 | java_binary( |
| 84 | name = "my-runner", |
| 85 | srcs = glob(["**/*.java"]), |
| 86 | main_class = "com.example.ProjectRunner", |
| 87 | ) |
Googler | a0d555f | 2015-04-22 08:32:33 +0000 | [diff] [blame] | 88 | {% endhighlight %} |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 89 | |
| 90 | BUILD files are Python-like scripts. BUILD files cannot contain arbitrary |
| 91 | Python, but each build rule looks like a Python function call and you can use |
| 92 | "#" to start a single-line comment. |
| 93 | |
| 94 | `java_binary` is the type of thing this rule will build. |
| 95 | `name` is how you'll refer to the rule when you run "bazel build" |
| 96 | (in the "examples/java:hello-world" build above the `name` was |
| 97 | "hello-world"). `srcs` lists the Java source files Bazel should |
| 98 | compile into a Java binary. `glob(["**/*.java"])` is a handy |
| 99 | shorthand for "recursively include every file that ends with .java" (see the |
Han-Wen Nienhuys | 361af11 | 2015-03-17 13:00:09 +0000 | [diff] [blame] | 100 | [user manual](bazel-user-manual.html) |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 101 | for more information about globbing). Replace `com.example.ProjectRunner` with |
| 102 | the class that contains the main method. |
| 103 | |
| 104 | If you have no actual Java project you're using, you can use the following |
| 105 | commands to make a fake project for this example: |
| 106 | |
Googler | a0d555f | 2015-04-22 08:32:33 +0000 | [diff] [blame] | 107 | {% highlight bash %} |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 108 | $ # If you're not already there, move to your build root directory. |
| 109 | $ cd ~/gitroot/base_workspace |
| 110 | $ mkdir -p my-project/java/com/example |
Ross Light | e7dd23c | 2015-03-19 16:37:19 +0000 | [diff] [blame] | 111 | $ cat > my-project/java/com/example/ProjectRunner.java <<EOF |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 112 | package com.example; |
| 113 | |
| 114 | public class ProjectRunner { |
| 115 | public static void main(String args[]) { |
| 116 | Greeting.sayHi(); |
| 117 | } |
| 118 | } |
| 119 | EOF |
Ross Light | e7dd23c | 2015-03-19 16:37:19 +0000 | [diff] [blame] | 120 | $ cat > my-project/java/com/example/Greeting.java <<EOF |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 121 | package com.example; |
| 122 | |
| 123 | public class Greeting { |
| 124 | public static void sayHi() { |
| 125 | System.out.println("Hi!"); |
| 126 | } |
| 127 | } |
| 128 | EOF |
Googler | a0d555f | 2015-04-22 08:32:33 +0000 | [diff] [blame] | 129 | {% endhighlight %} |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 130 | |
| 131 | Now build your project: |
| 132 | |
Googler | a0d555f | 2015-04-22 08:32:33 +0000 | [diff] [blame] | 133 | {% highlight bash %} |
Kristina Chodorow | 3adbc49 | 2015-05-11 17:19:26 +0000 | [diff] [blame] | 134 | $ bazel fetch my-project:my-runner |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 135 | $ bazel build my-project:my-runner |
| 136 | INFO: Found 1 target... |
| 137 | Target //my-project:my-runner up-to-date: |
| 138 | bazel-bin/my-project/my-runner.jar |
| 139 | bazel-bin/my-project/my-runner |
| 140 | INFO: Elapsed time: 1.021s, Critical Path: 0.83s |
| 141 | $ bazel-bin/my-project/my-runner |
| 142 | Hi! |
Googler | a0d555f | 2015-04-22 08:32:33 +0000 | [diff] [blame] | 143 | {% endhighlight %} |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 144 | |
Googler | 3a21f00 | 2015-03-18 21:52:07 +0000 | [diff] [blame] | 145 | Congratulations, you've created your first Bazel BUILD file! |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 146 | |
Googler | bed6245 | 2015-04-14 18:44:55 +0000 | [diff] [blame] | 147 | ## Adding Dependencies |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 148 | |
| 149 | Creating one rule to build your entire project may be sufficient for small |
| 150 | projects, but as projects get larger it's important to break up the build into |
| 151 | self-contained libraries that can be assembled into a final product. This way |
| 152 | the entire world doesn't need to be rebuilt on small changes and Bazel can |
| 153 | parallelize more of the build steps. |
| 154 | |
| 155 | To break up a project, create separate rules for each subcomponent and then |
| 156 | make them depend on each other. For the example above, add the following rules |
Googler | 08050a3 | 2015-03-31 20:50:16 +0000 | [diff] [blame] | 157 | to the `my-project/BUILD` file: |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 158 | |
Googler | a0d555f | 2015-04-22 08:32:33 +0000 | [diff] [blame] | 159 | {% highlight python %} |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 160 | java_binary( |
| 161 | name = "my-other-runner", |
| 162 | srcs = ["java/com/example/ProjectRunner.java"], |
| 163 | main_class = "com.example.ProjectRunner", |
| 164 | deps = [":greeter"], |
| 165 | ) |
| 166 | |
| 167 | java_library( |
| 168 | name = "greeter", |
| 169 | srcs = ["java/com/example/Greeting.java"], |
| 170 | ) |
Googler | a0d555f | 2015-04-22 08:32:33 +0000 | [diff] [blame] | 171 | {% endhighlight %} |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 172 | |
| 173 | Now you can build and run `my-project:my-other-runner`: |
| 174 | |
Googler | a0d555f | 2015-04-22 08:32:33 +0000 | [diff] [blame] | 175 | {% highlight bash %} |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 176 | $ bazel run my-project:my-other-runner |
| 177 | INFO: Found 1 target... |
| 178 | Target //my-project:my-other-runner up-to-date: |
| 179 | bazel-bin/my-project/my-other-runner.jar |
| 180 | bazel-bin/my-project/my-other-runner |
| 181 | INFO: Elapsed time: 2.454s, Critical Path: 1.58s |
| 182 | |
| 183 | INFO: Running command line: bazel-bin/my-project/my-other-runner |
| 184 | Hi! |
Googler | a0d555f | 2015-04-22 08:32:33 +0000 | [diff] [blame] | 185 | {% endhighlight %} |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 186 | |
| 187 | If you edit _ProjectRunner.java_ and rebuild `my-other-runner`, only |
Googler | 08050a3 | 2015-03-31 20:50:16 +0000 | [diff] [blame] | 188 | `ProjectRunner.java` needs to be rebuilt (<code>greeter</code> is unchanged). |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 189 | |
Googler | bed6245 | 2015-04-14 18:44:55 +0000 | [diff] [blame] | 190 | ## Using Multiple Packages |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 191 | |
| 192 | For larger projects, you will often be dealing with several directories. You |
| 193 | can refer to targets defined in other BUILD files using the syntax |
| 194 | `//package-name:target-name`. For example, suppose |
Googler | 08050a3 | 2015-03-31 20:50:16 +0000 | [diff] [blame] | 195 | `my-project/java/com/example/` has a `cmdline/` subdirectory with the following |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 196 | file: |
| 197 | |
Googler | a0d555f | 2015-04-22 08:32:33 +0000 | [diff] [blame] | 198 | {% highlight bash %} |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 199 | $ mkdir my-project/java/com/example/cmdline |
Ross Light | e7dd23c | 2015-03-19 16:37:19 +0000 | [diff] [blame] | 200 | $ cat > my-project/java/com/example/cmdline/Runner.java <<EOF |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 201 | package com.example.cmdline; |
| 202 | |
| 203 | import com.example.Greeting; |
| 204 | |
| 205 | public class Runner { |
| 206 | public static void main(String args[]) { |
| 207 | Greeting.sayHi(); |
| 208 | } |
| 209 | } |
| 210 | EOF |
Googler | a0d555f | 2015-04-22 08:32:33 +0000 | [diff] [blame] | 211 | {% endhighlight %} |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 212 | |
Googler | 08050a3 | 2015-03-31 20:50:16 +0000 | [diff] [blame] | 213 | We could add a `BUILD` file at `my-project/java/com/example/cmdline/BUILD` |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 214 | that contained the following rule: |
| 215 | |
Googler | a0d555f | 2015-04-22 08:32:33 +0000 | [diff] [blame] | 216 | {% highlight python %} |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 217 | # ~/gitroot/base_workspace/my-project/java/com/example/cmdline/BUILD |
| 218 | java_binary( |
| 219 | name = "runner", |
| 220 | srcs = ["Runner.java"], |
| 221 | main_class = "com.example.cmdline.Runner", |
| 222 | deps = ["//my-project:greeter"] |
| 223 | ) |
Googler | a0d555f | 2015-04-22 08:32:33 +0000 | [diff] [blame] | 224 | {% endhighlight %} |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 225 | |
| 226 | However, by default, build rules are _private_. This means that they can only be |
| 227 | referred to by rules in the same BUILD file. This prevents libraries that are |
| 228 | implementation details from leaking into public APIs, but it also means that you |
| 229 | must explicitly allow `runner` to depend on `my-project:greeter`. As is, if we |
| 230 | build `runner` we'll get a permissions error: |
| 231 | |
Googler | a0d555f | 2015-04-22 08:32:33 +0000 | [diff] [blame] | 232 | {% highlight bash %} |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 233 | $ bazel build my-project/java/com/example/cmdline:runner |
| 234 | ERROR: /usr/local/google/home/kchodorow/gitroot/base_workspace/my-project/java/com/example/cmdline/BUILD:2:1: |
| 235 | Target '//my-project:greeter' is not visible from target '//my-project/java/com/example/cmdline:runner'. |
| 236 | Check the visibility declaration of the former target if you think the dependency is legitimate. |
| 237 | ERROR: Analysis of target '//my-project/java/com/example/cmdline:runner' failed; build aborted. |
| 238 | INFO: Elapsed time: 0.091s |
Googler | a0d555f | 2015-04-22 08:32:33 +0000 | [diff] [blame] | 239 | {% endhighlight %} |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 240 | |
| 241 | You can make a rule visibile to rules in other BUILD files by adding a |
| 242 | `visibility = level` attribute. Change the `greeter` rule in |
| 243 | _my-project/BUILD_ to be visible to our new rule: |
| 244 | |
Googler | a0d555f | 2015-04-22 08:32:33 +0000 | [diff] [blame] | 245 | {% highlight python %} |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 246 | java_library( |
| 247 | name = "greeter", |
| 248 | srcs = ["java/com/example/Greeting.java"], |
| 249 | visibility = ["//my-project/java/com/example/cmdline:__pkg__"], |
| 250 | ) |
Googler | a0d555f | 2015-04-22 08:32:33 +0000 | [diff] [blame] | 251 | {% endhighlight %} |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 252 | |
| 253 | This makes `//my-project:greeter` visible to any rule in the |
| 254 | `//my-project/java/com/example/cmdline` package. Now we can build and |
| 255 | run the binary: |
| 256 | |
Googler | a0d555f | 2015-04-22 08:32:33 +0000 | [diff] [blame] | 257 | {% highlight bash %} |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 258 | $ bazel run my-project/java/com/example/cmdline:runner |
| 259 | INFO: Found 1 target... |
| 260 | Target //my-project/java/com/example/cmdline:runner up-to-date: |
| 261 | bazel-bin/my-project/java/com/example/cmdline/runner.jar |
| 262 | bazel-bin/my-project/java/com/example/cmdline/runner |
| 263 | INFO: Elapsed time: 1.576s, Critical Path: 0.81s |
| 264 | |
| 265 | INFO: Running command line: bazel-bin/my-project/java/com/example/cmdline/runner |
| 266 | Hi! |
Googler | a0d555f | 2015-04-22 08:32:33 +0000 | [diff] [blame] | 267 | {% endhighlight %} |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 268 | |
Han-Wen Nienhuys | 361af11 | 2015-03-17 13:00:09 +0000 | [diff] [blame] | 269 | See the [build encyclopedia](build-encyclopedia.html) for more visibility options. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 270 | |
Googler | bed6245 | 2015-04-14 18:44:55 +0000 | [diff] [blame] | 271 | ## Deploying |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 272 | |
| 273 | If you look at the contents of |
| 274 | _bazel-bin/my-project/java/com/example/cmdline/runner.jar_, you can see that it |
| 275 | only contains `Runner.class`, not its dependencies (`Greeting.class`): |
| 276 | |
Googler | a0d555f | 2015-04-22 08:32:33 +0000 | [diff] [blame] | 277 | {% highlight bash %} |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 278 | $ jar tf bazel-bin/my-project/java/com/example/cmdline/runner.jar |
| 279 | META-INF/ |
| 280 | META-INF/MANIFEST.MF |
| 281 | com/ |
| 282 | com/example/ |
| 283 | com/example/cmdline/ |
| 284 | com/example/cmdline/Runner.class |
Googler | a0d555f | 2015-04-22 08:32:33 +0000 | [diff] [blame] | 285 | {% endhighlight %} |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 286 | |
| 287 | To deploy a `runner` binary, we need a self-contained jar. To build this, build |
| 288 | runner_deploy.jar (or, more generally, _<target-name>_deploy.jar_): |
| 289 | |
Googler | a0d555f | 2015-04-22 08:32:33 +0000 | [diff] [blame] | 290 | {% highlight bash %} |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 291 | $ bazel build my-project/java/com/example/cmdline:runner_deploy.jar |
| 292 | INFO: Found 1 target... |
| 293 | Target //my-project/java/com/example/cmdline:runner_deploy.jar up-to-date: |
| 294 | bazel-bin/my-project/java/com/example/cmdline/runner_deploy.jar |
| 295 | INFO: Elapsed time: 1.700s, Critical Path: 0.23s |
Googler | a0d555f | 2015-04-22 08:32:33 +0000 | [diff] [blame] | 296 | {% endhighlight %} |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 297 | |
| 298 | `runner_deploy.jar` will contain all of its dependencies. |
| 299 | |
Googler | bed6245 | 2015-04-14 18:44:55 +0000 | [diff] [blame] | 300 | ## Next Steps |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 301 | |
| 302 | You can now create your own targets and compose them. See the [build |
Han-Wen Nienhuys | 361af11 | 2015-03-17 13:00:09 +0000 | [diff] [blame] | 303 | encyclopedia](build-encyclopedia.html) |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 304 | and Bazel |
Han-Wen Nienhuys | 361af11 | 2015-03-17 13:00:09 +0000 | [diff] [blame] | 305 | [user manual](bazel-user-manual.html) |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 306 | for more information. |
| 307 | [Let us know](https://groups.google.com/forum/#!forum/bazel-discuss) |
| 308 | if you have any questions! |