Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot - parent pom when you already have a parent pom

People also ask

Can POM xml have two parents?

You can achieve multiple inheritance with profiles: You create (multiple) profiles in the root pom, and auto activate any variation of these profiles achieves multiple inheritance of maven configuration. This is not ideal because child projects cannot control the activation of profiles defined in the parent POM.

What is the parent POM in Maven POM xml for spring boot?

All Spring Boot projects use spring-boot-starter-parent as a parent in pom. xml file. Configuration: It allows us to maintain consistency of Java Version and other related properties.

Can parent POM have dependencies?

Now child POM need to refer the parent POM using parent tag and specifying groupId/artifactId/version attributes. This pom file will inherit all properties and dependencies from parent POM and additionally can include extra sub-project specific dependencies as well.


You can use the spring-boot-starter-parent like a "bom" (c.f. Spring and Jersey other projects that support this feature now), and include it only in the dependency management section with scope=import.That way you get a lot of the benefits of using it (i.e. dependency management) without replacing the settings in your actual parent.

The 2 main other things it does are

  1. define a load of properties for quickly setting versions of dependencies that you want to override
  2. configure some plugins with default configuration (principally the Spring Boot maven plugin). So those are the things you will have to do manually if you use your own parent.

Example provided in Spring Boot documentation:

<dependencyManagement>
    <dependencies>
        <dependency>
            <!-- Import dependency management from Spring Boot -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.1.3.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

Update 2018-01-04 with 1.5.9.RELEASE.

I have full code and runable example here https://www.surasint.com/spring-boot-with-no-parent-example/

You need this as a basic

   <dependencyManagement>
        <dependencies>
            <dependency>
                <!-- Import dependency management from Spring Boot -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${springframework.boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

But that is not enough, you also need explicitly define goal for spring-boot-maven-plugin (If you use Spring Boot as parent, you do not have to explicitly define this)

    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>${springframework.boot.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>

Otherwise you cannot build as executable jar or war.

Not yet, if you are using JSP, you need to have this:

<properties>    
  <failOnMissingWebXml>false</failOnMissingWebXml>
</properties>

Otherwise, you will get this error message:

    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war (default-war) on project spring-boot-09: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executi
ng in update mode) -> [Help 1]

NO NO , this is still not enough if you are using Maven Profile and Resource Filter with Spring Boot with "@" instead of "${}" (like this example https://www.surasint.com/spring-boot-maven-resource-filter/). Then you need to explicitly add this in

    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>

And this in

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.7</version>
            <configuration>
                <delimiters>
                    <delimiter>@</delimiter>
                </delimiters>
                <useDefaultDelimiters>false</useDefaultDelimiters>
            </configuration>
        </plugin>

See the example in the link https://www.surasint.com/spring-boot-with-no-parent-example/.