cushon | 58dc6b9 | 2017-07-06 17:04:27 -0400 | [diff] [blame] | 1 | --- |
| 2 | layout: posts |
| 3 | title: Configuring your Java builds |
| 4 | --- |
| 5 | |
| 6 | Let say that you want to build for Java 8 and Error Prone checks off but keep |
| 7 | the tools directory provided with Bazel in the package path, you could do that |
| 8 | by having the following rc file: |
| 9 | |
| 10 | ``` |
| 11 | build --javacopt=-XepDisableAllChecks |
| 12 | build --javacopt="-source 8 -target 8" |
| 13 | ``` |
| 14 | |
| 15 | However, the file would becomes quickly overloaded, especially if you take |
| 16 | all languages and options into account. Instead, you can tweak the |
| 17 | [java_toolchain](https://github.com/bazelbuild/bazel/tree/0e1680e58f01f3d443f7e68865b5a56b76c9dadf/tools/jdk/BUILD#L73) |
| 18 | rule that specifies the various options for the java compiler. So in a |
| 19 | BUILD file: |
| 20 | |
| 21 | ```python |
| 22 | java_toolchain( |
| 23 | name = "my_toolchain", |
| 24 | encoding = "UTF-8", |
| 25 | source_version = "8", |
| 26 | target_version = "8", |
| 27 | misc = [ |
Googler | 24e1409 | 2018-05-04 16:06:27 -0700 | [diff] [blame] | 28 | "-Xep:CollectionIncompatibleType:ERROR", # https://errorprone.info/bugpattern/CollectionIncompatibleType |
cushon | 58dc6b9 | 2017-07-06 17:04:27 -0400 | [diff] [blame] | 29 | ], |
| 30 | ) |
| 31 | ``` |
| 32 | |
| 33 | And to keep it out of the tools directory (or you need to copy the rest |
| 34 | of the package), you can redirect the default one in a bazelrc: |
| 35 | |
| 36 | ``` |
| 37 | build --java_toolchain=//package:my_toolchain |
| 38 | ``` |
| 39 | |
| 40 | In the future, toolchain rules should be the configuration points for all |
| 41 | the languages but it is a long road. We also want to make it easier to |
| 42 | rebind the toolchain using the `bind` rule in the WORKSPACE file. |
| 43 | |