In the case of beginners spring boot
, the official example is for us to inherit a spring spring-boot-starter-parent , the parent:
<parent>
<groupId>Org.springframework.boot</groupId>
<artifactid>Spring-boot-starter-parent</artifactid>
<version>1.5.1.RELEASE</version></Parent>
<dependencies>
<dependency>
<groupId>Org.springframework.boot</groupId>
<artifactid>Spring-boot-starter-web</artifactid>
</Dependency>
</dependencies>
However, in general, in our own projects, we define our own parent project, in which case the above approach will not work. So, how do you do it? In fact, the spring's official website also gives a workaround:
In our own parent project, add the following declaration
<dependencymanagement>
<dependencies>
<dependency>
<groupId>Org.springframework.boot</groupId>
<artifactid>Spring-boot-dependencies</artifactid>
<version>1.5.1.RELEASE</version>
<type>Pom</type>
<scope>Import</Scope>
</Dependency>
</dependencies>
</dependencymanagement>
Please note that its type is pom,scope is import, and this type of dependency can only be The Dependencymanagement tag is declared.
Then, we can change the parent's declaration of the subproject in our project to the parent project of our own project, for example, mine is:
<parent>
<groupId>org.test</groupId>
<artifactId>spring</artifactId>
<version>0.1-SNAPSHOT</version>
</parent>
One thing you need to pay attention to.
In the dependencies of a subproject, you do not need (and cannot) add the declaration to Spring-boot-dependencies again, otherwise the subproject will not compile.
That is, in the subproject, the following configuration is superfluous:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
</dependency>
Why is it like this?
Because spring-boot-dependencies there is no corresponding jar package , it is just a pom configuration, you can go to the maven warehouse to see.
It defines a lot of dependency declarations.
So, with it, we don't need to declare version when we use dependencies on the subproject, such as:
<dependencies> <dependency>
<groupId>Org.springframework.boot</groupId>
<artifactid>Spring-boot-starter-web</artifactid>
</Dependency>
<dependency>
<groupId>Org.springframework.boot</groupId>
<artifactid>Spring-boot-starter-test</artifactid>
<scope>Test</Scope> </Dependency>
</dependencies>
For example, spring-boot-starter-web
and spring-boot-starter-test
in spring-boot-dependencies
the declarations are:
<dependency>
<groupId>Org.springframework.boot</groupId>
<artifactid>Spring-boot-starter-web</artifactid>
<version>1.5.1.RELEASE</version></Dependency>
<dependency>
<groupId>Org.springframework.boot</groupId>
<artifactid>Spring-boot-starter-test</artifactid>
<version>1.5.1.RELEASE</version> <exclusions>
<exclusion>
<groupId>Commons-logging</groupId>
<artifactid>Commons-logging</artifactid>
</exclusion>
</exclusions>
</Dependency>
Reference documents
-Spring Official documentation
Spring Boot does not use the default parent and instead uses the parent of its own project