Spring Boot Projects as Maven Sub-Modules

  • Post author:
  • Post category:Java / Spring

The easiest ways to setup a Spring Boot project with Maven is to define spring-boot-starter-parent as the project’s parent in you POM file:

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

This way, you will inherit some useful dependencies and other definitions.

But there are situations when you need to use a different parent, for example if your Spring Boot project is supposed to be a sub-module in a larger project.

In this case, you can still import the above mentioned dependencies by adding spring-boot-dependencies as a dependency in the <dependencyManagement> section of your POM file:

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

But you will find that your integration tests, usually managed by the Maven Failsafe plugin, will now fail with an error:

Unable to find a @SpringBootConfiguration

To resolve this, you need to add the following <classesDirectory> configuration to the Failsafe plugin definition in your POM file:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-failsafe-plugin</artifactId>
  <version>2.22.2</version>
  <executions>
    <execution>
      <goals>
        <goal>integration-test</goal>
           <goal>verify</goal>
        </goals>
    </execution>
 </executions>
 <configuration>
   <classesDirectory>${project.build.outputDirectory}</classesDirectory>
 </configuration>
</plugin>

You can confirm that this definition is in fact part of the spring-boot-starter-parent‘s POM by looking into it!