Using IntelliJ

This section describes how to install and setup JetBrains' IntelliJ IDEA, then how to import an application into IntelliJ and run it.

Installing and Importing

This section covers installation and setup. These notes/screenshots were prepared using IntelliJ IDEA Community Edition 2023.1.2, but is believed to be compatible with more recent versions/other editions of the IDE.

Download and Install

Download latest version of IntelliJ Community Edition, and install.

Import Project

In IntelliJ a project can contain multiple modules; these need not be physically located together.

Download either the HelloWorld or SimpleApp starter app to your filesystem.

From the welcome screen, select "open project":

010 welcome

Navigate to the top-level pom.xml of the starter app:

020 import pom

Acknowledge that the containing directory can be trusted:

025 trust directory

Project opened:

030 imported

Use File  Project Structure to confirm that JDK 11 is configured:

040 project structure

Configuring

Compiler Settings

There are a number of compiler settings that influence the compiler. We highly recommend you set these.

On the Compiler Settings page, ensure that build automatically is enabled, and optionally compile independent modules in parallel. You might also want to increase the heap size, eg to 2048.

010 build automatically
Figure 1. IntelliJ Compiler Settings

On the Annotation Processors page, confirm that these are enabled for all modules with the …​ sources directory correctly defined. (In most recent editions of IntelliJ, this is detected automatically; no changes are required).

020 annotation processor
Figure 2. IntelliJ Annotation Processor Settings

If using JDO/DataNucleus, this setting enables the generation of the Q* classes for DataNucleus type-safe queries.

It is also required for frameworks such as Lombok.

Editor Settings

On the Auto Import settings page, check the optimize imports on the fly and add unambiguous imports on the fly

010 auto import
Figure 3. IntelliJ Auto Import Setting

Plugins

You might want to set up some additional plugins. You can do this using File > Settings > Plugins (or equivalently File > Other Settings > Configure Plugins).

Highly recommended are:

  • Lombok plugin (bundled with the IDE, and pre-enabled)

    If you plan to use Project Lombok to reduce boilerplate.

  • Maven Helper plugin

  • AsciiDoctor plugin

    Extremely useful if you are doing any authoring of documents (plugin’s git repo is here)

  • diagrams.net plugin * Allows diagrams to be authored within the IDE; works well with the AsciiDoctor plugin.

Running

Let’s see how to run both the app and the tests.

We run the application by creating a Run configuration, using Run > Edit Configurations.

There is one complication, which is the ORM. If the app uses JPA, then dynamic class weaving should be configured. If the app uses JDO, then Datanucleus enhancer should be configured.

Running the App (JPA)

The screenshot below shows a run configuration for JPA:

012 run configuration jpa

With JPA, the classes need to be "weaved" in order to support lazy loading and (more performant) dirty object tracking. This is typically done dynamically at runtime, using a Java agent. The SimpleApp and HelloWorld starter apps demonstrate this, bundling the spring-instrument.jar file. To run, use:

-javaagent:lib/spring-instrument.jar

as a JVM argument (where the system properties also are located.)

Running the App (JDO)

DataNucleus requires that all entities are bytecode enhanced. When building from the command line using Maven, the datanucleus:enhance Maven plugin takes care of this.

We can just have IntelliJ run the enhance as a separate run configuration before the run configuration that runs the app itself.

First, set up the run configuration to do the enhance:

005 datanucleus enhance run configuration
Figure 4. Run Configuration to enhance the entities
Check "work offline" (in Maven Options) to speed this up slightly.

If on Windows you encounter "The command line is too long", then set '-Dfork=false' as a VM option, in Java options:

006 datanucleus enhance run configuration fork false

Then, set up the run configuration for the app:

010 run configuration
Figure 5. Run Configuration to run the app

Note how it runs the "enhance" configuration first, specified using "Modify options".

Because we are using Maven to enhance the app, build the app at least once from the Maven command line:

mvn install -DskipTests -T1C

You should now be able to run the app using Run > Run Configuration. The same configuration can also be used to debug the app if you so need.

Dynamic Enhancement ?

Rather than statically enhancing the classes, an alternative — at least in theory — is to enhance the classes dynamically, at runtime.

There is an IntelliJ plugin (not tested) that purports to support this, but (at time of writing) is out of date and only supports older versions of IntelliJ.

Running the Unit Tests

The easiest way to run the unit tests is just to right click on the relevant package in the Project Window, and choose run unit tests. Hopefully your tests will pass (!).

030 running unit tests
Figure 6. Running Unit Tests from Project Explorer

As a side-effect, this will create a run configuration, very similar to the one we manually created for the main app.

Running the Integration Tests

Integration tests can be run in the same way as unit tests.

One approach is to initially run the tests use the right click on the integtests module:

045 running integ tests
Figure 7. Running Integ Tests from Project Explorer

There are also tests in the webapp-tests module:

046 running integ tests webapp tests

If the app uses JDO, then the tests might fail because the entities won’t have been enhanced. However, we can then easily update the automatically-created run configuration to run the datanucleus enhancer goal (same as when running the application):

050 running integration tests run configuration
Figure 8. Integration Tests Run Configuration