Working with your Spring Boot application in IntelliJ

Integrating a Spring Boot application with IntelliJ is not difficult, as IntelliJ is designed for this scenario. However, there are a few things to consider that will make our lives easier.

Import your sources

Since we already have the first version of our CarParts microservice available, we use the import function of IntelliJ to create the project. This function is available either in the initial popup or, if we have already an opened project in IntelliJ, under "FileNewProject 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

Import project from existing sources

In the popup we select build.gradle (Gradle) 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.

Now we can already edit the application.yml and configure our database connection. Here we either use the environment variables (the part before the first colon, e.g. JDBC_DATABASE_URL) or adjust the fallback behind it, what we prefer for now.

Adjusting our database connection

Adjusting our database connection

Project settings

With a right click on the project and "Open Module Settings" we are at the project-specific settings - in our case we have to select the correct JDK for Java 8.

Selecting the JDK for our project

Selecting the JDK for our project

In the general settings ("File → Settings") there is also a preference for Gradle which I prefer to adjust. Under "Build → Build Tools → Gradle" the two settings "Build and run using" and "Run tests using" can be changed to IntelliJ.

Adjusting gradle preferences

Adjusting gradle preferences

Please also check out this article for the proper setup of Lombok in IntelliJ.

Starting the application

Now our Spring Boot application is ready to be launched. IntelliJ provides the build tasks directly - we can start the application with bootRun (Gradle) or spring-boot:run (Maven). But we can also start our application with our own RunConfiguration. For this purpose we create a new configuration of the type "Application" under "RunEdit Configurations" - the settings for our CarPart application are as follows.

Creating a run configuration for our Spring Boot microservice

Creating a run configuration for our Spring Boot microservice

If we have a multi-module application, we select the hightest module ("web") at "Use classpath of module".

Now we can start our application from IntelliJ! If everything works, our application should be available at http://localhost:8080/.

Using the local profile

If we don't want to commit our database settings to the version control, there is also a nice way around it. In the application configuration we already entered -Dspring.profiles.active=local and thus the profile "local" is automatically active. Therefore we can also store our database settings in a new file application-local.yml.

Adding application-local.yml

Adding application-local.yml

This file is automatically taken up by Spring Boot when the application starts with the corresponding profile. Since this file is listed in the .gitignore, it is not added to git and can be edited separately by each developer. Instead we may add another fileapplication-local.example, ready to be copied and customized.

Further readings

Connecting Spring Boot to a database
Profiles in Spring Boot