SpringBootDependencyManagementwithaCustomParent

编程

However, using the parent POM may not always be desirable, if we already have a parent to inherit from.

In this quick tutorial, we"re going to take a look at how we can still use Boot without the parent starter.

2. Spring Boot Without Parent POM

The parent pom.xml takes care of dependency and plugin management. For that reason, inheriting from it provides valuable support in an application, so it is usually the preferred course of action when creating a Boot application. You can find more details on how to build an application based on the parent starter in our previous article.

In practice though, we may be constrained by design rules or other preferences to use a different parent.

Fortunately, Spring Boot offers an alternative to inheriting from the parent starter, that can still afford us some of its advantages.

If we don"t make use of the parent POM, we can still benefit from dependency management by adding the spring-boot-dependencies artifact with scope=import:

1

2

3

4

5

6

7

8

9

10

11

<

dependencyManagement

>

     

<

dependencies

>

        

<

dependency

>

            

<

groupId

>org.springframework.boot</

groupId

>

            

<

artifactId

>spring-boot-dependencies</

artifactId

>

            

<

version

>1.5.6.RELEASE</

version

>

            

<

type

>pom</

type

>

            

<

scope

>import</

scope

>

        

</

dependency

>

    

</

dependencies

>

</

dependencyManagement

>

Next, we can start simply start adding Spring dependencies and making use of Spring Boot features:

1

2

3

4

<

dependency

>

    

<

groupId

>org.springframework.boot</

groupId

>

    

<

artifactId

>spring-boot-starter-web</

artifactId

>

</

dependency

>

On the other hand, without the parent POM, we no longer benefit from plugin management. This means we need to add the spring-boot-maven-plugin explicitly:

1

2

3

4

5

6

7

8

<

build

>

    

<

plugins

>

        

<

plugin

>

            

<

groupId

>org.springframework.boot</

groupId

>

            

<

artifactId

>spring-boot-maven-plugin</

artifactId

>

        

</

plugin

>

    

</

plugins

>

</

build

>

3. Overriding Dependency Versions

If we want to use a different version for a certain dependency than the one managed by Boot, we need to declare it in the dependencyManagement section, before spring-boot-dependencies is declared:

1

2

3

4

5

6

7

8

9

10

<

dependencyManagement

>

    

<

dependencies

>

        

<

dependency

>

            

<

groupId

>org.springframework.boot</

groupId

>

            

<

artifactId

>spring-boot-starter-data-jpa</

artifactId

>

            

<

version

>1.5.5.RELEASE</

version

>

        

</

dependency

>

    

</

dependencies

>

    

// ...

</

dependencyManagement

>

By contrast, just declaring the version for the dependency outside the dependencyManagement tag will no longer work.

4. Conclusion

In this quick tutorial, we"ve seen how we can use Spring Boot without the parent pom.xml.

The source code for the examples can be found over on GitHub.

以上是 SpringBootDependencyManagementwithaCustomParent 的全部内容, 来源链接: utcz.com/z/513031.html

回到顶部