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":
Navigate to the top-level pom.xml of the starter app:
Acknowledge that the containing directory can be trusted:
Project opened:
Use
to confirm that JDK 11 is configured: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.
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).
If using JDO/DataNucleus, this setting enables the generation of the It is also required for frameworks such as Lombok. |
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:
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:
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: |
Then, set up the run configuration for 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.
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 (!).
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:
There are also tests in the webapp-tests
module:
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):