Use Maven In Intellij IDEA

 

This page is outdated and no longer receives updates!

Use Maven in Intellij IDEA

I am going to demonstrate how I use Maven in Intellij IDEA. You are welcome to edit this page if you find anything that's useful or beneficial to developers.

 

Requirement:

  1. # Intellij IDEA 9 with Maven plugins enabled. It is enabled by default.

  1. Apache Maven 2.2.1. Download maven from maven.apache.org and follow the installation instruction.

Checkout Source and Create Project:







  1. Clone the repository, select a parent directory





  2. IntelliJ will give a prompt to configure the detected frameworks





Develop with Maven

There are three modules defined in maven project, openmrs-api,openmrs-web and openmrs-webapp. OpenMrs is a parent project. When we run any maven goal on parent project, it will run the same goal on its children according to the dependency order. Common maven goals are shown under Lifecycle node. There are also goals provided by maven plugins which are shown under Plugins node.

How to run Junit

If you want to run all tests across modules, click test under Lifecycle node in Openmrs project.

If you want to run all tests in one module, click test under Lifecycle node in that module.

If you want to run a single test class,right click on test class on project view and click run, or open the test class and move the mouse outside of test methods and right click to run.

If you want to run a single test method, open the test class and move the mouse inside test method or on test method name and right click to run

By default IDEA will build project instead of using maven test-compile goal before running junit test. To change it you can click menu "Run -> Edit Configuration", click "Edit defaults" and select "Junit", Uncheck make under before launch section and save.

You can also create IDEA run configuration to run junit tests. For example to create a run configuration that runs all junit tests across modules, you can click menu "Run -> Edit Configuration", click + and pick junit, type name "All Tests", select "All in package", select search for tests "In whole project", select "openmrs:test-compile" in before launch section and save. The pro of this is that it uses native IDE test view, and you can navigate to the line where error happens.

How to Run Web Application

Create a run configuration by click "Run -> Edit Configuration", click + and pick "Maven", type Name "OpenMRS", select working directory to be the root of webapp module, type goal "jetty:run" and save. Now you select "OpenMRS" and run or debug it.

 

Current configuration of Jetty is that it will reload static resources like jsp, javascript files etc. If there is any java class change, It will require to run maven:compile goal and restart Jetty.

How to Debug Web Application

Jetty plugin can pick up any changes of static resources, so changes of jsp, property or css files don't require a restart. However changes of java class will have to restart. The following solutions could help in some situations.

JVM Hotswap

If you only change things in a method, you can use JVM Hotswap. In IDEA, you click "Build -> Compile.." which will compile only the class you modify and IDEA will provide hotswap dialog. Currently IDEA doesn't support hotswap if you use maven to compile.

Hot Deploy

There is memory leak when hot deploying web application. But if you increase JVM permgen, you can still do it.

Use JRebel

Check this http://www.zeroturnaround.com/jrebel/comparison, You can see Jrebel can do a lot more than JVM Hotswap does. Here is how you configure JRebel to work with OpenMRS

  1. Install JRebel plugin for Intellij IDEA and restart.

     



    In Settings > Debugger > Java ; Untick "Synthetic Fields"




    In Settings > Debugger > Stepping ; Tick "Skip Synthetic Methods"




    Tick "Skip synthetic methods"

  2. Configure JRebel by running JRebel Configuration Wizard.


    Select "Intellij 8x or later", tick "Use maven to build my application", tick "I run the server from my IDE"


    Untick "Spring Framework Plugin"

  3. Run or Debug "OpenMRS" with JRebel from "Run -> Run/Debug with JRebel"

When you change any java classes, just run maven:compile and reload the web page, and JRebel will reload the class for you.