1. Overview
Spring Boot provides the parent POM for the easier creation of Spring Boot applications.
Not everyone likes inheriting from the spring-boot-starter-parent
POM to create an executable jar/war. You may have your own corporate standard parent that you need to use, or you may just prefer to explicitly declare all your Maven configuration. In this tutorial, we’ll demonstrate how to create an executable jar/war using Maven without a parent pom.
2. Spring Boot Without Parent POM
We use Apache Maven to build and manage our project dependencies. This time we don’t inherit from the spring-boot-starter-parent
but include a dependencyManagement
BOM. When using this BOM, we need to include the repackage
goal to create an executable jar/war file.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dailycodebuffer.example</groupId>
<artifactId>Spring-Boot-Custom-Parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Spring-Boot-Custom-Parent</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.dailycodebuffer.example.SpringBootCustomParen.SpringBootCustomParentApplication</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
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>
Bootstrapping Spring Boot
To illustrate this example, we created a simple class that’ll print some basic output to the console.
@SpringBootApplication
public class SpringBootCustomParentApplication {
private static Logger log = LoggerFactory.getLogger(SpringBootCustomParentApplication.class);
public static void main(String[] args) {
SpringApplication.run(SpringBootCustomParentApplication.class, args);
}
@PostConstruct
private void init(){
log.info("creating an executable jar/war with spring boot without parent pom");
}
}
Output
The previous application generates the following output.
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.0.RELEASE)
2019-11-02 00:59:11.383 INFO 7552 --- [ main] .d.e.S.SpringBootCustomParentApplication : Starting SpringBootCustomParentApplication on Shabbir with PID 7552 (E:\DailyCodeBuffer\Codes\Spring-MVC-Tutorials\Spring-Boot-Custom-Parent\target\classes started by shabb in E:\DailyCodeBuffer\Codes\Spring-MVC-Tutorials\Spring-Boot-Custom-Parent)
2019-11-02 00:59:11.431 INFO 7552 --- [ main] .d.e.S.SpringBootCustomParentApplication : No active profile set, falling back to default profiles: default
2019-11-02 00:59:15.580 INFO 7552 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-11-02 00:59:15.605 INFO 7552 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-11-02 00:59:15.606 INFO 7552 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.27]
2019-11-02 00:59:15.908 INFO 7552 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-11-02 00:59:15.908 INFO 7552 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 4107 ms
2019-11-02 00:59:16.019 INFO 7552 --- [ main] .d.e.S.SpringBootCustomParentApplication : creating an executable jar/war with spring boot without parent pom
2019-11-02 00:59:16.575 INFO 7552 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-11-02 00:59:17.029 INFO 7552 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-11-02 00:59:17.042 INFO 7552 --- [ main] .d.e.S.SpringBootCustomParentApplication : Started SpringBootCustomParentApplication in 6.993 seconds (JVM running for 9.981)
2019-11-02 00:59:42.356 INFO 7552 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-11-02 00:59:42.357 INFO 7552 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2019-11-02 00:59:42.382 INFO 7552 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 25 ms
3. Conclusion
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.