Alphalearningschool
        

<<< Prev

Next >>>

Up
Spring Boot using command Prompt

In order this run this Spring Boot Application, we need to convert this application to a JAR file by running some basic commands in command prompt.

Steps to create a JAR File:

As we have created a maven project, so will be only discussing about maven commands.

  1. To install this application, we use the command ‘mvn clean install’.    Syntax:-  ProjectName>mvn clean install
  2. After executing the command, we get ‘BUILD SUCCESSFUL’ which means java project is successfully converted to a jar file.

Running a java file using Command Prompt:

 

Tomcat Deployment:

There are few steps we need to follow to deploy a Sprint Boot Application.

  • To deploy a spring Boot application first we need to convert it to a WAR file.
  • To convert it to a WAR file we have to extend our @SpringBootApplication class to SpringBootServletInitializer class and override configure method. SpringBootServletInitializer helps us to configure our SpringBoot Application when it is launched.

 

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer{
	

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
		return super.configure(builder);
	}

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

}

 

  • Then, we have to mention main Class name in pom.xml file i.e.
<start-class>com.example.demo.DemoApplication</start-class>

 

After adding <start-class> pom.xml looks like this:

 

<?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>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.1.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
		<start-class>com.example.demo.DemoApplication</start-class>
	</properties>

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

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

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

</project>

 

  • Then we have to update JAR into WAR by adding a packaging tag in pom.xml i.e.
<packaging>war</packaging>

 

After adding <packaging> pom.xml looks like this:

 

<?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>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.1.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo</name>
	<description>Demo project for Spring Boot</description>
	<packaging>war</packaging>

	<properties>
		<java.version>1.8</java.version>
		<start-class>com.example.demo.DemoApplication</start-class>
	</properties>

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

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

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

</project>

 

  • Now, we have to create WAR file to deploy it to TOMCAT server. This can be done by using ‘package’ command in command prompt.

          Syntax: ProjectName>mvn package

 

  • After creating WAR file we can deploy Spring Boot Application to TOMCAT Server by sampling adding WAR file under the WEBAPPS directory.
  • After Successful deployment, hit http://localhost:8080/demo URL using any web browser.