Sandeep
Sandeep

Reputation: 1401

Why both spring-boot-starter-parent and spring-boot-starter-web are needed for Spring Boot application pom?

I am following a video tutorial on Spring Boot (from javabrains.io). The pom file for the sample project contains a parent block with groupId as org.springframework.boot and artifactId as spring-boot-starter-parent.

Additionally it contains a dependency block with groupId as org.springframework.boot and artifactId as spring-boot-starter-web.

Q) Why do we need both elements (i.e. parent and dependency) in our pom.xml?

I thought that since the project pom inherits from spring-boot-starter-parent, all the dependencies will be automatically inherited as well.


The sample project pom.xml file is as follows:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>io.javabrains.springbootquickstart</groupId>
    <artifactId>course-api</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Java Brains Course API</name>

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

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

    <properties>
        <java.version>1.8</java.version>
    </properties>
</project>

Upvotes: 3

Views: 4359

Answers (1)

Krisz
Krisz

Reputation: 2264

If you check the the spring-boot-starter-parent pom file, you'll see that it provides default properties and maven plugin configuration, whereas spring-boot-starter-web provides web-related spring dependencies without any additional configuration. Furthermore, both starters inherit from spring-boot-dependencies, which defines a list of dependencies that spring supports. This allows you to omit the version for any of these dependencies in your build configuration. You can learn more by reading the official documentation.

So to summarize, spring-boot-starter-parent provides

  • default maven plugin setup
  • default maven properties
  • dependency management

Whereas spring-boot-starter-web pulls in web-related dependencies.

Upvotes: 5

Related Questions