Spring Boot does not use the default parent and instead uses the parent of its own project

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.