IntelliJ IDEA Knowledge Base

This knowledge base is for issues that pop-up when developing with IntelliJ IDEA.


Getting Started / Importing project into IntelliJ IDEA

Prerequisites: You must have installed and activated IntelliJ IDEA. You also must have cloned the git repo from GitHub (see the README file for the repository you want to work on). 

To import your project into IntelliJ IDEA:

  1. On the IntelliJ IDEA Welcome dialog, click "Import Project". Or choose New > Project from Existing Sources on the menu.
  2. Navigate to top-level directory of the git repository you want to work on (eg, openlmis-requisition/). Select that folder.
  3. Choose "Import project from external model" and select "Gradle" as the type of model (see screenshot).
  4. Select "Use gradle wrapper task configuration" (see screenshot).
  5. IntelliJ IDEA should give you notifications about detecting your Git version control (VCS) and detecting Spring.

Unit tests are finding missing symbols, works fine in gradle

Answer:  Ensure you have annotation processing on.  IntelliJ IDEA → Preferences → Search for "Annotation Processors" → On page check "Enable Annotation Processing".


No Git history

Answer:  Enable the VCS root in preferences.


Integration Tests have missing symbols / can't find source

Answer: `src/integration-tests/` aren't automatically recognized in Gradle as Java source.  Add them in preferences.


Lots of symbols, e.g. Entity, javax.*, etc can't be found

Answer:  Your IDEA files are likely messed-up and you need to re-generate them.  First remove any ipr, iws, and iml files from the project's root.  For good measure run gradle cleanIdea.  Next run gradle idea.  Do not run either of these from inside a Docker Container (e.g. openlmis/dev) or using docker-compose.


BuildConfig can't be found

Answer: Your IDEA doesn't recognize generated classes. In order to change that you need to in Project Window (Alt +1) right click on build → Mark directory as → Cancel Exclusion and again right click on build / gen / buildconfig / src / main → Mark directory as →Generated Source Root. If necessary click on class that can't be imported → alt + enter → select first option.
In order to unit test with IntelliJ you need temporarily copy build/gen/buildconfig/src/main/org.openlmis/BuildConfig.java to src/main/java/org.openlmis.


Use Java Google style in IntelliJ IDEA

OLD:  The above file is an old version, the latest is at:  https://github.com/google/styleguide/blob/gh-pages/intellij-java-google-style.xml


Download the above file and  place it in IntelliJ codestyle config directory (for Linux this will be similar to: /home/user/.ideaIC2016.1/config/codestyles). Restart IntelliJ if you have it started. import it as an intelliJ Idea code style XML. Go to File → Settings → Code Style and follow the screenshot below. 

Go to File → Settings → Code Style and from the dropdown list select scheme "GoogleStyle".

Running Unit Tests inside IntelliJ IDEA

Running tests in IntelliJ IDEA should work out of the box. If you are getting errors about permissions or Gradle not being able to create directories, the underlying issue is most likely file ownership. If you were running docker-compose as root, it is likely that root has became the owner of the .gradle directory in your project. In order to change ownership back to your user run:

sudo chown <user>:<user> -R .

in the project directory. <user> is your system username.


To find out how not to run Docker as root (e.g. sudo), see the docker cheat sheet.

Running Integration Tests inside IntelliJ IDEA

There are two main obstacles that keep the integration tests from being run outside of the dev docker container:

  • The application context does not know what 'db' and 'log' are, since they are defined properly inside the container. We need to point these to localhost or comment out the references.
  • The WireMock server has trouble running on port 80 outside of the dev docker container, so it needs to run on a different port (e.g. 8088).

Steps to work around these obstacles:

  • BaseWebIntegrationTest: change WireMockRule(80) to something like WireMockRule(8088).
  • application.properties: for auth.server.url, add the port number specified in BaseWebIntegrationTest to the URL (e.g. http://${VIRTUAL_HOST:localhost}:8088/auth/oauth/check_token).
  • applicationContext.xml: change serverName from db to localhost.
  • logback.xml: comment out all tags inside configuration except for the include tag.

IntelliJ reference card

Below is a collection of keyboard shortcuts for IntelliJ:

https://resources.jetbrains.com/assets/products/intellij-idea/IntelliJIDEA_ReferenceCard.pdf

OpenLMIS: the global initiative for powerful LMIS software