Spring Boot provides the parent POM for an easier creation of Spring Boot applications.
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:
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.7.11</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:
<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:
<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:
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <version>2.4.0</version> </dependency> </dependencies> // ... </dependencyManagement>
By contrast, just declaring the version for the dependency outside the dependencyManagement tag will no longer work.
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.