How to resolve the problem of import or resolve spring boot dependencies automatically in IntellijIDEA ?
1. Purpose
In this post, I will demo how to solve the problem of importing or resolving the spring boot dependencies in IntellijIDEA. We have correctly add spring boot dependencies in maven or gradle, but IntelliJIDEA still can not import the spring boot packages. Why?
2. Environment
- Spring boot
3. The problem and solution
3.1 The problem
Here is the structure of our simple spring boot project (The project name is highlighted: spring-boot-jasypt
):
You can see that this project only contains one class named SbjApp
and a pom.xml
. It’s very simple.
We have written a spring boot main class ,just as the right side of the picture, it just add the annotation @SpringBootApplication
in the main class. But the IDE IntelljIDEA
can not recgonize the annotation @SpringBootApplication
and its font color is red!!!
Why did this happen, we have added the dependencies of spring boot in our pom.xml , just as follows:
This is the dependency tree resolved by IntellijIDEA:
You can see that there is no spring boot dependency in our project. Why?
3.2 Solution
We should add at least one spring boot starter dependency to this project, just as follows:
You can refresh the dependencies of this project, it would be like this:
You can see that the spring boot dependencies are added to this project properly. Then our main class code works:
You can see that IntellijIDEA now can recognize this spring boot annotation @SpringBootApplication
, we can now import the spring boot dependency packages in our project now!
It works! But why?
3.3 The reason
Our problem was solved by adding a spring boot starter
to our project, so what’s a spring boot starter?
According to this document:
Spring Boot provides a number of starters that allow us to add jars in the classpath. Spring Boot built-in starters make development easier and rapid. Spring Boot Starters are the dependency descriptors.
In the Spring Boot Framework, all the starters follow a similar naming pattern: spring-boot-starter-*, where ***** denotes a particular type of application. For example, if we want to use Spring and JPA for database access, we need to include the spring-boot-starter-data-jpa dependency in our pom.xml file of the project.
Just as the following picture shows, a spring-boot-starter-web
contains multiple modules and dependencies:
For spring-boot-starter
, it’s a core stater which only contains the core of spring-boot ,spring-boot-starter
is used for core starter, including auto-configuration support, logging, and YAML.
If we only use dependencyManagement
of maven to import spring boot dependencies, we would not get the real dependencies added to our project, because:
In dependencyManagement, only the dependencies are declared, and the introduction is not automatically improted, so the dependencies that need to be declared. If the dependency is not declared in the project, it will not be imported;
4. Summary
In this post, I tried to explain why the dependency import is failing when we only use dependencyManagement
with maven, we need to declare the real dependencies in maven to import the spring boot dependencies.
Final Words + More Resources
My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope:
Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!