|
1 |
| -# Copyright 2017 The Bazel Authors. All rights reserved. |
2 |
| -# |
3 |
| -# Licensed under the Apache License, Version 2.0 (the "License"); |
4 |
| -# you may not use this file except in compliance with the License. |
5 |
| -# You may obtain a copy of the License at |
6 |
| -# |
7 |
| -# http://www.apache.org/licenses/LICENSE-2.0 |
8 |
| -# |
9 |
| -# Unless required by applicable law or agreed to in writing, software |
10 |
| -# distributed under the License is distributed on an "AS IS" BASIS, |
11 |
| -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 |
| -# See the License for the specific language governing permissions and |
13 |
| -# limitations under the License. |
14 |
| - |
15 |
| -"""Rules for defining external Java dependencies. |
16 |
| -
|
17 |
| -java_import_external() replaces `maven_jar` and `http_jar`. It is the |
18 |
| -recommended solution for defining third party Java dependencies that are |
19 |
| -obtained from web servers. |
20 |
| -
|
21 |
| -This solution offers high availability, low latency, and repository |
22 |
| -scalability at the cost of simplicity. Tooling can be used to generate |
23 |
| -
|
24 |
| -The default target in this BUILD file will always have the same name as |
25 |
| -the repository itself. This means that other Bazel rules can depend on |
26 |
| -it as `@repo//:repo` or `@repo` for short. |
27 |
| -
|
28 |
| -### Setup |
29 |
| -
|
30 |
| -Add the following to your `WORKSPACE` file: |
31 |
| -
|
32 |
| -```python |
33 |
| -load("@bazel_tools//tools/build_defs/repo:java.bzl", "java_import_external") |
34 |
| -``` |
35 |
| -
|
36 |
| -### Best Practices |
37 |
| -
|
38 |
| -#### Downloading |
39 |
| -
|
40 |
| -The recommended best practices for downloading Maven jars are as follows: |
41 |
| -
|
42 |
| -1. Always follow release versions or pinned revisions. |
43 |
| -2. Permanently mirror all dependencies to GCS or S3 as the first URL |
44 |
| -3. Put the original URL in the GCS or S3 object name |
45 |
| -4. Make the second URL the original repo1.maven.org URL |
46 |
| -5. Make the third URL the maven.ibiblio.org mirror, if it isn't 404 |
47 |
| -6. Always specify the sha256 checksum |
48 |
| -
|
49 |
| -Bazel has one of the most sophisticated systems for downloading files of any |
50 |
| -build system. Following these best practices will ensure that your codebase |
51 |
| -takes full advantage of the level of reliability that Bazel able to offer. See |
52 |
| -https://goo.gl/uQOE11 for more information. |
53 |
| -
|
54 |
| -#### Selection |
55 |
| -
|
56 |
| -Avoid using jars that bundle their dependencies. For example, a Maven jar for |
57 |
| -the artifact com.initech:tps:1.0 should not contain a classes named |
58 |
| -com.fakecorp.foo. Try to see if Initech distributes a tps jar that doesn't |
59 |
| -bundle its dependencies. Then create a separate java_import_external() for each |
60 |
| -one and have the first depend on the second. |
61 |
| -
|
62 |
| -Sometimes jars are distributed with their dependencies shaded. What this means |
63 |
| -is that com.initech.tps will contain classes like |
64 |
| -com.initech.tps.shade.com.fakecorp.foo. This is less problematic, since it |
65 |
| -won't lead to mysterious classpath conflicts. But it can lead to inefficient |
66 |
| -use of space and make the license of the the end product more difficult to |
67 |
| -determine. |
68 |
| -
|
69 |
| -#### Licensing |
70 |
| -
|
71 |
| -The following values for the licenses field are typically used. If a jar |
72 |
| -contains multiple works with difference licenses, then only the most |
73 |
| -restrictive one is listed, and the rest are noted in accompanying comments. |
74 |
| -
|
75 |
| -The following are examples of how licenses could be categorized, ordered |
76 |
| -by those with terms most permissive to least: |
77 |
| -
|
78 |
| -- **unencumbered**: CC0, Unlicense |
79 |
| -- **permissive**: Beerware |
80 |
| -- **notice**: Apache, MIT, X11, BSD, ISC, ZPL, Unicode, JSON, Artistic |
81 |
| -- **reciprocal**: MPL, CPL, EPL, Eclipse, APSL, IBMPL, CDDL |
82 |
| -- **restricted**: GPL, LGPL, OSL, Sleepycat, QTPL, Java, QMail, NPL |
83 |
| -- **by_exception_only**: AGPL, WTFPL |
84 |
| -
|
85 |
| -### Naming |
86 |
| -
|
87 |
| -Bazel repository names must match the following pattern: `[_0-9A-Za-z]+`. To |
88 |
| -choose an appropriate name based on a Maven group and artifact ID, we recommend |
89 |
| -an algorithm https://gist.github.com/jart/41bfd977b913c2301627162f1c038e55 which |
90 |
| -can be best explained by the following examples: |
91 |
| -
|
92 |
| -- com.google.guava:guava becomes com_google_guava |
93 |
| -- commons-logging:commons-logging becomes commons_logging |
94 |
| -- junit:junit becomes junit |
95 |
| -
|
96 |
| -Adopting this naming convention will help maximize the chances that your |
97 |
| -codebase will be able to successfully interoperate with other Bazel codebases |
98 |
| -using Java. |
99 |
| -
|
100 |
| -### Example |
101 |
| -
|
102 |
| -Here is an example of a best practice definition of Google's Guava library: |
103 |
| -
|
104 |
| -```python |
105 |
| -java_import_external( |
106 |
| - name = "com_google_guava", |
107 |
| - licenses = ["notice"], # Apache 2.0 |
108 |
| - jar_urls = [ |
109 |
| - "http://bazel-mirror.storage.googleapis.com/repo1.maven.org/maven2/com/google/guava/guava/20.0/guava-20.0.jar", |
110 |
| - "http://repo1.maven.org/maven2/com/google/guava/guava/20.0/guava-20.0.jar", |
111 |
| - "http://maven.ibiblio.org/maven2/com/google/guava/guava/20.0/guava-20.0.jar", |
112 |
| - ], |
113 |
| - jar_sha256 = "36a666e3b71ae7f0f0dca23654b67e086e6c93d192f60ba5dfd5519db6c288c8", |
114 |
| - deps = [ |
115 |
| - "@com_google_code_findbugs_jsr305", |
116 |
| - "@com_google_errorprone_error_prone_annotations", |
117 |
| - ], |
118 |
| -) |
119 |
| -
|
120 |
| -java_import_external( |
121 |
| - name = "com_google_code_findbugs_jsr305", |
122 |
| - licenses = ["notice"], # BSD 3-clause |
123 |
| - jar_urls = [ |
124 |
| - "http://bazel-mirror.storage.googleapis.com/repo1.maven.org/maven2/com/google/code/findbugs/jsr305/1.3.9/jsr305-1.3.9.jar", |
125 |
| - "http://repo1.maven.org/maven2/com/google/code/findbugs/jsr305/1.3.9/jsr305-1.3.9.jar", |
126 |
| - "http://maven.ibiblio.org/maven2/com/google/code/findbugs/jsr305/1.3.9/jsr305-1.3.9.jar", |
127 |
| - ], |
128 |
| - jar_sha256 = "905721a0eea90a81534abb7ee6ef4ea2e5e645fa1def0a5cd88402df1b46c9ed", |
129 |
| -) |
130 |
| -
|
131 |
| -java_import_external( |
132 |
| - name = "com_google_errorprone_error_prone_annotations", |
133 |
| - licenses = ["notice"], # Apache 2.0 |
134 |
| - jar_sha256 = "e7749ffdf03fb8ebe08a727ea205acb301c8791da837fee211b99b04f9d79c46", |
135 |
| - jar_urls = [ |
136 |
| - "http://bazel-mirror.storage.googleapis.com/repo1.maven.org/maven2/com/google/errorprone/error_prone_annotations/2.0.15/error_prone_annotations-2.0.15.jar", |
137 |
| - "http://maven.ibiblio.org/maven2/com/google/errorprone/error_prone_annotations/2.0.15/error_prone_annotations-2.0.15.jar", |
138 |
| - "http://repo1.maven.org/maven2/com/google/errorprone/error_prone_annotations/2.0.15/error_prone_annotations-2.0.15.jar", |
139 |
| - ], |
140 |
| -) |
141 |
| -``` |
142 |
| -
|
143 |
| -### Annotation Processors |
144 |
| -
|
145 |
| -Defining jars that contain annotation processors requires a certain level of |
146 |
| -trickery, which is best done by copying and pasting from codebases that have |
147 |
| -already done it before. Please see the Google Nomulus and Bazel Closure Rules |
148 |
| -codebases for examples in which java_import_external has been used to define |
149 |
| -Dagger 2.0, AutoValue, and AutoFactory. |
150 |
| -
|
151 |
| -Please note that certain care needs to be taken into consideration regarding |
152 |
| -whether or not these annotation processors generate actual API, or simply |
153 |
| -generate code that implements them. See the Bazel documentation for further |
154 |
| -information. |
155 |
| -
|
156 |
| -### Test Dependencies |
157 |
| -
|
158 |
| -It is strongly recommended that the `testonly_` attribute be specified on |
159 |
| -libraries that are intended for testing purposes. This is passed along to the |
160 |
| -generated `java_library` rule in order to ensure that test code remains |
161 |
| -disjoint from production code. |
162 |
| -
|
163 |
| -### Provided Dependencies |
164 |
| -
|
165 |
| -The feature in Bazel most analagous to Maven's provided scope is the neverlink |
166 |
| -attribute. This should be used in rare circumstances when a distributed jar |
167 |
| -will be loaded into a runtime environment where certain dependencies can be |
168 |
| -reasonably expected to already be provided. |
| 1 | +""" |
| 2 | +'jvm_import_external' offers additional functionality above what maven_jar has to offer. |
| 3 | +In addition to downloading the jars, it allows to define this jar's dependencies. |
| 4 | +thus it enables the explicit definition of the entire transitive dependency graph. |
| 5 | +
|
| 6 | +The rule achieves this by writing 'import' build rules in BUILD files next to the downloaded jars. |
| 7 | +The name of the underlying 'import' rule needs to be specified. |
| 8 | +An optional 'load' statement can also be provided, along with any other relevant custom attribute. |
| 9 | +These import rules must have the following attributes: |
| 10 | +- "jars" |
| 11 | +- "deps" |
| 12 | +- "runtime_deps" |
| 13 | +- "exports" |
| 14 | +
|
| 15 | +the code here is solely based on `jave_import_external` from bazelbuild/bazel repository and is proposed to be upstreamed back. |
| 16 | +
|
| 17 | +the following macros are defined below that utilize jvm_import_external: |
| 18 | +
|
| 19 | +- scala_import_external - uses `scala_import` as the underlying build rule |
| 20 | +- jvm_maven_import_external - offers a 'maven' like api for identifying jars using 'artifact' format |
| 21 | +- scala_maven_import_external - combination of scala_import_external and jvm_maven_import_external |
| 22 | +- java_import_external - to demonstrate that the original functionality of `java_import_external` stayed intact. |
169 | 23 | """
|
170 | 24 |
|
171 | 25 | _HEADER = "# DO NOT EDIT: generated by jvm_import_external()"
|
|
0 commit comments