Gert van Dijk | beb687b | 2019-08-26 16:39:58 +0200 | [diff] [blame] | 1 | = Gerrit Code Review - IntelliJ IDEA Setup |
Alice Kober-Sotzek | d9da89f | 2016-11-25 12:02:18 +0100 | [diff] [blame] | 2 | |
| 3 | == Prerequisites |
Gert van Dijk | beb687b | 2019-08-26 16:39:58 +0200 | [diff] [blame] | 4 | |
| 5 | === Bazel |
| 6 | |
| 7 | Bazel must be installed as described by |
| 8 | <<dev-bazel#installation,Building with Bazel - Installation>>. |
| 9 | |
| 10 | It's strongly recommended to verify you can build your Gerrit tree with Bazel |
| 11 | for 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 | |
| 16 | Before downloading IntelliJ, look at the |
| 17 | link:https://plugins.jetbrains.com/plugin/8609-bazel/versions[JetBrains plugin repository page of the Bazel plugin] |
| 18 | to see what version of the IntelliJ IDEA it is actually compatible with. |
| 19 | |
| 20 | Also note that the version of the Bazel plugin used in turn may or may not be |
| 21 | compatible with the Bazel version used. |
Alice Kober-Sotzek | d9da89f | 2016-11-25 12:02:18 +0100 | [diff] [blame] | 22 | |
| 23 | In addition, Java 8 must be specified on your path or via `JAVA_HOME` so that |
| 24 | building with Bazel via the Bazel plugin is possible. |
| 25 | |
| 26 | TIP: If the synchronization of the project with the BUILD files using the Bazel |
| 27 | plugin fails and IntelliJ reports the error **Could not get Bazel roots**, this |
| 28 | indicates that the Bazel plugin couldn't find Java 8. |
| 29 | |
Gert van Dijk | beb687b | 2019-08-26 16:39:58 +0200 | [diff] [blame] | 30 | === Installation of IntelliJ IDEA |
| 31 | |
| 32 | Please refer to the |
| 33 | link:https://www.jetbrains.com/help/idea/installation-guide.html[installation guide provided by Jetbrains] |
| 34 | to install it on your platform. Make sure to install a version compatible with |
| 35 | the Bazel plugin as mentioned above. |
Alice Kober-Sotzek | d9da89f | 2016-11-25 12:02:18 +0100 | [diff] [blame] | 36 | |
| 37 | == Installation of the Bazel plugin |
| 38 | |
Gert van Dijk | beb687b | 2019-08-26 16:39:58 +0200 | [diff] [blame] | 39 | The plugin is usually installed using the Jetbrains plugin repository as shown |
| 40 | in the steps below, but it's also possible to |
| 41 | link:https://github.com/bazelbuild/intellij[build it from source]. |
| 42 | |
Alice Kober-Sotzek | d9da89f | 2016-11-25 12:02:18 +0100 | [diff] [blame] | 43 | . Go to *File -> Settings -> Plugins*. |
Gert van Dijk | beb687b | 2019-08-26 16:39:58 +0200 | [diff] [blame] | 44 | + |
| 45 | (Or, from the welcome screen, *Configure -> Plugins*) |
| 46 | . Activate the *Marketplace* tab. |
| 47 | . Search for the plugin `Bazel` (by Google). |
| 48 | + |
| 49 | TIP: In case the Bazel plugin is not listed, or if it shows an outdated version, |
| 50 | verify 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-Sotzek | d9da89f | 2016-11-25 12:02:18 +0100 | [diff] [blame] | 51 | . Install it. |
Gert van Dijk | beb687b | 2019-08-26 16:39:58 +0200 | [diff] [blame] | 52 | . Restart IntelliJ IDEA. |
Alice Kober-Sotzek | d9da89f | 2016-11-25 12:02:18 +0100 | [diff] [blame] | 53 | |
Gert van Dijk | beb687b | 2019-08-26 16:39:58 +0200 | [diff] [blame] | 54 | [TIP] |
| 55 | ==== |
| 56 | If your project's Bazel build fails with **Cannot run program "bazel": No such |
| 57 | file or directory**, then you may have to set the binary location in the Bazel |
| 58 | plugin settings: |
Ben Rohlfs | 8b93254 | 2019-01-08 19:32:39 +0100 | [diff] [blame] | 59 | |
Gert van Dijk | beb687b | 2019-08-26 16:39:58 +0200 | [diff] [blame] | 60 | . Go to *Preferences -> Other Settings -> Bazel Settings*. |
| 61 | . Set the *Bazel binary location*. |
| 62 | ==== |
Ben Rohlfs | 8b93254 | 2019-01-08 19:32:39 +0100 | [diff] [blame] | 63 | |
Gert van Dijk | beb687b | 2019-08-26 16:39:58 +0200 | [diff] [blame] | 64 | == Creation of the project |
Alice Kober-Sotzek | d9da89f | 2016-11-25 12:02:18 +0100 | [diff] [blame] | 65 | |
| 66 | . Go to *File -> Import Bazel Project*. |
Gert van Dijk | beb687b | 2019-08-26 16:39:58 +0200 | [diff] [blame] | 67 | + |
| 68 | (Or, from the welcome screen, *Import Bazel Project* should already be shown in |
| 69 | there.) |
Alice Kober-Sotzek | d9da89f | 2016-11-25 12:02:18 +0100 | [diff] [blame] | 70 | . For *Use existing bazel workspace -> Workspace*, select the directory |
| 71 | containing the Gerrit source code. |
| 72 | . Choose *Import from workspace* and select the `.bazelproject` file which is |
| 73 | located 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 |
| 75 | desired. |
Gert van Dijk | beb687b | 2019-08-26 16:39:58 +0200 | [diff] [blame] | 76 | . 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 |
| 79 | warnings may be present in the build. |
| 80 | |
| 81 | At this point all the basic functionality should be working such as Java class |
| 82 | inspection and running <<unit-tests,unit tests>>. |
Alice Kober-Sotzek | d9da89f | 2016-11-25 12:02:18 +0100 | [diff] [blame] | 83 | |
| 84 | TIP: The project data directory can be separate from the source code. One |
| 85 | advantage of this is that project files don't need to be excluded from version |
| 86 | control. |
| 87 | |
Alice Kober-Sotzek | 74d2f5b | 2016-11-25 14:04:53 +0100 | [diff] [blame] | 88 | == Recommended settings |
| 89 | |
| 90 | === Code style |
Alice Kober-Sotzek | 0b257d8 | 2017-03-02 11:58:36 +0100 | [diff] [blame] | 91 | |
| 92 | ==== google-java-format plugin |
| 93 | Install the `google-java-format` plugin by following these steps: |
| 94 | |
| 95 | . Go to *File -> Settings -> Plugins*. |
Gert van Dijk | beb687b | 2019-08-26 16:39:58 +0200 | [diff] [blame] | 96 | . Activate the *Marketplace* tab. |
| 97 | . Search for the plugin `google-java-format` by Google. |
Alice Kober-Sotzek | 0b257d8 | 2017-03-02 11:58:36 +0100 | [diff] [blame] | 98 | . Install it. |
Gert van Dijk | beb687b | 2019-08-26 16:39:58 +0200 | [diff] [blame] | 99 | . Restart IntelliJ IDEA. |
Alice Kober-Sotzek | 0b257d8 | 2017-03-02 11:58:36 +0100 | [diff] [blame] | 100 | |
Gert van Dijk | beb687b | 2019-08-26 16:39:58 +0200 | [diff] [blame] | 101 | Every time you start IntelliJ IDEA, make sure to use *Code -> Reformat with |
Alice Kober-Sotzek | 0b257d8 | 2017-03-02 11:58:36 +0100 | [diff] [blame] | 102 | google-java-format* on an arbitrary line of code. This replaces the default |
| 103 | CodeStyleManager with a custom one. Thus, uses of *Reformat Code* either via |
| 104 | *Code -> Reformat Code*, keyboard shortcuts, or the commit dialog will use the |
| 105 | custom style defined by the `google-java-format` plugin. |
| 106 | |
Gert van Dijk | beb687b | 2019-08-26 16:39:58 +0200 | [diff] [blame] | 107 | Please refer to the documentation on the <<dev-contributing#style,code style>> |
| 108 | for which version of `google-java-format` is used with Gerrit. |
| 109 | |
Alice Kober-Sotzek | 0b257d8 | 2017-03-02 11:58:36 +0100 | [diff] [blame] | 110 | ==== Code style settings |
| 111 | The `google-java-format` plugin is the preferred way to format the code. As it |
| 112 | only kicks in on demand, it's also recommended to have code style settings |
| 113 | which help to create properly formatted code as-you-go. Those settings can't |
| 114 | completely mimic the format enforced by the `google-java-format` plugin but try |
| 115 | to be as close as possible. So before submitting code, please make sure to run |
| 116 | *Reformat Code*. |
| 117 | |
| 118 | . Download |
| 119 | https://raw.githubusercontent.com/google/styleguide/gh-pages/intellij-java-google-style.xml[ |
| 120 | intellij-java-google-style.xml]. |
Alice Kober-Sotzek | 74d2f5b | 2016-11-25 14:04:53 +0100 | [diff] [blame] | 121 | . Go to *File -> Settings -> Editor -> Code Style*. |
Gert van Dijk | beb687b | 2019-08-26 16:39:58 +0200 | [diff] [blame] | 122 | . Click on the wrench icon with the tooltip _Show Scheme Actions_. |
| 123 | . Click on *Import Scheme*. |
Alice Kober-Sotzek | 0b257d8 | 2017-03-02 11:58:36 +0100 | [diff] [blame] | 124 | . Select the previously downloaded file `intellij-java-google-style.xml`. |
Gert van Dijk | beb687b | 2019-08-26 16:39:58 +0200 | [diff] [blame] | 125 | . Make sure that `GoogleStyle` is chosen as the current *Scheme*. |
Alice Kober-Sotzek | 74d2f5b | 2016-11-25 14:04:53 +0100 | [diff] [blame] | 126 | |
| 127 | In addition, the EditorConfig settings (which ensure a consistent style between |
| 128 | Eclipse, IntelliJ, and other editors) should be applied on top of that. Those |
| 129 | settings are in the file `.editorconfig` of the Gerrit source code. IntelliJ |
| 130 | will automatically pick up those settings if the EditorConfig plugin is enabled |
| 131 | and configured correctly as can be verified by: |
| 132 | |
| 133 | . Go to *File -> Settings -> Plugins*. |
Gert van Dijk | beb687b | 2019-08-26 16:39:58 +0200 | [diff] [blame] | 134 | . Ensure that the *EditorConfig* plugin (by JetBrains) is enabled. |
Alice Kober-Sotzek | 74d2f5b | 2016-11-25 14:04:53 +0100 | [diff] [blame] | 135 | . Go to *File -> Settings -> Editor -> Code Style*. |
| 136 | . Ensure that *Enable EditorConfig support* is checked. |
| 137 | |
| 138 | NOTE: If IntelliJ notifies you later on that the EditorConfig settings override |
| 139 | the code style settings, simply confirm that. |
| 140 | |
Alice Kober-Sotzek | b11a075 | 2016-11-25 16:08:43 +0100 | [diff] [blame] | 141 | === Copyright |
Gert van Dijk | beb687b | 2019-08-26 16:39:58 +0200 | [diff] [blame] | 142 | |
| 143 | . Copy the folder `$(gerrit_source_code)/tools/intellij/copyright` (not just the |
Alice Kober-Sotzek | b11a075 | 2016-11-25 16:08:43 +0100 | [diff] [blame] | 144 | contents) to `$(project_data_directory)/.idea`. If it already exists, replace |
Gert van Dijk | beb687b | 2019-08-26 16:39:58 +0200 | [diff] [blame] | 145 | it. If you didn't select a custom data directory the command could look like |
| 146 | this, as run from the Gerrit source tree checkout as working directory: |
| 147 | + |
| 148 | ---- |
| 149 | cp -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 | + |
| 154 | Only in case it hasn't picked up the copyright profile automatically, import |
| 155 | the `Gerrit_Copyright.xml` from that folder manually. |
Alice Kober-Sotzek | b11a075 | 2016-11-25 16:08:43 +0100 | [diff] [blame] | 156 | |
Gert van Dijk | beb687b | 2019-08-26 16:39:58 +0200 | [diff] [blame] | 157 | === Git integration |
| 158 | This section is only relevant in case you want to use the Git integration |
| 159 | plugin in IntelliJ IDEA. |
Alice Kober-Sotzek | b11a075 | 2016-11-25 16:08:43 +0100 | [diff] [blame] | 160 | |
Alice Kober-Sotzek | 6af5c3b | 2016-11-25 15:22:38 +0100 | [diff] [blame] | 161 | To 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 Dijk | beb687b | 2019-08-26 16:39:58 +0200 | [diff] [blame] | 164 | . 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 |
| 171 | right margin*. |
Alice Kober-Sotzek | 6af5c3b | 2016-11-25 15:22:38 +0100 | [diff] [blame] | 172 | |
| 173 | In addition, you should follow the instructions of |
| 174 | <<dev-contributing#git_commit_settings,this section>> (if you haven't |
| 175 | done so already): |
| 176 | |
Gert van Dijk | beb687b | 2019-08-26 16:39:58 +0200 | [diff] [blame] | 177 | * Install the Git commit message hook for the `Change-Id` line. |
Alice Kober-Sotzek | 6af5c3b | 2016-11-25 15:22:38 +0100 | [diff] [blame] | 178 | * Set up the HTTP access. |
| 179 | |
| 180 | Setting up the HTTP access will allow you to commit changes via IntelliJ without |
| 181 | specifying your credentials. The Git hook won't be noticeable during a commit |
| 182 | as it's executed after the commit dialog of IntelliJ was closed. |
| 183 | |
Alice Kober-Sotzek | d9da89f | 2016-11-25 12:02:18 +0100 | [diff] [blame] | 184 | == Run configurations |
Alice Kober-Sotzek | 1e7c17a | 2016-11-25 13:37:55 +0100 | [diff] [blame] | 185 | Run configurations can be accessed on the toolbar. To edit them or add new ones, |
| 186 | choose *Edit Configurations* on the drop-down list of the run configurations |
| 187 | or go to *Run -> Edit Configurations*. |
Alice Kober-Sotzek | d9da89f | 2016-11-25 12:02:18 +0100 | [diff] [blame] | 188 | |
Gert van Dijk | beb687b | 2019-08-26 16:39:58 +0200 | [diff] [blame] | 189 | [[runconfigurations-daemon]] |
David Pursehouse | b7f1b9c | 2019-08-30 14:04:27 +0900 | [diff] [blame] | 190 | === Gerrit Daemon |
| 191 | |
Gert van Dijk | beb687b | 2019-08-26 16:39:58 +0200 | [diff] [blame] | 192 | [WARNING] |
| 193 | ==== |
| 194 | At the moment running this (local) configuration results in a |
Alice Kober-Sotzek | 485a5ff | 2017-03-02 13:20:32 +0100 | [diff] [blame] | 195 | `java.io.FileNotFoundException`. To debug a local Gerrit server with IntelliJ, |
| 196 | use the instructions of <<dev-readme#run_daemon,Running the Daemon>> in |
| 197 | combination with <<remote-debug,Debugging a remote Gerrit server>>. |
| 198 | |
Gert van Dijk | beb687b | 2019-08-26 16:39:58 +0200 | [diff] [blame] | 199 | (link:https://bugs.chromium.org/p/gerrit/issues/detail?id=11360[Issue 11360]) |
| 200 | ==== |
| 201 | |
Alice Kober-Sotzek | d9da89f | 2016-11-25 12:02:18 +0100 | [diff] [blame] | 202 | Copy `$(gerrit_source_code)/tools/intellij/gerrit_daemon.xml` to |
| 203 | `$(project_data_directory)/.idea/runConfigurations/`. |
| 204 | |
| 205 | This run configuration starts the Gerrit daemon similarly as |
| 206 | <<dev-readme#run_daemon,Running the Daemon>>. |
| 207 | |
| 208 | NOTE: The <<dev-readme#init,Site Initialization>> has to be completed |
| 209 | before this run configuration works properly. |
Alice Kober-Sotzek | 1e7c17a | 2016-11-25 13:37:55 +0100 | [diff] [blame] | 210 | |
Gert van Dijk | beb687b | 2019-08-26 16:39:58 +0200 | [diff] [blame] | 211 | [[unit-tests]] |
Alice Kober-Sotzek | 1e7c17a | 2016-11-25 13:37:55 +0100 | [diff] [blame] | 212 | === Unit tests |
| 213 | To create run configurations for unit tests, run or debug them via a right-click |
| 214 | on a method, class, file, or package. The created run configuration is a |
Gert van Dijk | beb687b | 2019-08-26 16:39:58 +0200 | [diff] [blame] | 215 | temporary one and can be saved to make it permanent by selecting *Create |
| 216 | 'Bazel test [...]'...* from the context menu. |
Alice Kober-Sotzek | 1e7c17a | 2016-11-25 13:37:55 +0100 | [diff] [blame] | 217 | |
| 218 | Normally, this approach generates JUnit run configurations. When the Bazel |
| 219 | plugin manages a project, it intercepts the creation and creates a Bazel test |
| 220 | run configuration instead, which can be used just like the standard ones. |
| 221 | |
Alice Kober-Sotzek | 485a5ff | 2017-03-02 13:20:32 +0100 | [diff] [blame] | 222 | [[remote-debug]] |
| 223 | === Debugging a remote Gerrit server |
| 224 | If a remote Gerrit server is running and has opened a debug port, you can attach |
| 225 | IntelliJ via a `Remote debug configuration`. |
| 226 | |
| 227 | . Go to *Run -> Edit Configurations*. |
| 228 | . Click on the *+* to add a new configuration. |
Gert van Dijk | beb687b | 2019-08-26 16:39:58 +0200 | [diff] [blame] | 229 | . Choose *Remote* from the *Templates*. |
Alice Kober-Sotzek | 485a5ff | 2017-03-02 13:20:32 +0100 | [diff] [blame] | 230 | . Adjust *Configuration -> Settings -> Host* and *Port*. |
| 231 | . Start this configuration in `Debug` mode. |
| 232 | |
Gert van Dijk | beb687b | 2019-08-26 16:39:58 +0200 | [diff] [blame] | 233 | TIP: This run configuration dialog also shows the line for the JVM as startup |
| 234 | flag that you can copy to include in your |
| 235 | `$(gerrit_test_site)/etc/gerrit.config` in the `[container]` section in order |
| 236 | to work-around the <<runconfigurations-daemon,local run configuration issue>>. |
| 237 | |
Alice Kober-Sotzek | 5533281 | 2016-12-07 15:12:14 +0100 | [diff] [blame] | 238 | GERRIT |
| 239 | ------ |
| 240 | Part of link:index.html[Gerrit Code Review] |
| 241 | |
| 242 | SEARCHBOX |
| 243 | --------- |