Using Spring Boot without inheriting parent starter pom

[Updated: May 29, 2017, Created: Dec 2, 2016]

In the last tutorial we saw how to use spring starters dependencies by inheriting spring-boot-starter-parent. As maven project can only inherit from a single parent, sometimes it might not be possible to inherit our project from the spring-boot-starter-parent pom. There we have another maven specific option: import spring-boot-dependencies which is the parent of spring-boot-starter-parent. Importing dependencies is just like copying/pasting the target content. Also 'import' scope works only within <dependencyManagement>

Let's understand that by modifying our last example.

This time we are going to use maven's <dependencyManagement> and then include spring-boot-dependencies as dependency with scope=import and type=pom.

<?xml version="1.0" encoding="UTF-8"?>
<project ......>
 <modelVersion>4.0.0</modelVersion>

 <groupId>com.logicbig.example</groupId>
 <artifactId>spring-boot-parent-pom-import</artifactId>
 <version>1.0-SNAPSHOT</version>

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

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

</project>


Our Java code is still the same from the last tutorial example:

@SpringBootApplication
public class ApplicationExample {
    @Bean
    public MyBean myBean () {
        return new MyBean();
    }

    public static void main (String[] args) {
        ApplicationContext ctx =
                            SpringApplication.run(ApplicationExample.class, args);
        MyBean bean = ctx.getBean(MyBean.class);
        bean.doSomething();
    }

    private static class MyBean {

        public void doSomething () {
            Bag bag = new HashBag();
            bag.add("ONE", 6);
            System.out.println("Doing something in MyBean");
            System.out.println(bag);
        }
    }
}

Output

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.4.2.RELEASE)

2016-12-02 19:12:02.500  INFO 14112 --- [           main] com.logicbig.example.ApplicationExample  : Starting ApplicationExample on JoeMsi with PID 14112 (D:\LogicBig\example-projects\spring-boot\spring-boot-parent-pom-import\target\classes started by Joe in D:\LogicBig\example-projects\spring\spring-core-validation)
2016-12-02 19:12:02.503  INFO 14112 --- [           main] com.logicbig.example.ApplicationExample  : No active profile set, falling back to default profiles: default
2016-12-02 19:12:02.549  INFO 14112 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@df27fae: startup date [Fri Dec 02 19:12:02 CST 2016]; root of context hierarchy
2016-12-02 19:12:03.600  INFO 14112 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2016-12-02 19:12:03.611  INFO 14112 --- [           main] com.logicbig.example.ApplicationExample  : Started ApplicationExample in 1.35 seconds (JVM running for 1.661)
Doing something in MyBean
[6:ONE]
2016-12-02 19:12:03.616  INFO 14112 --- [       Thread-1] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@df27fae: startup date [Fri Dec 02 19:12:02 CST 2016]; root of context hierarchy
2016-12-02 19:12:03.616  INFO 14112 --- [       Thread-1] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown


Inheriting vs Importing parent starter

We may prefer to use importing the parent rather than inheriting form it for many reasons. One of them could be: we already may have another parent, which we want to inherit from. Or we just don't want to inherit from a framework pom because of our design policies. With 'import' option, we can still keep the benefits of the dependency management, but not the plugin management. We have to include the plugins ourselves if we are importing the starter.

Example Project

Dependencies and Technologies Used :

  • Spring Boot Starter 1.4.2.RELEASE: Core starter, including auto-configuration support, logging and YAML.
    Corresponding Spring version: 4.3.2.RELEASE
  • Commons Collections 3.2.1: Types that extend and augment the Java Collections Framework.
  • JDK 1.8
  • Maven 3.3.9

Spring Boot Starters Select All Download
  • spring-boot-parent-pom-import
    • src
      • main
        • java
          • com
            • logicbig
              • example

See Also