Spring Boot – Create Executable using Maven without Parent Pom
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.
Create Executable JAR/WAR Without Parent Pom
We use Apache Maven to build and manage our projects 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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.memorynotfound.springboot</groupId> <artifactId>executable-without-parent</artifactId> <version>1.0.0-SNAPSHOT</version> <url>https://memorynotfound.com</url> <name>Spring Boot - ${project.artifactId}</name> <packaging>jar</packaging> <properties> <java.version>1.8</java.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>1.5.3.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <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> <mainClass>com.memorynotfound.springboot.Application</mainClass> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </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 without 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-without-parent.jar
Output
The previous application generates the following output.
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.5.3.RELEASE) 2017-05-29 09:13:57.998 INFO 959 --- [ main] c.memorynotfound.springboot.Application : Starting Application v1.0.0-SNAPSHOT 2017-05-29 09:13:58.002 DEBUG 959 --- [ main] c.memorynotfound.springboot.Application : Running with Spring Boot v1.5.3.RELEASE, Spring v4.3.8.RELEASE 2017-05-29 09:13:58.002 INFO 959 --- [ main] c.memorynotfound.springboot.Application : No active profile set, falling back to default profiles: default 2017-05-29 09:13:58.532 INFO 959 --- [ main] c.memorynotfound.springboot.Application : creating an executable jar/war with spring boot without parent pom 2017-05-29 09:13:58.707 INFO 959 --- [ main] c.memorynotfound.springboot.Application : Started Application in 11.106 seconds (JVM running for 11.479)
Downloads
From:一号门
COMMENTS