Creating Spring boot executable jar
To create a completely self-contained executable jar(fat jars) file that we could run in production, Just spring-boot-maven-plugin to our pom.xml as shown in below pom.xml
Configuring build plugin in pom.xml
<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.candidjava.spring.boot</groupId> <artifactId>helloworld</artifactId> <version>0.0.1-SNAPSHOT</version> <name>helloworldjar</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <!-- Package as an executable jar --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Spring boot Application (Example.java)
package com.candidjava.spring.boot.helloworld; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Example { public static void main(String[] args) throws Exception { SpringApplication.run(Example.class, args); } }
Sample rest controller (UserController.java)
package com.candidjava.spring.boot.helloworld; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { @GetMapping("/user") String home() { return "Helloworld"; } }
Creating Spring boot executable jar
To create a completely self-contained executable jar(fat jars) file that we could run in production, Just spring-boot-maven-plugin to our pom.xml as shown in below pom.xml
How to build a spring boot jar using maven
Save your pom.xml and run mvn package from the command line or using eclipse maven goal.
Now look in the target directory, you should see helloworldjar.jar, you can copy the jar to any location and run it.
Running spring boot jar
To run that application, use the java -jar command, as follows:
..target >java -jar helloworldjar.jar
Download
Download source code from my github account Click here