Googler | 032aab2 | 2017-09-14 16:49:56 +0200 | [diff] [blame] | 1 | --- |
| 2 | layout: documentation |
| 3 | title: Java and Bazel |
| 4 | --- |
| 5 | |
| 6 | # Java and Bazel |
| 7 | |
| 8 | This page contains resources that help you use Bazel with Java projects. It |
| 9 | links to a tutorial, build rules, and other information specific to building |
| 10 | Java projects with Bazel. |
| 11 | |
| 12 | ## Contents |
| 13 | |
| 14 | - [Working with Bazel](#working-with-bazel) |
| 15 | - [Migrating to Bazel](#migrating-to-bazel) |
| 16 | - [Best practices](#best-practices) |
| 17 | - [Directory structure](#directory-structure) |
| 18 | - [BUILD files](#build-files) |
laurentlb | 749165f | 2017-12-14 03:41:55 -0800 | [diff] [blame] | 19 | - [Java and new rules](#java-and-new-rules) |
Googler | 032aab2 | 2017-09-14 16:49:56 +0200 | [diff] [blame] | 20 | |
| 21 | ## Working with Bazel |
| 22 | |
| 23 | The following resources will help you work with Bazel on Java projects: |
| 24 | |
| 25 | * [Tutorial: Building a Java Project](tutorial/java.html) |
Jingwen Chen | 0f4544d | 2018-12-14 16:28:16 -0800 | [diff] [blame] | 26 | * [Java rules](be/java.html) |
Googler | 032aab2 | 2017-09-14 16:49:56 +0200 | [diff] [blame] | 27 | |
| 28 | ## Migrating to Bazel |
| 29 | |
| 30 | If you currently build your Java projects with Maven, follow the steps in the |
| 31 | migration guide to start building your Maven projects with Bazel: |
| 32 | |
| 33 | * [Migrating from Maven to Bazel](migrate-maven.html) |
| 34 | |
| 35 | ## Best practices |
| 36 | |
| 37 | In addition to [general Bazel best practices](best-practices.html), below are |
| 38 | best practices specific to Java projects. |
| 39 | |
| 40 | ### Directory structure |
| 41 | |
| 42 | Prefer Maven's standard directory layout (sources under `src/main/java`, tests |
| 43 | under `src/test/java`). |
| 44 | |
| 45 | ### BUILD files |
| 46 | |
| 47 | Follow these guidelines when creating your BUILD files: |
| 48 | |
| 49 | * Use one BUILD file per package containing Java sources. |
| 50 | |
| 51 | * Every BUILD file should contain one `java_library` rule that looks like this: |
| 52 | |
| 53 | ```python |
| 54 | java_library( |
| 55 | name = "directory-name", |
| 56 | srcs = glob(["*.java"]), |
Alpha | 0cfc7b2 | 2018-05-02 05:04:18 -0700 | [diff] [blame] | 57 | deps = [...], |
Googler | 032aab2 | 2017-09-14 16:49:56 +0200 | [diff] [blame] | 58 | ) |
| 59 | ``` |
| 60 | * The name of the library should be the name of the directory containing the |
| 61 | BUILD file. |
| 62 | |
Jingwen Chen | 0f4544d | 2018-12-14 16:28:16 -0800 | [diff] [blame] | 63 | * The sources should be a non-recursive [`glob`](be/functions.html#glob) |
Googler | 032aab2 | 2017-09-14 16:49:56 +0200 | [diff] [blame] | 64 | of all Java files in the directory. |
| 65 | |
| 66 | * Tests should be in a matching directory under `src/test` and depend on this |
| 67 | library. |
| 68 | |
laurentlb | 749165f | 2017-12-14 03:41:55 -0800 | [diff] [blame] | 69 | ## Java and new rules |
Googler | 032aab2 | 2017-09-14 16:49:56 +0200 | [diff] [blame] | 70 | |
laurentlb | 749165f | 2017-12-14 03:41:55 -0800 | [diff] [blame] | 71 | **Note**: Creating new rules is for advanced build and test scenarios. |
| 72 | You do not need it when getting started with Bazel. |
Googler | 032aab2 | 2017-09-14 16:49:56 +0200 | [diff] [blame] | 73 | |
laurentlb | 749165f | 2017-12-14 03:41:55 -0800 | [diff] [blame] | 74 | The following modules, configuration fragments, and providers will help you |
Jingwen Chen | 0f4544d | 2018-12-14 16:28:16 -0800 | [diff] [blame] | 75 | [extend Bazel's capabilities](skylark/concepts.html) |
laurentlb | 749165f | 2017-12-14 03:41:55 -0800 | [diff] [blame] | 76 | when building your Java projects: |
Googler | 032aab2 | 2017-09-14 16:49:56 +0200 | [diff] [blame] | 77 | |
| 78 | * Modules: |
| 79 | |
| 80 | * [`java_annotation_processing`](skylark/lib/java_annotation_processing.html) |
| 81 | * [`java_common`](skylark/lib/java_common.html) |
| 82 | * [`java_compilation_info`](skylark/lib/java_compilation_info.html) |
| 83 | * [`java_output`](skylark/lib/java_output.html) |
| 84 | * [`java_output_jars`](skylark/lib/java_output_jars.html) |
| 85 | * [`java_proto_common`](skylark/lib/java_proto_common.html) |
| 86 | * [`JavaRuntimeClasspathProvider`](skylark/lib/JavaRuntimeClasspathProvider.html) |
| 87 | * [`JavaRuntimeInfo`](skylark/lib/JavaRuntimeInfo.html) |
| 88 | * [`JavaToolchainSkylarkApiProvider`](skylark/lib/JavaToolchainSkylarkApiProvider.html) |
| 89 | |
| 90 | * Configuration fragments: |
| 91 | |
| 92 | * [`java`](skylark/lib/java.html) |
| 93 | |
| 94 | * Providers: |
| 95 | |
| 96 | * [`java`](skylark/lib/JavaSkylarkApiProvider.html) |
| 97 | * [`JavaInfo`](skylark/lib/JavaInfo.html) |