Run Spring Boot Application Command Line Maven





Hey guys in this article, you will learn how to run Spring Boot application from command line using Maven.

Read More:

Overview


When we create a new spring boot project, spring initializer adds maven plugin to the pom.xml

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

We can use this plugin to package and generate the executable jar. It also generates maven wrapper files mvnw.cmd and mvnw.sh

Create spring boot project


There are many different ways to create a spring boot application, you can follow the below articles to create one –

>> Create spring boot application using Spring initializer
>> Create spring boot application in Spring tool suite [STS]
>> Create spring boot application in IntelliJ IDEA

Add maven Dependencies


Open pom.xml and add the following dependencies –

<?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 https://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.5.5</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>in.bushansirgur</groupId>
	<artifactId>springbootapp</artifactId>
	<version>1.0.0</version>
	<name>springbootapp</name>
	<description>Spring boot app</description>
	<properties>
		<java.version>1.8</java.version>
	</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>

Create a Rest Controller


Create HomeController.java inside in.bushansirgur.springbootapp.controller package and add the following content

package in.bushansirgur.springbootapp.controller;

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

@RestController
public class HomeController {
	
	@GetMapping("/home")
	public String home() {
		return "This is home page";
	}
}

Run the app from Command line


Go to the root of the project from command line, enter the following command to package the app

4

This will generate the Jar or war file inside the target folder depending upon the selection you made while creating the project.

mvnw is a maven wrapper files, spring initializer created these files to run the maven project. We don’t need to install Maven separately on our system.

To run the spring boot project from command line using maven, enter the following command from the root of the folder.

3

This will start the tomcat server on the default port 8080. Open the browser and navigate to the url http://localhost:8080/home

That’s it for this post, if you like this post, share this with your friends and colleagues or you can share this within your social media platform. Thanks, I will see you in our next post.



Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

Leave a Reply