Updated April 10, 2023
Definition of Spring Boot Run Command
Spring boot run command is defined as a methodology to run spring boot applications through the use of a command-line interface. Generally, during the development of the spring boot application, we try to run the application using the integrated development environment or in other words IDE, but as we try to launch the spring boot application in the production environment, it becomes inevitable to use the command-line interface to run the spring boot application as the production environment might not always support an IDE. Spring boot as an application development tool does accelerate the development process by providing a lot of starter packs and initializers. Bundling up the different elements and running them in the environment is what we will talk about in detail in this article.
Syntax of Spring Boot Run Command
The spring boot run command, as mentioned, is a methodology for running spring boot applications in mainly production environments. There are many ways in which this capability can be achieved but adhering to the length of the article we will talk about the most frequent ways in the professional world.
Generate a Maven artifact from a Spring Boot application:
mvnclean install
This method needs to run the FAT JAR which gets build post the run of the install command.
Running of jar file created (Read this as FAT JAR):
java -jar <jar file name>
Running the Spring Boot without building the FAT JAR using Maven installed:
mvnspring-boot:run
,/pre> Run maven if when maven is not installed in the system:
mvnw install
mvnw and mvnw.cmd are the 2 wrappers which we get when we download our Spring Boot project from Spring Initializer i.e. https://spring.io. This method needs to run the FAT JAR which gets build post the run of the install command.
Running the Spring boot application from the source directory without building FAT JAR:
mvnwspring-boot:run
Make sure that the spring-boot plugin is a part of the pom.xml while making list of dependencies in the pom.xml. Here there is no build of FAT JAR and hence no requirement of running java -jar command and even without that the spring boot application would launch.
How does Run Command Work in Spring Boot?
- In this article we will focus our efforts on understanding 2 aspects, one of them is to run the Spring Boot application while we have maven installed in our system, and the other by using a workaround of running the Spring Boot application without maven installed in our system. In addition, there are 2 different varieties that are present in each of the cases mentioned above while running the Spring Boot application. In one we can build a FAT JAR and then run the FAT JAR while the other is to bypass the build of FAT JAR and run the Spring Boot application without it.
- Let us first start with the concept of running the spring boot run command with the maven installed in the system. For that first, we will have to install Apache Maven in the system. One can download the zip file of the apache maven, extract that zip and add the bin folder with the mvn command to the PATH in the environment variables. Once maven is in place, if the developer runs mvn install, the Spring Boot project is compiled, tested, and then packaged, and finally build the .jar file into the local maven repository. While building the jar file it takes care of the dependency management where 3rd party dependencies are included in the project. The JAR file is built with convention as maven expects a certain directory structure for it to be compiled and an mvn clean install does the job of compilation and packaging work. It also runs some code quality checks along with the execution of test cases. Not only that but the application can also be deployed to remote servers through plugins. Once that JAR file is built, we can run the java -jar command to run the application in the environment where the application will be deployed.
- The alternate to run the spring boot without building the JAR file is to run the command mvnspring-boot:run. This command makes sure that the POM.xml has the plugin which signifies that we want to use Tomcat to run our code. When the code is run in the project root folder the plugin reads the POM.xml and understands that a web application container is needed and thus triggers the download of Apache tomcat. Once all things are downloaded, the spring boot server runs automatically and launches the application as a spring boot application.
- The ./mvnw command is no different from maven clean install and just allows the developer to not have a dependency of maven installed into the system. This wrapper file is obtained while we download the spring boot project from the spring initializer. This works very similar to fully installed maven except for the fact that maven is not actually installed. Apart from that the other commands like a clean install, and spring-boot: run works the same way as it does for cases of mvn.
Examples of Spring Boot Run Command
For the examples, we will use the same spring boot codes and demonstrate both cases!
Pom.xml: (Make sure below dependency is present):
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
startWeb.java
package com.example.demo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class startWeb {
@RequestMapping("/")
public String home() {
return "Hello Readers";
}
}
RuncommanddemoApplication.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RuncommanddemoApplication {
public static void main(String[] args) {
SpringApplication.run(RuncommanddemoApplication.class, args);
}
}
Example #1
Running with mvn and FAT JAR:
Syntax:
mvn clean install
cd target
java -jar <jar file name>
Output:
Example #2
Running with mvnwspring-boot:run:
Syntax:
mvnwspring-boot:run
Output:
Conclusion
In conclusion, in this article we have learned about the working of spring boot run command along with the flexibility spring boot provides to the developers with various alternatives to choose from and act as per convenience. The rest 2 cases of “mvn and spring-boot: run” and “mvnw and FAT JAR” are left to the readers for hands-on experience!
Recommended Articles
This is a guide to Spring Boot Run Command. Here we also discuss the introduction and how does run command work in spring boot? along with different examples and its code implementation. You may also have a look at the following articles to learn more –