Spring Boot – Create Executable using Maven with Parent Pom
Maven users can inherit from the spring-boot-starter-parent
project to obtain sensible defaults. You should only need to specify the Spring Boot version number on this dependency. If you import additional starters, you can safely omit the version number. This tutorial demonstrates how to create an executable jar/war using Maven with a Parent POM. In another tutorial, you can also create an executable jar/war without using a parent pom
Create Executable JAR/WAR With Parent Pom
We are using Apache Maven to create and manage our project dependencies. You can inherit from the spring-boot-starter-parent
to bootstrap spring-boot. When choosing to inherit from the parent, you can create an executable jar or war using the spring-boot-maven-plugin
and setting the executable
element of the configuration
element to true.
Note: You can create either ajar
or awar
file. You can configure this by setting thepackaging
element to the appropriate value for your needs.
You can optionally specify the main-class that bootstraps the application. When omitted, spring automatically searches for a public static void main(String[] args)
method.
<?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
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.memorynotfound.springboot</groupId>
<artifactId>executable-with-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<url>https://memorynotfound.com</url>
<name>Spring Boot - ${project.artifactId}</name>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
<mainClass>com.memorynotfound.springboot.Application</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
Bootstrapping Spring Boot
To illustrate this example, we created a simple class that’ll print some basic output to the console.
package com.memorynotfound.springboot;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import javax.annotation.PostConstruct;
@SpringBootApplication
public class Application {
private static Logger log = LoggerFactory.getLogger(Application.class);
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
@PostConstruct
private void init(){
log.info("creating an executable jar/war with spring boot with parent pom");
}
}
Creating the Executable with Maven
We create the executable running the following maven goals.
mvn clean package
Running the executable
Once the previous tasks complets, we can execute the generated jar or war using the following command.
java -jar executable-with-parent.jar
Output
The previous application generates the following output.
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.3.RELEASE)
2017-05-29 09:14:49.234 INFO 980 --- [ main] c.memorynotfound.springboot.Application : Starting Application v1.0.0-SNAPSHOT
2017-05-29 09:14:49.238 DEBUG 980 --- [ main] c.memorynotfound.springboot.Application : Running with Spring Boot v1.5.3.RELEASE, Spring v4.3.8.RELEASE
2017-05-29 09:14:49.239 INFO 980 --- [ main] c.memorynotfound.springboot.Application : No active profile set, falling back to default profiles: default
2017-05-29 09:14:49.800 INFO 980 --- [ main] c.memorynotfound.springboot.Application : creating an executable jar/war with spring boot with parent pom
2017-05-29 09:14:50.054 INFO 980 --- [ main] c.memorynotfound.springboot.Application : Started Application in 11.244 seconds (JVM running for 11.614)