Install and Configure IntelliJ for OpenMRS Development

Install and Configure IntelliJ for OpenMRS Development

OpenMRS code follows specific format. There is also a OpenMRS live template to help you save some typing effort. To take advantage of auto formatting and openmrs code templates, you need to follow the instructions below to configure IntelliJ.

Download and install IntelliJ

  1. Windows

  2. Linux

Edit the file {idea_install_dir}/bin/idea.vmoptions

  • Your {idea_install_dir} may be /snap/intellij-idea-community/NNN

  • If you can’t find idea.vmoptions, look for  idea64.vmoptions

If you have 3GB RAM, the suggested JVM startup parameters are:

-Xms64m

-Xmx1024m

-XX:MaxPermSize=400m

-ea

Clone openmrs-core

To configure code format in IntelliJ, you will need two xml files in the openmrs-core repository. To get these two files, you need to clone openmrs-core repository to your local drive.

  1. Sign in to your Github account.

  2. Go to: https://github.com/openmrs/openmrs-core, fork the repository.

  3. Run the following command on your computer:

    $ git clone https://github.com/{your_github_id}/openmrs-core

Configure IntelliJ for OpenMRS

Configure Eclipse OpenMRS Code Formatter

  1. Click on the “Open Project” button

  2. Open the openmrs-core folder

  3. Select File > Settings.

  4. In the panel on the left, click on “Plugins”

  5. In the search bar on the right, enter “eclipse code formatter”

  6. In “Adapter for Eclipse…” click the “Install” button

  7. Click “Accept”

  8. Click “Restart”

  9. Select File > Settings.

  10. Click on “Adapter for Eclipse…” at the bottom of the left pane

  11. Click on the “Use Eclipse’s Code Formatter” button (below the “Disabled” button)

  12. Select Eclipse workspace/project folder or config file,  and browse to your openmrs-core folder, e.g.:
    /openmrs-core/tools/src/main/resources/eclipse/OpenMRSFormatter.xml

  13. Unselect Optimize Imports. Keep everything else to default.

  14. Select OK to apply the changes.  
    From now on you can let IntelliJ format any java class for you. See IntelliJ - Reformatting Source Code for details.

Define Java Import Order

In order for IntelliJ to order the java imports according to the OpenMRS style and remove unused imports do the following:

  1. Select File > Settings. In the top left search box enter code style

  2. Select Code Style > Java.

  3. Select the settings icon (to the right of “Scheme:”) and select Import Scheme > Eclipse XML Profile.
    Note: We are using the Eclipse XML Profile NOT IntelliJ's!

  4. Browse to  /openmrs-core/tools/src/main/resources/eclipse, select OpenMRSFormatter.xml.

  5. Click “OK”

  6. Select the Imports tab.

  7. Set both Class count to use import with * and Names count to use static import with * to a high enough number like 99

  8. Change the Import Layout to match the configuration as shown, so that the order of packages is as follows. Select each row and use the up and down arrows to change the order.

    1. Import static all other imports

    2. <blank line>,

    3. import javax.*,

    4. import java.*,   

    5. <blank line>

    6. import all other imports.

    7. Import module imports (?)

  9. Click “OK”

    From now on you can let IntelliJ format and remove unused imports in any java class for you. Read IntelliJ - Optimize Imports.

Configure XML Code Format

The OpenMRSFormatter.xml only handles the Java formatting (limitation of Eclipse), you also need to configure the XML Editor.

  1. Select File > Settings. In the search box, enter xml

  2.  Select Editor > Code Style > XML 

  3. Select the Tabs and Indents tab, select the Use tab character checkbox.

  4. Select  the Other tab, set the Hard wrap at  to 125 columns.
    This is the maximum line length we choose for XML, which is equal to the one we set for Java files. For Java this is ensured by the Checkstyle - LineLength so please update this number here if the openmrs-core/checkstyle.xml setting changes.

Set Up OpenMRS Live Templates

To save you some typing and focus on solving problems, we put together a few code snippets/templates, which are called Live Templates in IntelliJ. Once you imported them, you can type short keywords and IntelliJ inserts the appropriate template code with some variables automatically. You can then adjust the snippet to your use case.

  1. To import the templates copy the openmrs-core/tools/src/main/resources/intellij/templates.xml file to configuration directory/templates. 

  2. Restart IntelliJ, select File > Settings and enter templates in the search box. 

  3. Select Editor > Code Style > Live Templates.
    On the left pane, OpenMRS displays as one of the live template groups.

When you select OpenMRS, all the templates belong to this group display. You can easily see the definition of each template and enable/disable the templates.

You can also get the names of all snippets by executing the following:

  • On Linux:

    cat tools/src/main/resources/intellij/templates.xml | grep "template name"
  • On Windows:

    openmrs-core\tools\src\main\resources\intellij\templates.xml | find “template name”

    Now you’ve completed the configuration of IntelliJ. To learn more about IntelliJ go to How to Set up and Use IntelliJ.