blob: 50770798cc017728c3856805bef075e938852f2a [file] [log] [blame]
Gert van Dijkbeb687b2019-08-26 16:39:58 +02001= Gerrit Code Review - IntelliJ IDEA Setup
Alice Kober-Sotzekd9da89f2016-11-25 12:02:18 +01002
3== Prerequisites
Gert van Dijkbeb687b2019-08-26 16:39:58 +02004
5=== Bazel
6
7Bazel must be installed as described by
8<<dev-bazel#installation,Building with Bazel - Installation>>.
9
10It's strongly recommended to verify you can build your Gerrit tree with Bazel
11for Java 8 from the command line first. Ensure that at least
12`bazel build gerrit` runs successfully before you proceed.
13
14=== IntelliJ version and Bazel plugin
15
16Before downloading IntelliJ, look at the
17link:https://plugins.jetbrains.com/plugin/8609-bazel/versions[JetBrains plugin repository page of the Bazel plugin]
18to see what version of the IntelliJ IDEA it is actually compatible with.
19
20Also note that the version of the Bazel plugin used in turn may or may not be
21compatible with the Bazel version used.
Alice Kober-Sotzekd9da89f2016-11-25 12:02:18 +010022
23In addition, Java 8 must be specified on your path or via `JAVA_HOME` so that
24building with Bazel via the Bazel plugin is possible.
25
26TIP: If the synchronization of the project with the BUILD files using the Bazel
27plugin fails and IntelliJ reports the error **Could not get Bazel roots**, this
28indicates that the Bazel plugin couldn't find Java 8.
29
Gert van Dijkbeb687b2019-08-26 16:39:58 +020030=== Installation of IntelliJ IDEA
31
32Please refer to the
33link:https://www.jetbrains.com/help/idea/installation-guide.html[installation guide provided by Jetbrains]
34to install it on your platform. Make sure to install a version compatible with
35the Bazel plugin as mentioned above.
Alice Kober-Sotzekd9da89f2016-11-25 12:02:18 +010036
37== Installation of the Bazel plugin
38
Gert van Dijkbeb687b2019-08-26 16:39:58 +020039The plugin is usually installed using the Jetbrains plugin repository as shown
40in the steps below, but it's also possible to
41link:https://github.com/bazelbuild/intellij[build it from source].
42
Alice Kober-Sotzekd9da89f2016-11-25 12:02:18 +010043. Go to *File -> Settings -> Plugins*.
Gert van Dijkbeb687b2019-08-26 16:39:58 +020044+
45(Or, from the welcome screen, *Configure -> Plugins*)
46. Activate the *Marketplace* tab.
47. Search for the plugin `Bazel` (by Google).
48+
49TIP: In case the Bazel plugin is not listed, or if it shows an outdated version,
50verify the compatibility between the Bazel plugin and IntelliJ IDEA on link:https://plugins.jetbrains.com/plugin/8609-bazel/versions[the JetBrains plugin page].
Alice Kober-Sotzekd9da89f2016-11-25 12:02:18 +010051. Install it.
Gert van Dijkbeb687b2019-08-26 16:39:58 +020052. Restart IntelliJ IDEA.
Alice Kober-Sotzekd9da89f2016-11-25 12:02:18 +010053
Gert van Dijkbeb687b2019-08-26 16:39:58 +020054[TIP]
55====
56If your project's Bazel build fails with **Cannot run program "bazel": No such
57file or directory**, then you may have to set the binary location in the Bazel
58plugin settings:
Ben Rohlfs8b932542019-01-08 19:32:39 +010059
Gert van Dijkbeb687b2019-08-26 16:39:58 +020060. Go to *Preferences -> Other Settings -> Bazel Settings*.
61. Set the *Bazel binary location*.
62====
Ben Rohlfs8b932542019-01-08 19:32:39 +010063
Gert van Dijkbeb687b2019-08-26 16:39:58 +020064== Creation of the project
Alice Kober-Sotzekd9da89f2016-11-25 12:02:18 +010065
66. Go to *File -> Import Bazel Project*.
Gert van Dijkbeb687b2019-08-26 16:39:58 +020067+
68(Or, from the welcome screen, *Import Bazel Project* should already be shown in
69there.)
Alice Kober-Sotzekd9da89f2016-11-25 12:02:18 +010070. For *Use existing bazel workspace -> Workspace*, select the directory
71containing the Gerrit source code.
72. Choose *Import from workspace* and select the `.bazelproject` file which is
73located in the top directory of the Gerrit source code.
74. Adjust the path of the project data directory and the name of the project if
75desired.
Gert van Dijkbeb687b2019-08-26 16:39:58 +020076. Finish the creation of the project.
77. Verify that you can now build the project. Hit the button with the Bazel icon
78(located on the top-right by default) to synchronize the project. Note that
79warnings may be present in the build.
80
81At this point all the basic functionality should be working such as Java class
82inspection and running <<unit-tests,unit tests>>.
Alice Kober-Sotzekd9da89f2016-11-25 12:02:18 +010083
84TIP: The project data directory can be separate from the source code. One
85advantage of this is that project files don't need to be excluded from version
86control.
87
Alice Kober-Sotzek74d2f5b2016-11-25 14:04:53 +010088== Recommended settings
89
90=== Code style
Alice Kober-Sotzek0b257d82017-03-02 11:58:36 +010091
92==== google-java-format plugin
93Install the `google-java-format` plugin by following these steps:
94
95. Go to *File -> Settings -> Plugins*.
Gert van Dijkbeb687b2019-08-26 16:39:58 +020096. Activate the *Marketplace* tab.
97. Search for the plugin `google-java-format` by Google.
Alice Kober-Sotzek0b257d82017-03-02 11:58:36 +010098. Install it.
Gert van Dijkbeb687b2019-08-26 16:39:58 +020099. Restart IntelliJ IDEA.
Alice Kober-Sotzek0b257d82017-03-02 11:58:36 +0100100
Gert van Dijkbeb687b2019-08-26 16:39:58 +0200101Every time you start IntelliJ IDEA, make sure to use *Code -> Reformat with
Alice Kober-Sotzek0b257d82017-03-02 11:58:36 +0100102google-java-format* on an arbitrary line of code. This replaces the default
103CodeStyleManager with a custom one. Thus, uses of *Reformat Code* either via
104*Code -> Reformat Code*, keyboard shortcuts, or the commit dialog will use the
105custom style defined by the `google-java-format` plugin.
106
Gert van Dijkbeb687b2019-08-26 16:39:58 +0200107Please refer to the documentation on the <<dev-contributing#style,code style>>
108for which version of `google-java-format` is used with Gerrit.
109
Alice Kober-Sotzek0b257d82017-03-02 11:58:36 +0100110==== Code style settings
111The `google-java-format` plugin is the preferred way to format the code. As it
112only kicks in on demand, it's also recommended to have code style settings
113which help to create properly formatted code as-you-go. Those settings can't
114completely mimic the format enforced by the `google-java-format` plugin but try
115to be as close as possible. So before submitting code, please make sure to run
116*Reformat Code*.
117
118. Download
119https://raw.githubusercontent.com/google/styleguide/gh-pages/intellij-java-google-style.xml[
120intellij-java-google-style.xml].
Alice Kober-Sotzek74d2f5b2016-11-25 14:04:53 +0100121. Go to *File -> Settings -> Editor -> Code Style*.
Gert van Dijkbeb687b2019-08-26 16:39:58 +0200122. Click on the wrench icon with the tooltip _Show Scheme Actions_.
123. Click on *Import Scheme*.
Alice Kober-Sotzek0b257d82017-03-02 11:58:36 +0100124. Select the previously downloaded file `intellij-java-google-style.xml`.
Gert van Dijkbeb687b2019-08-26 16:39:58 +0200125. Make sure that `GoogleStyle` is chosen as the current *Scheme*.
Alice Kober-Sotzek74d2f5b2016-11-25 14:04:53 +0100126
127In addition, the EditorConfig settings (which ensure a consistent style between
128Eclipse, IntelliJ, and other editors) should be applied on top of that. Those
129settings are in the file `.editorconfig` of the Gerrit source code. IntelliJ
130will automatically pick up those settings if the EditorConfig plugin is enabled
131and configured correctly as can be verified by:
132
133. Go to *File -> Settings -> Plugins*.
Gert van Dijkbeb687b2019-08-26 16:39:58 +0200134. Ensure that the *EditorConfig* plugin (by JetBrains) is enabled.
Alice Kober-Sotzek74d2f5b2016-11-25 14:04:53 +0100135. Go to *File -> Settings -> Editor -> Code Style*.
136. Ensure that *Enable EditorConfig support* is checked.
137
138NOTE: If IntelliJ notifies you later on that the EditorConfig settings override
139the code style settings, simply confirm that.
140
Alice Kober-Sotzekb11a0752016-11-25 16:08:43 +0100141=== Copyright
Gert van Dijkbeb687b2019-08-26 16:39:58 +0200142
143. Copy the folder `$(gerrit_source_code)/tools/intellij/copyright` (not just the
Alice Kober-Sotzekb11a0752016-11-25 16:08:43 +0100144contents) to `$(project_data_directory)/.idea`. If it already exists, replace
Gert van Dijkbeb687b2019-08-26 16:39:58 +0200145it. If you didn't select a custom data directory the command could look like
146this, as run from the Gerrit source tree checkout as working directory:
147+
148----
149cp -r tools/intellij/copyright .ijwb/.idea/
150----
151. Go to *File -> Settings -> Editor -> Copyright -> Copyright Profiles*.
152. Verify that the *Gerrit Copyright* is now present there.
153+
154Only in case it hasn't picked up the copyright profile automatically, import
155the `Gerrit_Copyright.xml` from that folder manually.
Alice Kober-Sotzekb11a0752016-11-25 16:08:43 +0100156
Gert van Dijkbeb687b2019-08-26 16:39:58 +0200157=== Git integration
158This section is only relevant in case you want to use the Git integration
159plugin in IntelliJ IDEA.
Alice Kober-Sotzekb11a0752016-11-25 16:08:43 +0100160
Alice Kober-Sotzek6af5c3b2016-11-25 15:22:38 +0100161To simplify the creation of commit messages which are compliant with the
162<<dev-contributing#commit-message,Commit Message>> format, do the following:
163
Gert van Dijkbeb687b2019-08-26 16:39:58 +0200164. Go to *File -> Settings -> Version Control -> Commit Dialog*.
165. In the *Commit message inspections*, activate the three inspections:
166* *Blank line between subject and body*,
167* *Limit body line* and
168* *Limit subject line*.
169. For the limit line inspections, make sure that 72 is specified as value.
170. For *Limit body line*, tick *Show right margin* and *Wrap when typing reaches
171right margin*.
Alice Kober-Sotzek6af5c3b2016-11-25 15:22:38 +0100172
173In addition, you should follow the instructions of
174<<dev-contributing#git_commit_settings,this section>> (if you haven't
175done so already):
176
Gert van Dijkbeb687b2019-08-26 16:39:58 +0200177* Install the Git commit message hook for the `Change-Id` line.
Alice Kober-Sotzek6af5c3b2016-11-25 15:22:38 +0100178* Set up the HTTP access.
179
180Setting up the HTTP access will allow you to commit changes via IntelliJ without
181specifying your credentials. The Git hook won't be noticeable during a commit
182as it's executed after the commit dialog of IntelliJ was closed.
183
Alice Kober-Sotzekd9da89f2016-11-25 12:02:18 +0100184== Run configurations
Alice Kober-Sotzek1e7c17a2016-11-25 13:37:55 +0100185Run configurations can be accessed on the toolbar. To edit them or add new ones,
186choose *Edit Configurations* on the drop-down list of the run configurations
187or go to *Run -> Edit Configurations*.
Alice Kober-Sotzekd9da89f2016-11-25 12:02:18 +0100188
Gert van Dijkbeb687b2019-08-26 16:39:58 +0200189[[runconfigurations-daemon]]
David Pursehouseb7f1b9c2019-08-30 14:04:27 +0900190=== Gerrit Daemon
191
Gert van Dijkbeb687b2019-08-26 16:39:58 +0200192[WARNING]
193====
194At the moment running this (local) configuration results in a
Alice Kober-Sotzek485a5ff2017-03-02 13:20:32 +0100195`java.io.FileNotFoundException`. To debug a local Gerrit server with IntelliJ,
196use the instructions of <<dev-readme#run_daemon,Running the Daemon>> in
197combination with <<remote-debug,Debugging a remote Gerrit server>>.
198
Gert van Dijkbeb687b2019-08-26 16:39:58 +0200199(link:https://bugs.chromium.org/p/gerrit/issues/detail?id=11360[Issue 11360])
200====
201
Alice Kober-Sotzekd9da89f2016-11-25 12:02:18 +0100202Copy `$(gerrit_source_code)/tools/intellij/gerrit_daemon.xml` to
203`$(project_data_directory)/.idea/runConfigurations/`.
204
205This run configuration starts the Gerrit daemon similarly as
206<<dev-readme#run_daemon,Running the Daemon>>.
207
208NOTE: The <<dev-readme#init,Site Initialization>> has to be completed
209before this run configuration works properly.
Alice Kober-Sotzek1e7c17a2016-11-25 13:37:55 +0100210
Gert van Dijkbeb687b2019-08-26 16:39:58 +0200211[[unit-tests]]
Alice Kober-Sotzek1e7c17a2016-11-25 13:37:55 +0100212=== Unit tests
213To create run configurations for unit tests, run or debug them via a right-click
214on a method, class, file, or package. The created run configuration is a
Gert van Dijkbeb687b2019-08-26 16:39:58 +0200215temporary one and can be saved to make it permanent by selecting *Create
216'Bazel test [...]'...* from the context menu.
Alice Kober-Sotzek1e7c17a2016-11-25 13:37:55 +0100217
218Normally, this approach generates JUnit run configurations. When the Bazel
219plugin manages a project, it intercepts the creation and creates a Bazel test
220run configuration instead, which can be used just like the standard ones.
221
Alice Kober-Sotzek485a5ff2017-03-02 13:20:32 +0100222[[remote-debug]]
223=== Debugging a remote Gerrit server
224If a remote Gerrit server is running and has opened a debug port, you can attach
225IntelliJ via a `Remote debug configuration`.
226
227. Go to *Run -> Edit Configurations*.
228. Click on the *+* to add a new configuration.
Gert van Dijkbeb687b2019-08-26 16:39:58 +0200229. Choose *Remote* from the *Templates*.
Alice Kober-Sotzek485a5ff2017-03-02 13:20:32 +0100230. Adjust *Configuration -> Settings -> Host* and *Port*.
231. Start this configuration in `Debug` mode.
232
Gert van Dijkbeb687b2019-08-26 16:39:58 +0200233TIP: This run configuration dialog also shows the line for the JVM as startup
234flag that you can copy to include in your
235`$(gerrit_test_site)/etc/gerrit.config` in the `[container]` section in order
236to work-around the <<runconfigurations-daemon,local run configuration issue>>.
237
Alice Kober-Sotzek55332812016-12-07 15:12:14 +0100238GERRIT
239------
240Part of link:index.html[Gerrit Code Review]
241
242SEARCHBOX
243---------