DEV Community


Posted on • Updated on • Originally published at

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 life 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

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, whatever we prefer for now.

  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

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. That makes the log output 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. 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

In order to add our custom profile to the VM options, we have to activate it in the dropdown behind "Modify options" first! 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 ("base") included as well.

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 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

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 file application-local.example, ready to be copied and customized.

Further readings

Connecting Spring Boot to a database

Profiles in Spring Boot

Discussion (0)