Last updated: 2024-07-05
Working with your Spring Boot application in IntelliJ
Working with a Spring Boot application in IntelliJ is not difficult, as IntelliJ is designed for this scenario. However, there are a few things to consider that will make our life easier.
Import your sources
In order to add our newly created Spring Boot application to Intellij, we can use the import function to create the project. This function is available either in the initial popup or, if we already have an opened project in IntelliJ, under File → New → Project from existing sources
. A project in IntelliJ can include several applications (so-called modules). In case we want to add our source code to an existing project, we can do this with File → New → Modules from existing sources
.
Import project from existing sources
In the popup we select build.gradle
(Gradle), build.gradle.kts
(Kotlin) or pom.xml
(Maven) - IntelliJ will then automatically recognize everything else. If we have several pom.xml
in our project, we select the top one.
Project settings
With a right click on the newly imported project and Open Module Settings
we can edit the project-specific settings. In case it's not already selected, we have to provide and select the JDK for Java 17, as this is the minimum requirement for Spring Boot 3.
Selecting the JDK for our project
Since Spring 6 the -parameters
flag is recommended for compiling Java source code, otherwise a warning will be logged. We can add this parameter under Build, Execution, Deployment → Compiler → Java Compiler
in the field Additional command line parameters
.
Warning about missing -parameters flag, replaced with an exception in future Spring versions
In the general settings under File → Settings
there is also an interesting preference for Gradle projects. Under Build → Build Tools → Gradle
the two settings "Build and run using" as well as "Run tests using" can be changed to IntelliJ. That will make the log output during testing and development more consistent.
Adjusting gradle preferences
Please also check out this article for the proper setup of Lombok in IntelliJ.
Run Configuration
Now our Spring Boot application is ready to be launched. The build tasks are listed in the Gradle or Maven section in IntelliJ - we could start the application with bootRun
(Gradle) or spring-boot:run
(Maven). We can also start our application with our own RunConfiguration. For this purpose we create a new configuration of the type "Application" under Run → Edit Configurations
- the settings for our application are as follows.
Creating a run configuration for our Spring Boot microservice
In order to add the Spring Boot profile local
as a VM option, we have to activate it in the dropdown behind "Modify options" first. Now we insert -Dspring.profiles.active=local
in the respecive field.
If we have a multi-module application, we select the hightest module web.main
at "Use classpath of module". In this way, we have all references modules (like base
) included as well.
Now we are ready to start our application in IntelliJ! After configuring our database connection, our application is available at http://localhost:8080/
.