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>

Note that its type is Pom,scope is import, and this type of dependency can only be declared in the dependencymanagement tag.

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 , spring-boot-starter-test and spring-boot-dependencies in 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>

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.