1. Overview

In this article, we learn how we can include or configure maven dependency without parent pom. In some organization might have their own parent pom so we can not use multiple parent pom in the same maven project. Using parent pom is automatically maintaining versions of dependencies, for example in the below example, we have added spring-boot-starter-web but haven’t specified the version but still, spring boot will automatically take an appropriate version of spring-boot-starter-web. Spring also provides advantages without parent pom as well.

2. Without parent pom configurations

We require two updates in pom.xml as like:

  1. Include spring-boot-dependencies with the appropriate version and make sure that scope=import which help us to maintain dependency management.
  2. Add repackage in the execution goals to generate jar or war based on spring boot packaging format. Otherwise, it may generate an error like  no main manifest attribute, in spring-boot-example-1.0-SNAPSHOT.jar when we try to run the application using java -jar spring-boot-example-1.0-SNAPSHOT.jar
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://maven.apache.org/POM/4.0.0"
         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>spring-boot-example</groupId>
    <artifactId>spring-boot-example</artifactId>
    <version>1.0-SNAPSHOT</version>
    <description>Spring boot without parent pom</description>
    <!-- Inherit defaults from Spring Boot -->
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <spring.boot.version>2.1.5.RELEASE</spring.boot.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring.boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <!-- Package as an executable jar -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring.boot.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

3. Conclusion

In this article, we learned that we can configure spring boot application without spring-boot-starter-parent parent pom. We also learned that the main advantage of parent pom for dependency and it’s version management.

4. Source Code

5. References

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *