In this post, I would 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?
- 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:
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:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>bswen-project</artifactId> <groupId>bswen-project</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>spring-boot-jasypt</artifactId> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> <start-class>SbjApp</start-class> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>1.4.3.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>3.0.3</version> </dependency> </dependencies> </project>
This is the dependency tree resolved by IntellijIDEA:
You can see that there is no spring boot dependency in our project. Why?
We should add at least one spring boot starter dependency to this project, just as follows:
<dependencies> <dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>3.0.3</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies>
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:
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;
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.