Friday 13 September 2019

How to create an executable jar from spring boot project?


Spring boot maven plugin is used to generate an UBER jar (by combining all the dependent jars)

Step 1: Add spring-boot-maven-plugin to pom.xml.

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Step 2: Add below statement to pom.xml. This statement tells that I would like to export this application as jar file.
<packaging>jar</packaging>

If you want to export the application as war file mention below statement.
<packaging>war</packaging>

Step 3: Go to the directory where pom.xml is located and execute below commmand to generate UBER jar file.
mvn package

Find the below working application.


HomeController.java
package com.sample.app.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HomeController {

    @RequestMapping("/")
    public String homePage() {
        return "Welcome to Spring boot Application Development";
    }

}


App.java
package com.sample.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

}


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>springbootMisc</groupId>
    <artifactId>springbootMisc</artifactId>
    <version>1</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
    </parent>

    <packaging>jar</packaging>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>


Total project structure looks like below.

Generate UBER jar file

Right click on project.

Run As -> Run Configurations…


Mention ‘package’ in goals section and click on Run button.

It generates a jar file in project target directory.
$ls *jar

springbootMisc-1.jar

Run the jar file by executing the command ‘java -jar {jarFile}’
$java -jar springbootMisc-1.jar
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.6.RELEASE)

2019-07-19 09:03:53.610  INFO 40838 --- [           main] com.sample.app.App                       : Starting App v1 on C02X902SJGH5 with PID 40838 (/Users/harikrishnagurram/Documents/EclipseWorkSpaces/Learnings/springbootMisc/target/springbootMisc-1.jar started by harikrishnagurram in /Users/harikrishnagurram/Documents/EclipseWorkSpaces/Learnings/springbootMisc/target)
2019-07-19 09:03:53.613  INFO 40838 --- [           main] com.sample.app.App                       : No active profile set, falling back to default profiles: default
2019-07-19 09:03:54.543  INFO 40838 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-07-19 09:03:54.581  INFO 40838 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-07-19 09:03:54.581  INFO 40838 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.21]
2019-07-19 09:03:54.695  INFO 40838 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-07-19 09:03:54.695  INFO 40838 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1042 ms
2019-07-19 09:03:54.914  INFO 40838 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-07-19 09:03:55.117  INFO 40838 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2019-07-19 09:03:55.121  INFO 40838 --- [           main] com.sample.app.App                       : Started App in 1.925 seconds (JVM running for 2.346)

Open the url ‘http://localhost:8080/’ in browser, you can see below screen.

You can download complete working application from this link.


Previous                                                    Next                                                    Home

No comments:

Post a Comment