Wednesday 11 July 2018

Spring boot: Create an executable jar using maven

In this post, you are going to learn.
a.   How to create a java project using maven
b.   Update pom.xml with spring boot dependencies
c.   Update pom.xml with spring-boot-maven-plugin
d.   Update App.java file
e.   Generate the jar file
f.    Run the jar file

Create a java project using maven
Maven provides number of archetypes to create project skeltons.

For example,
maven-archetype-quickstart : Generate simple maven project.
maven-archetype-j2ee-simple : Generate simple j2ee application.

Open command prompt and execute the command "mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart"

When you execute above command, maven asks groupId, artifactId, version, and package information and create a simple project.

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:3.0.1:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:3.0.1:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO]
[INFO] --- maven-archetype-plugin:3.0.1:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
Define value for property 'groupId': com.selflearningjava
Define value for property 'artifactId': HelloWorld
Define value for property 'version' 1.0-SNAPSHOT: : 1
Define value for property 'package' com.selflearningjava: : com.sample.app
Confirm properties configuration:
groupId: com.selflearningjava
artifactId: HelloWorld
version: 1
package: com.sample.app
 Y: : Y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: C:\Users\krishna\Documents\Study\Spring Boot\examples\projects
[INFO] Parameter: package, Value: com.sample.app
[INFO] Parameter: groupId, Value: com.selflearningjava
[INFO] Parameter: artifactId, Value: HelloWorld
[INFO] Parameter: packageName, Value: com.sample.app
[INFO] Parameter: version, Value: 1
[INFO] project created from Old (1.x) Archetype in dir: C:\Users\krishna\Documents\Study\Spring Boot\examples\projects\HelloWorld
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:32 min
[INFO] Finished at: 2018-04-20T15:15:53+05:30
[INFO] ------------------------------------------------------------------------
[0m[0m

It creates below project structure.


Maven creates ‘App.java’ file like below.

package com.sample.app;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
    }
}

Step 2: Update pom.xml with spring boot dependencies
Add below dependencies to pom.xml file.

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

         <dependencies>

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

         </dependencies>

Step 3: Update pom.xml with spring-boot-maven-plugin

To create an executable jar, we need to add the spring-boot-maven-plugin to our pom.xml.

<build>
         <plugins>
                  <plugin>
                           <groupId>org.springframework.boot</groupId>
                           <artifactId>spring-boot-maven-plugin</artifactId>
                  </plugin>
         </plugins>
</build>

Final pom.xml looks like below.


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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.selflearningjava</groupId>
  <artifactId>HelloWorld</artifactId>
  <packaging>jar</packaging>
  <version>1</version>
  <name>HelloWorld</name>
  <url>http://maven.apache.org</url>
  
  <parent>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-parent</artifactId>
 <version>2.0.1.RELEASE</version>
  </parent>
 
  <dependencies>
  
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
 
 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
 </dependency>

 <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>
  </plugin>
 </plugins>
</build>
</project>


Step 4: Update App.java file

App.java

package com.sample.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@EnableAutoConfiguration
public class App 
{
 @RequestMapping("/")
 @ResponseBody
 String home() {
  return "Hello World!";
 }

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

Step 5: Generate the jar file.
Go to the directory where the pom.xml file is located and run the command 'mvn package'.

[INFO]
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.sample.app.AppTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.007 s - in com.sample.app.AppTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO]
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ HelloWorld ---
[INFO] Building jar: C:\Users\krishna\Documents\Study\Spring Boot\examples\projects\HelloWorld\target\HelloWorld-1.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.0.1.RELEASE:repackage (default) @ HelloWorld ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 14.817 s
[INFO] Finished at: 2018-04-20T15:32:41+05:30
[INFO] ------------------------------------------------------------------------

‘mvn package’ command generates the jar file and place it in target folder.

Step 6: Execute the jar file.
Run the command ‘java -jar HelloWorld-1.jar’.


Once the application started, open the url ‘http://localhost:8080/’ in browser, you can able to see ‘Hello World’ message.




Previous                                                 Next                                                 Home

No comments:

Post a Comment