1
votes

I have couple of spring cloud projects and wish to put all common dependencies into my own parent pom too. Many samples shows how to do it with <dependencyManagement>. But in my case with spring-boot-starter-parent and org.springframework.cloud, it seems to be not working using dependency management as the parent has already become 'spring-boot-starter-parent' and dependency management is also there having org.springframework.cloud. Following is one of my spring cloud projects' pom files.

<groupId>com.demo</groupId>
<artifactId>demo-customer-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>demo-customer-service</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
    <relativePath /> 
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencyManagement>
    <dependencies>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Dalston.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>  

as above, there are two parents: org.springframework.boot starter parent and cloud. So how can I have my own parent ?

Any suggestion please how the parent and the child pom files should be ?

1
What do you mean by "So how can I have my own parent ?"miskender
@miroh . I mean, a parent pom file which includes org.springframework.cloud , org.springframework.boot dependecies along with other project specific depedencies like apache commons, database dependecies. Because those dependencies will be common to all child projects. As a result, I ll be able to remove versions defined in above pom.Débora
The pom you provided in your question can be a parent. If you delete dependencies section(it could still be a parent if you don't delete it).miskender
@miroh. Thanks for attention. Then 2 ques: 1. my parent has its own parent too ? I just ask to be clear more: is it a good practice ? 2. Why delete dependencies? because those dependencies will be common to many of child projects.Débora

1 Answers

2
votes

The pom you provided can perfeclty become a parent pom. Parent poms can have parent's themself also. It is commonly used and if you check the projects you are using on github they have chain of parents.
For example spring-cloud-build-dependencies has spring-boot-dependencies as parent. spring-boot-dependencies has spring-boot-build as parent.

Inside your parent pom, dependencies defined inside <dependencies></dependencies> will be added to all the childs of your parent. If all your childs are using these dependencies you can add them in this tag. (I generally add unit test dependencies here, but you can add anything)
Parent poms usualy located one folder above of childs pom.xml. (But <relativePath> can be used if it is elsewhere)

|-parent pom.xml
|-child project folder
|--child pom.xml