blob: c3237ed22c870489059afd423787b1f03c259caf [file] [log] [blame]
Marian Harbachebeb1542019-12-13 10:42:46 +01001:linkattrs:
Han-Wen Nienhuys80fcbe12016-09-22 13:16:51 +02002= Gerrit Code Review - Building with Bazel
3
Han-Wen Nienhuys1e318032020-01-08 16:53:08 +01004[[summary]]
5== TL;DR
6
7If you have the prerequisites, running
8
9```
10 $ bazel build gerrit
11```
12
13should generate a .war file under `bazel-bin/gerrit.war`.
14
Alice Kober-Sotzekd9da89f2016-11-25 12:02:18 +010015[[installation]]
Olivier Croquettea9091192018-01-14 20:20:56 +010016== Prerequisites
Han-Wen Nienhuys80fcbe12016-09-22 13:16:51 +020017
Olivier Croquettea9091192018-01-14 20:20:56 +010018To build Gerrit from source, you need:
Han-Wen Nienhuys80fcbe12016-09-22 13:16:51 +020019
Olivier Croquettea9091192018-01-14 20:20:56 +010020* A Linux or macOS system (Windows is not supported at this time)
David Ostrovskyb89332d2020-04-27 07:06:24 +020021* A JDK for Java 8|11|...
David Ostrovsky2b5fe092021-03-03 11:52:30 +010022* Python 3
Marian Harbach34253372019-12-10 18:01:31 +010023* link:https://github.com/nodesource/distributions/blob/master/README.md[Node.js (including npm),role=external,window=_blank]
Ben Rohlfs11876a42020-04-27 10:16:51 +020024* Bower (`npm install -g bower`)
David Pursehousebfe2d142020-01-29 18:02:53 +090025* link:https://docs.bazel.build/versions/master/install.html[Bazel,role=external,window=_blank] -launched with
26link:https://github.com/bazelbuild/bazelisk[Bazelisk,role=external,window=_blank]
Han-Wen Nienhuysf66222f2018-04-18 14:11:40 +020027* Maven
28* zip, unzip
Christian Aistleitner53345062020-04-27 14:37:08 +020029* curl
Han-Wen Nienhuysf66222f2018-04-18 14:11:40 +020030* gcc
Han-Wen Nienhuys80fcbe12016-09-22 13:16:51 +020031
Marco Miller9bede912020-01-27 17:38:40 -050032[[bazel]]
33=== Bazel
34
Ben Rohlfs11876a42020-04-27 10:16:51 +020035link:https://github.com/bazelbuild/bazelisk[Bazelisk,role=external,window=_blank] is a version
36manager for link:https://bazel.build/[Bazel,role=external,window=_blank], similar to how `nvm`
37manages `npm` versions. It takes care of downloading and installing Bazel itself, so you don't have
David Ostrovsky8ee67742020-05-14 08:23:39 +020038to worry about using the correct version of Bazel. Bazelisk can be installed in different
39ways: link:https://docs.bazel.build/install-bazelisk.html[Install,role=external,window=_blank]
Marco Miller9bede912020-01-27 17:38:40 -050040
David Pursehousec8b633d2019-05-08 08:36:18 +090041[[java]]
42=== Java
43
Matthias Sohn82786672021-02-25 17:19:53 +010044Ensure that the link:install.html#Requirements[required Java version]
45is installed and that `JAVA_HOME` is set to it.
Alice Kober-Sotzeka8029b62019-05-07 19:13:00 +020046
47To check the installed version of Java, open a terminal window and run:
48
49`java -version`
50
David Ostrovskyb89332d2020-04-27 07:06:24 +020051[[java-8]]
52==== Java 8 support (deprecated)
53
54Java 8 is a legacy Java release and support for Java 8 will be discontinued
55in future gerrit releases. To build Gerrit with Java 8 language level, run:
56
57```
Luca Milanesiobef704e2020-11-05 11:29:36 +000058 $ bazel build --java_toolchain //tools:error_prone_warnings_toolchain :release
David Ostrovskyb89332d2020-04-27 07:06:24 +020059```
60
61[[java-11]]
62==== Java 11 support
63
64Java language level 11 is the default. To build Gerrit with Java 11 language
65level, run:
66
67```
68 $ bazel build :release
69```
70
David Ostrovskyef561282019-11-12 14:49:13 -080071[[java-13]]
72==== Java 13 support
David Pursehousec8b633d2019-05-08 08:36:18 +090073
David Ostrovskyef561282019-11-12 14:49:13 -080074Java 13 (and newer) is supported through vanilla java toolchain
Marian Harbach34253372019-12-10 18:01:31 +010075link:https://docs.bazel.build/versions/master/toolchains.html[Bazel option,role=external,window=_blank].
David Ostrovskyef561282019-11-12 14:49:13 -080076To build Gerrit with Java 13 and newer, specify vanilla java toolchain and
David Ostrovsky0bceb042018-08-25 07:37:54 +020077provide the path to JDK home:
David Ostrovskyf55b69f2018-08-23 08:25:24 +020078
79```
David Ostrovsky7e1fcd92018-11-03 08:48:47 +010080 $ bazel build \
David Ostrovskyef561282019-11-12 14:49:13 -080081 --define=ABSOLUTE_JAVABASE=<path-to-java-13> \
David Ostrovsky5f45e822019-09-26 17:19:27 +020082 --javabase=@bazel_tools//tools/jdk:absolute_javabase \
David Ostrovsky7e1fcd92018-11-03 08:48:47 +010083 --host_javabase=@bazel_tools//tools/jdk:absolute_javabase \
David Ostrovsky17b60d92019-11-10 12:25:53 -080084 --host_java_toolchain=@bazel_tools//tools/jdk:toolchain_vanilla \
85 --java_toolchain=@bazel_tools//tools/jdk:toolchain_vanilla \
David Ostrovskyf55b69f2018-08-23 08:25:24 +020086 :release
87```
88
David Ostrovsky0bceb042018-08-25 07:37:54 +020089To run the tests, `--javabase` option must be passed as well, because
90bazel test runs the test using the target javabase:
91
92```
David Ostrovsky7e1fcd92018-11-03 08:48:47 +010093 $ bazel test \
David Ostrovskyef561282019-11-12 14:49:13 -080094 --define=ABSOLUTE_JAVABASE=<path-to-java-13> \
David Ostrovsky7e1fcd92018-11-03 08:48:47 +010095 --javabase=@bazel_tools//tools/jdk:absolute_javabase \
96 --host_javabase=@bazel_tools//tools/jdk:absolute_javabase \
David Ostrovsky17b60d92019-11-10 12:25:53 -080097 --host_java_toolchain=@bazel_tools//tools/jdk:toolchain_vanilla \
98 --java_toolchain=@bazel_tools//tools/jdk:toolchain_vanilla \
David Ostrovsky0bceb042018-08-25 07:37:54 +020099 //...
100```
101
102To avoid passing all those options on every Bazel build invocation,
103they could be added to ~/.bazelrc resource file:
104
105```
106$ cat << EOF > ~/.bazelrc
Marco Millerd1839872020-09-03 17:46:24 -0400107build --define=ABSOLUTE_JAVABASE=<path-to-java-13>
Orgad Shaneh7af0efd2020-08-20 22:21:23 +0300108build --javabase=@bazel_tools//tools/jdk:absolute_javabase
109build --host_javabase=@bazel_tools//tools/jdk:absolute_javabase
110build --host_java_toolchain=@bazel_tools//tools/jdk:toolchain_vanilla
111build --java_toolchain=@bazel_tools//tools/jdk:toolchain_vanilla
112EOF
David Ostrovsky0bceb042018-08-25 07:37:54 +0200113```
114
115Now, invoking Bazel with just `bazel build :release` would include
116all those options.
117
Alice Kober-Sotzek79290dc2019-05-07 19:26:18 +0200118=== Node.js and npm packages
Marian Harbach34253372019-12-10 18:01:31 +0100119See link:https://gerrit.googlesource.com/gerrit/+/master/polygerrit-ui/README.md#installing-node_js-and-npm-packages[Installing Node.js and npm packages,role=external,window=_blank].
Alice Kober-Sotzek79290dc2019-05-07 19:26:18 +0200120
Han-Wen Nienhuys80fcbe12016-09-22 13:16:51 +0200121[[build]]
122== Building on the Command Line
123
124=== Gerrit Development WAR File
125
David Ostrovsky7163dac2017-07-29 06:49:38 +0200126To build the Gerrit web application:
Han-Wen Nienhuys80fcbe12016-09-22 13:16:51 +0200127
128----
129 bazel build gerrit
130----
131
Han-Wen Nienhuys80fcbe12016-09-22 13:16:51 +0200132The output executable WAR will be placed in:
133
134----
135 bazel-bin/gerrit.war
136----
137
Edwin Kempin976dc0b2016-11-14 16:34:50 -0800138[[release]]
Edwin Kempin3cfce172016-11-14 16:17:17 -0800139=== Gerrit Release WAR File
140
Ben Rohlfsda0a62b2021-04-26 17:02:19 +0200141To build the Gerrit web application that includes the Gerrit UI,
David Ostrovsky7163dac2017-07-29 06:49:38 +0200142core plugins and documentation:
Edwin Kempin3cfce172016-11-14 16:17:17 -0800143
144----
145 bazel build release
146----
147
148The output executable WAR will be placed in:
149
150----
151 bazel-bin/release.war
152----
153
Han-Wen Nienhuys80fcbe12016-09-22 13:16:51 +0200154=== Headless Mode
155
Ben Rohlfsda0a62b2021-04-26 17:02:19 +0200156To build Gerrit in headless mode, i.e. without the Gerrit UI:
Han-Wen Nienhuys80fcbe12016-09-22 13:16:51 +0200157
158----
159 bazel build headless
160----
161
162The output executable WAR will be placed in:
163
164----
Edwin Kempin03eaa9a2016-11-14 15:45:51 -0800165 bazel-bin/headless.war
Han-Wen Nienhuys80fcbe12016-09-22 13:16:51 +0200166----
167
168=== Extension and Plugin API JAR Files
169
David Ostrovsky7163dac2017-07-29 06:49:38 +0200170To build the extension, plugin and acceptance-framework JAR files:
Han-Wen Nienhuys80fcbe12016-09-22 13:16:51 +0200171
172----
David Ostrovsky26f8da82016-12-24 09:29:33 +0100173 bazel build api
Han-Wen Nienhuys80fcbe12016-09-22 13:16:51 +0200174----
175
David Ostrovsky26f8da82016-12-24 09:29:33 +0100176The output archive that contains Java binaries, Java sources and
177Java docs will be placed in:
Han-Wen Nienhuys80fcbe12016-09-22 13:16:51 +0200178
179----
Luca Milanesio5bd7b9b2019-06-10 10:01:02 +0100180 bazel-bin/api.zip
Han-Wen Nienhuys80fcbe12016-09-22 13:16:51 +0200181----
182
David Ostrovsky7163dac2017-07-29 06:49:38 +0200183Install {extension,plugin,acceptance-framework}-api to the local
184maven repository:
David Ostrovsky50785fd2016-10-28 09:17:14 +0200185
186----
Edwin Kempind3bb3022017-01-24 11:28:34 +0100187 tools/maven/api.sh install
David Ostrovsky50785fd2016-10-28 09:17:14 +0200188----
189
190Install gerrit.war to the local maven repository:
191
192----
Edwin Kempind3bb3022017-01-24 11:28:34 +0100193 tools/maven/api.sh war_install
David Ostrovsky50785fd2016-10-28 09:17:14 +0200194----
Han-Wen Nienhuys80fcbe12016-09-22 13:16:51 +0200195
196=== Plugins
197
David Ostrovskyaf719682016-09-28 20:31:18 +0200198----
199 bazel build plugins:core
200----
201
202The output JAR files for individual plugins will be placed in:
203
204----
Luca Milanesio5bd7b9b2019-06-10 10:01:02 +0100205 bazel-bin/plugins/<name>/<name>.jar
David Ostrovskyaf719682016-09-28 20:31:18 +0200206----
207
208The JAR files will also be packaged in:
209
210----
Luca Milanesio5bd7b9b2019-06-10 10:01:02 +0100211 bazel-bin/plugins/core.zip
David Ostrovskyaf719682016-09-28 20:31:18 +0200212----
213
214To build a specific plugin:
215
216----
Edwin Kempin1ed86c72016-11-14 16:28:22 -0800217 bazel build plugins/<name>
David Ostrovskyaf719682016-09-28 20:31:18 +0200218----
219
220The output JAR file will be be placed in:
221
222----
Luca Milanesio5bd7b9b2019-06-10 10:01:02 +0100223 bazel-bin/plugins/<name>/<name>.jar
David Ostrovskyaf719682016-09-28 20:31:18 +0200224----
225
226Note that when building an individual plugin, the `core.zip` package
227is not regenerated.
Han-Wen Nienhuys80fcbe12016-09-22 13:16:51 +0200228
Han-Wen Nienhuys8ba8abe2016-10-25 15:54:28 +0200229[[IDEs]]
230== Using an IDE.
231
232=== IntelliJ
233
Marian Harbach34253372019-12-10 18:01:31 +0100234The Gerrit build works with Bazel's link:https://ij.bazel.build[IntelliJ plugin,role=external,window=_blank].
Alice Kober-Sotzekd9da89f2016-11-25 12:02:18 +0100235Please follow the instructions on <<dev-intellij#,IntelliJ Setup>>.
David Ostrovskyc69f3602016-11-13 08:01:08 -0800236
237=== Eclipse
238
239==== Generating the Eclipse Project
240
241Create the Eclipse project:
242
243----
David Pursehouse7e43d1d2017-01-24 08:45:16 +0900244 tools/eclipse/project.py
David Ostrovskyc69f3602016-11-13 08:01:08 -0800245----
246
247and then follow the link:dev-eclipse.html#setup[setup instructions].
248
249==== Refreshing the Classpath
250
251If an updated classpath is needed, the Eclipse project can be
252refreshed and missing dependency JARs can be downloaded by running
David Pursehouse7e43d1d2017-01-24 08:45:16 +0900253`project.py` again. For IntelliJ, you need to click the `Sync Project
Marian Harbach34253372019-12-10 18:01:31 +0100254with BUILD Files` button of link:https://ij.bazel.build[Bazel plugin,role=external,window=_blank].
David Ostrovskyc69f3602016-11-13 08:01:08 -0800255
Han-Wen Nienhuys80fcbe12016-09-22 13:16:51 +0200256[[documentation]]
Nasser Grainawia55b20d2021-03-08 11:52:01 -0700257== Documentation
Han-Wen Nienhuys80fcbe12016-09-22 13:16:51 +0200258
David Ostrovsky9413ff82016-10-25 00:02:17 +0200259To build only the documentation for testing or static hosting:
Han-Wen Nienhuys80fcbe12016-09-22 13:16:51 +0200260
David Ostrovsky9413ff82016-10-25 00:02:17 +0200261----
262 bazel build Documentation:searchfree
263----
264
265The html files will be bundled into `searchfree.zip` in this location:
266
267----
268 bazel-bin/Documentation/searchfree.zip
269----
270
271To build the executable WAR with the documentation included:
272
273----
274 bazel build withdocs
275----
276
277The WAR file will be placed in:
278
279----
280 bazel-bin/withdocs.war
281----
Han-Wen Nienhuys80fcbe12016-09-22 13:16:51 +0200282
Antoine Musso524b0562020-11-18 10:36:05 +0100283Alternatively, one can generate the documentation as flat files:
284
285----
286 bazel build Documentation:Documentation
287----
288
289The html, css, js files are placed in:
290
291----
292 `bazel-bin/Documentation/`
293----
294
Han-Wen Nienhuys80fcbe12016-09-22 13:16:51 +0200295[[tests]]
296== Running Unit Tests
297
298----
299 bazel test --build_tests_only //...
300----
301
302Debugging tests:
303
304----
David Ostrovsky17ef1272020-02-16 14:51:07 +0100305 bazel test --test_output=streamed --test_filter=com.gerrit.TestClass.testMethod testTarget
Edwin Kempind31903c2016-11-14 17:01:44 -0800306----
307
308Debug test example:
309
310----
David Ostrovsky714982e2017-08-24 09:43:44 +0200311 bazel test --test_output=streamed --test_filter=com.google.gerrit.acceptance.api.change.ChangeIT.getAmbiguous //javatests/com/google/gerrit/acceptance/api/change:api_change
Han-Wen Nienhuys80fcbe12016-09-22 13:16:51 +0200312----
313
314To run a specific test group, e.g. the rest-account test group:
315
316----
David Ostrovsky714982e2017-08-24 09:43:44 +0200317 bazel test //javatests/com/google/gerrit/acceptance/rest/account:rest_account
Han-Wen Nienhuys80fcbe12016-09-22 13:16:51 +0200318----
319
David Ostrovskyceb5ec12020-04-18 08:50:45 +0200320To run SSH tests using JSch ssh client:
321
322----
323 bazel test --test_env=SSH_CLIENT_IMPLEMENTATION=JSCH //...
324----
325
326To run SSH tests using Apache MINA ssh client:
327
328----
329 bazel test --test_env=SSH_CLIENT_IMPLEMENTATION=APACHE //...
330----
331
Patrick Hiesel244ff8c2016-12-22 11:40:23 +0100332To run only tests that do not use SSH:
333
334----
335 bazel test --test_env=GERRIT_USE_SSH=NO //...
336----
337
Edwin Kempin2e8e96c2017-02-10 11:52:01 +0100338To exclude tests that have been marked as flaky:
339
340----
341 bazel test --test_tag_filters=-flaky //...
342----
343
David Pursehouse7e18f1a2018-05-26 17:40:30 +0900344To exclude tests that require a Docker host:
345
346----
347 bazel test --test_tag_filters=-docker //...
348----
349
David Pursehouse180bca52019-12-04 20:05:07 +0900350To exclude tests that require very recent git client version:
351
352----
353 bazel test --test_tag_filters=-git-protocol-v2 //...
354----
355
Edwin Kempin2e8e96c2017-02-10 11:52:01 +0100356To ignore cached test results:
357
358----
359 bazel test --cache_test_results=NO //...
360----
361
David Pursehouse44445182017-02-22 15:59:17 +0900362To run one or more specific groups of tests:
363
364----
Jacek Centkowski1c41c412017-02-23 06:49:25 +0000365 bazel test --test_tag_filters=api,git //...
David Pursehouse44445182017-02-22 15:59:17 +0900366----
367
Patrick Hiesel2407aca2021-04-30 15:49:12 +0200368To run the tests against a specific index backend (LUCENE, FAKE):
369----
370 bazel test --test_env=GERRIT_INDEX_TYPE=LUCENE //...
371----
372
373Elastic search is not currently supported in integration tests.
374
David Pursehouse44445182017-02-22 15:59:17 +0900375The following values are currently supported for the group name:
376
377* annotation
378* api
David Pursehouse0223c0f2018-06-19 20:44:42 +0900379* docker
David Pursehouse44445182017-02-22 15:59:17 +0900380* edit
David Pursehouse0223c0f2018-06-19 20:44:42 +0900381* elastic
David Pursehouse44445182017-02-22 15:59:17 +0900382* git
David Pursehouse180bca52019-12-04 20:05:07 +0900383* git-protocol-v2
David Ostrovsky61a44ae2020-05-14 08:29:54 +0200384* git-upload-archive
David Pursehouse44445182017-02-22 15:59:17 +0900385* notedb
386* pgm
387* rest
388* server
389* ssh
390
Nasser Grainawi02f53452021-02-19 08:17:49 -0700391Bazel itself supports a multitude of ways to
392link:https://docs.bazel.build/versions/master/guide.html#specifying-targets-to-build[specify targets,role=external,window=_blank]
393for fine-grained test selection that can be combined with many of the examples
394above.
395
Saša Živkov1b716dd2021-03-12 17:03:33 +0100396[[debugging-tests]]
397== Debugging Unit Tests
398In some cases it may be necessary to debug a test while running it in bazel. For example, when we
399observe a different test result in Eclipse and bazel. Using the `--java_debug` option will start the
400JVM in debug mode and await for a remote debugger to attach.
401
402Example:
403[source,bash]
404----
405 bazel test --java_debug --test_tag_filters=delete-project //...
406 ...
407 Listening for transport dt_socket at address: 5005
408 ...
409----
410
411Now attach with a debugger to the port `5005`. For example use "Remote Java Application" launch
412configuration in Eclipe and specify the port `5005`.
413
David Pursehouse564b29a2018-05-30 07:49:02 +0900414[[elasticsearch]]
415=== Elasticsearch
416
David Pursehouseb04ae612018-06-19 20:34:36 +0900417Successfully running the Elasticsearch tests requires Docker, and
Marco Miller12e4f2b2019-12-19 16:24:43 -0500418may require setting the local virtual memory on
Marco Miller20b0d302019-12-21 14:29:49 -0500419link:https://www.elastic.co/guide/en/elasticsearch/reference/current/vm-max-map-count.html[linux,role=external,window=_blank] and
420link:https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html#_set_vm_max_map_count_to_at_least_262144[macOS,role=external,window=_blank].
Marco Miller12e4f2b2019-12-19 16:24:43 -0500421
Marco Miller20b0d302019-12-21 14:29:49 -0500422On macOS, if using link:https://docs.docker.com/docker-for-mac/[Docker Desktop,role=external,window=_blank],
Marco Miller12e4f2b2019-12-19 16:24:43 -0500423the effective memory value can be set in the Preferences, under the Advanced tab.
424The default value usually does not suffice and is causing premature container exits.
425That default is currently 2 GB and should be set to at least 5 (GB).
David Pursehouse564b29a2018-05-30 07:49:02 +0900426
David Pursehouseb04ae612018-06-19 20:34:36 +0900427If Docker is not available, the Elasticsearch tests will be skipped.
428Note that Bazel currently does not show
Marian Harbach34253372019-12-10 18:01:31 +0100429link:https://github.com/bazelbuild/bazel/issues/3476[the skipped tests,role=external,window=_blank].
David Pursehouse564b29a2018-05-30 07:49:02 +0900430
David Ostrovsky17ef1272020-02-16 14:51:07 +0100431[[logging]]
432=== Controlling logging level
Hamza Kacedc54e6b52020-01-24 12:59:09 -0500433
David Ostrovsky17ef1272020-02-16 14:51:07 +0100434Per default, logging level is set to `INFO` level for all tests. The `DEBUG`
435log level can be enabled for the tests.
Hamza Kacedc54e6b52020-01-24 12:59:09 -0500436
David Ostrovsky17ef1272020-02-16 14:51:07 +0100437In IDE, set `-Dgerrit.logLevel=debug` as a VM argument. With `bazel`, pass
438`GERRIT_LOG_LEVEL=debug` environment variable:
Hamza Kacedc54e6b52020-01-24 12:59:09 -0500439
440----
David Pursehoused623a5c2020-05-01 14:29:57 +0900441 bazel test --test_filter=com.google.gerrit.server.notedb.ChangeNotesTest \
David Ostrovsky17ef1272020-02-16 14:51:07 +0100442 --test_env=GERRIT_LOG_LEVEL=debug \
443 javatests/com/google/gerrit/server:server_tests
Hamza Kacedc54e6b52020-01-24 12:59:09 -0500444----
445
David Ostrovsky17ef1272020-02-16 14:51:07 +0100446The log results can be found in:
447`bazel-testlogs/javatests/com/google/gerrit/server/server_tests/test.log`.
448
449
Han-Wen Nienhuys80fcbe12016-09-22 13:16:51 +0200450== Dependencies
451
Han-Wen Nienhuys314fb112016-11-14 10:04:02 -0800452Dependency JARs are normally downloaded as needed, but you can
453download everything upfront. This is useful to enable
Han-Wen Nienhuys80fcbe12016-09-22 13:16:51 +0200454subsequent builds to run without network access:
455
456----
Han-Wen Nienhuys314fb112016-11-14 10:04:02 -0800457 bazel fetch //...
Han-Wen Nienhuys80fcbe12016-09-22 13:16:51 +0200458----
459
460When downloading from behind a proxy (which is common in some corporate
461environments), it might be necessary to explicitly specify the proxy that
462is then used by `curl`:
463
464----
465 export http_proxy=http://<proxy_user_id>:<proxy_password>@<proxy_server>:<proxy_port>
466----
467
David Ostrovskyc0f6d4d2017-01-20 08:30:07 +0100468Redirection to local mirrors of Maven Central and the Gerrit storage
469bucket is supported by defining specific properties in
470`local.properties`, a file that is not tracked by Git:
471
472----
473 echo download.GERRIT = http://nexus.my-company.com/ >>local.properties
474 echo download.MAVEN_CENTRAL = http://nexus.my-company.com/ >>local.properties
475----
476
477The `local.properties` file may be placed in the root of the gerrit repository
478being built, or in `~/.gerritcodereview/`. The file in the root of the gerrit
479repository has precedence.
Han-Wen Nienhuys80fcbe12016-09-22 13:16:51 +0200480
481== Building against unpublished Maven JARs
482
Dave Borowitz8626f872019-04-19 14:34:36 -0700483To build against unpublished Maven JARs, like PrologCafe, the custom JARs must
484be installed in the local Maven repository (`mvn clean install`) and
Han-Wen Nienhuys80fcbe12016-09-22 13:16:51 +0200485`maven_jar()` must be updated to point to the `MAVEN_LOCAL` Maven repository for
486that artifact:
487
488[source,python]
489----
490 maven_jar(
Dave Borowitz8626f872019-04-19 14:34:36 -0700491 name = 'prolog-runtime',
492 artifact = 'com.googlecode.prolog-cafe:prolog-runtime:42',
Han-Wen Nienhuys80fcbe12016-09-22 13:16:51 +0200493 repository = MAVEN_LOCAL,
494 )
495----
496
497== Building against artifacts from custom Maven repositories
498
499To build against custom Maven repositories, two modes of operations are
500supported: with rewrite in local.properties and without.
501
502Without rewrite the URL of custom Maven repository can be directly passed
503to the maven_jar() function:
504
505[source,python]
506----
507 GERRIT_FORGE = 'http://gerritforge.com/snapshot'
508
509 maven_jar(
510 name = 'gitblit',
Paladox nonedab92ab2017-01-28 22:56:33 +0000511 artifact = 'com.gitblit:gitblit:1.4.0',
Han-Wen Nienhuys80fcbe12016-09-22 13:16:51 +0200512 sha1 = '1b130dbf5578ace37507430a4a523f6594bf34fa',
Han-Wen Nienhuys80fcbe12016-09-22 13:16:51 +0200513 repository = GERRIT_FORGE,
514 )
515----
516
517When the custom URL has to be rewritten, then the same logic as with Gerrit
518known Maven repository is used: Repo name must be defined that matches an entry
519in local.properties file:
520
521----
522 download.GERRIT_FORGE = http://my.company.mirror/gerrit-forge
523----
524
525And corresponding WORKSPACE excerpt:
526
527[source,python]
528----
529 GERRIT_FORGE = 'GERRIT_FORGE:'
530
531 maven_jar(
532 name = 'gitblit',
Paladox nonedab92ab2017-01-28 22:56:33 +0000533 artifact = 'com.gitblit:gitblit:1.4.0',
Han-Wen Nienhuys80fcbe12016-09-22 13:16:51 +0200534 sha1 = '1b130dbf5578ace37507430a4a523f6594bf34fa',
Han-Wen Nienhuys80fcbe12016-09-22 13:16:51 +0200535 repository = GERRIT_FORGE,
536 )
537----
538
David Ostrovskyad61c852019-01-14 07:11:04 +0100539== Building against SNAPSHOT Maven JARs
540
541To build against SNAPSHOT Maven JARs, the complete SNAPSHOT version must be used:
542
543[source,python]
544----
545 maven_jar(
546 name = "pac4j-core",
547 artifact = "org.pac4j:pac4j-core:3.5.0-SNAPSHOT-20190112.120241-16",
548 sha1 = "da2b1cb68a8f87bfd40813179abd368de9f3a746",
549 )
550----
551
David Ostrovsky2bf355d2018-05-30 08:58:22 +0200552[[bazel-local-caches]]
David Ostrovsky88bc3722017-09-19 08:29:27 +0200553
David Pursehouse42cb2932018-06-05 14:49:06 +0900554To accelerate builds, several caches are activated per default:
David Ostrovsky2bf355d2018-05-30 08:58:22 +0200555
556* ~/.gerritcodereview/bazel-cache/downloaded-artifacts
557* ~/.gerritcodereview/bazel-cache/repository
558* ~/.gerritcodereview/bazel-cache/cas
559
Antoine Mussoff89f792021-05-04 11:14:05 +0200560The `downloaded-artifacts` cache can be relocated by setting the
561`GERRIT_CACHE_HOME` environment variable. The other two can be adjusted with
562`bazel build` options `--repository_cache` and `--disk_cache` respectively.
563
David Pursehouse42cb2932018-06-05 14:49:06 +0900564Currently none of these caches have a maximum size limit. See
Marian Harbach34253372019-12-10 18:01:31 +0100565link:https://github.com/bazelbuild/bazel/issues/5139[this bazel issue,role=external,window=_blank] for
David Pursehouse42cb2932018-06-05 14:49:06 +0900566details. Users should watch the cache sizes and clean them manually if
567necessary.
568
Dave Borowitzf04eb082018-07-16 08:51:17 -0700569[[npm-binary]]
570== NPM Binaries
571
Ben Rohlfsda0a62b2021-04-26 17:02:19 +0200572Parts of the Gerrit web app build require running NPM-based JavaScript programs
573as "binaries". We don't attempt to resolve and download NPM dependencies at
574build time, but instead use pre-built bundles of the NPM binary along with all
575its dependencies. Some packages on
Marian Harbach34253372019-12-10 18:01:31 +0100576link:https://docs.npmjs.com/misc/registry[registry.npmjs.org,role=external,window=_blank] come with their
Dave Borowitzf04eb082018-07-16 08:51:17 -0700577dependencies bundled, but this is the exception rather than the rule. More
578commonly, to add a new binary to this list, you will need to bundle the binary
579yourself.
580
581[NOTE]
582We can only use binaries that meet certain licensing requirements, and that do
583not include any native code.
584
585Start by checking that the license and file types of the bundle are acceptable:
586[source,bash]
587----
588 gerrit_repo=/path/to/gerrit
589 package=some-npm-package
590 version=1.2.3
591
Dmitrii Filippovc2cdcce2020-01-27 12:57:42 +0100592 # Note - yarn must be installed before running the following commands
593 yarn global add license-checker && \
Dave Borowitzf04eb082018-07-16 08:51:17 -0700594 rm -rf /tmp/$package-$version && mkdir -p /tmp/$package-$version && \
595 cd /tmp/$package-$version && \
Dmitrii Filippovc2cdcce2020-01-27 12:57:42 +0100596 yarn add $package@$version && \
Dave Borowitzf04eb082018-07-16 08:51:17 -0700597 license-checker | grep licenses: | sort -u
598----
599
600This will output a list of the different licenses used by the package and all
601its transitive dependencies. We can only legally distribute a bundle via our
602storage bucket if the licenses allow us to do so. As long as all of the listed
603license are allowed by
604link:https://opensource.google.com/docs/thirdparty/licenses/[Google's
Marian Harbach34253372019-12-10 18:01:31 +0100605standards,role=external,window=_blank]. Any `by_exception_only`, commercial, prohibited, or unlisted
Dave Borowitzf04eb082018-07-16 08:51:17 -0700606licenses are not allowed; otherwise, it is ok to distribute the source. If in
607doubt, contact a maintainer who is a Googler.
608
609Next, check the file types:
610[source,bash]
611----
612 cd /tmp/$package-$version
613 find . -type f | xargs file | grep -v 'ASCII\|UTF-8\|empty$'
614----
615
616If you see anything that looks like a native library or binary, then we can't
617use the bundle.
618
Dmitrii Filippovc2cdcce2020-01-27 12:57:42 +0100619If everything looks good, install the package with the following command:
620[source, bash]
Dave Borowitzf04eb082018-07-16 08:51:17 -0700621----
Dmitrii Filippovc2cdcce2020-01-27 12:57:42 +0100622# Add to ui_npm. Other packages.json can be updated in the same way
623cd $gerrit_repo/polygerrit-ui/app
624bazel run @nodejs//:yarn add $package
Dave Borowitzf04eb082018-07-16 08:51:17 -0700625----
626
Dmitrii Filippovc2cdcce2020-01-27 12:57:42 +0100627Update the `polygerrit-ui/app/node_modules_licenses/licenses.ts` file. You should add licenses
628for the package itself and for all transitive depndencies. If you forgot to add a license, the
629`Documentation:check_licenses` test will fail.
Dave Borowitzf04eb082018-07-16 08:51:17 -0700630
Dmitrii Filippovc2cdcce2020-01-27 12:57:42 +0100631After the update, commit all changes to the repository (including `yarn.lock`).
Dave Borowitzf04eb082018-07-16 08:51:17 -0700632
Dmitrii Filippovc2cdcce2020-01-27 12:57:42 +0100633[NOTE]
634====
635If a npm package has transitive dependencies (or just several files) with a not allowed
636license and you can't avoid use it in release, then you can add this package.
637For example some packages contain demo-code with a different license. Another example - optional
Ben Rohlfsda0a62b2021-04-26 17:02:19 +0200638dependencies, which are not needed to build the Gerrit web app, but they are installed together
639with the package anyway.
Dave Borowitzf04eb082018-07-16 08:51:17 -0700640
Dmitrii Filippovc2cdcce2020-01-27 12:57:42 +0100641In this case you should exclude all files and/or transitive dependencies with a not allowed license.
642Adding such package requires additional updates:
Dave Borowitzf04eb082018-07-16 08:51:17 -0700643
Dmitrii Filippovc2cdcce2020-01-27 12:57:42 +0100644- Add dependencies (or files) to the license.ts with an appropriate license marked with
645`allowed: false`.
Dave Borowitzf04eb082018-07-16 08:51:17 -0700646
Dmitrii Filippovc2cdcce2020-01-27 12:57:42 +0100647- update package.json postinstall script to remove all non-allowed files (if you don't
648update postinstall script, `Documentation:check_licenses` test will fail.)
649====
650
651=== Update NPM Binaries
652To update a NPM binary the same actions as for a new one must be done (check licenses,
653update `licenses.ts` file, etc...). The only difference is a command to install a package: instead
654of `bazel run @nodejs//:yarn add $package` you should run the `bazel run @nodejs//:yarn upgrade ...`
655command with correct arguments. You can find the list of arguments in the
656link:https://classic.yarnpkg.com/en/docs/cli/upgrade/[yarn upgrade doc,role=external,window=_blank].
Dave Borowitzf04eb082018-07-16 08:51:17 -0700657
658
Han-Wen Nienhuys9c15e2b2019-05-16 16:59:33 +0200659[[RBE]]
David Pursehouse0713b9a2019-05-21 12:20:26 +0900660== Google Remote Build Support
Han-Wen Nienhuys9c15e2b2019-05-16 16:59:33 +0200661
662The Bazel build can be used with Google's Remote Build Execution.
663
664
665This needs the following setup steps:
666
667```
668gcloud auth application-default login
669gcloud services enable remotebuildexecution.googleapis.com --project=${PROJECT}
670```
671
672Create a worker pool. The instances should have at least 4 CPUs each
673for adequate performance.
674
675```
676gcloud alpha remote-build-execution worker-pools create default \
677 --project=${PROJECT} \
678 --instance=default_instance \
679 --worker-count=50 \
Han-Wen Nienhuys8abed332020-04-06 16:50:38 +0200680 --machine-type=e2-standard-4 \
Han-Wen Nienhuys9c15e2b2019-05-16 16:59:33 +0200681 --disk-size=200
682```
683
684To use RBE, execute
685
686```
687bazel test --config=remote \
688 --remote_instance_name=projects/${PROJECT}/instances/default_instance \
689 javatests/...
690```
691
692
Han-Wen Nienhuys80fcbe12016-09-22 13:16:51 +0200693GERRIT
694------
695Part of link:index.html[Gerrit Code Review]
696
697SEARCHBOX
698---------