Are you ready to run your Spring Boot application and see it in action?
The process is straightforward, thanks to Maven's integration with Spring Boot. In this quick guide, we'll walk you through the steps to launch your Spring Boot app using Maven.
Navigate to the root directory of your Spring Boot project using your preferred command prompt or terminal.
Execute the following Maven command to launch your Spring Boot application:
This command triggers Maven to compile and package your Spring Boot app, and then starts it. Maven will automatically handle dependency downloads, code compilation, and run the embedded Tomcat server.
To run this command in the background you can use the nohup command:
nohup mvn spring-boot:run &
Explanation of the command:
After running this command, your Spring Boot application will start in the background, and the output will be redirected to a file named nohup.out in the same directory.
If you want to redirect the standard output and the standard error to another file, use the command below:
nohup mvn spring-boot:run > logs.txt >2 errors.txt &
If your project uses profiles, you can specify one by using the -P option followed by the profile name. For example:
mvn spring-boot:run -Pproduction
If you want to stop the running spring boot app, you have to check first the process attached to the configured port in spring config, by executing this command:
lsof -i -P | grep 'java.*LISTEN\|LISTEN.*java'
This command uses a regular expression with .* to match any characters (including none) between java and LISTEN, or vice versa. The \| is used as the OR operator within the regular expression.
Once you have seen the process that listening to your app, kill them with this command:
kill -9 'PROCESS_PID'
Ensure that your Spring Boot application has a main method in a class annotated with @SpringBootApplication. This class should be in the root package or a subpackage of the root package.
For production deployment, consider creating an executable JAR using the following Maven command:
mvn clean package
You can then run the JAR file with:
java -jar target/your-application-name.jar
Replace your-application-name with your actual JAR file name.
And there you have it!
You've successfully launched your Spring Boot application using Maven. This process is ideal for development purposes, and for production, you may opt for creating an executable JAR or WAR file.
📨 Subscribe below to receive all the techniques, principles and tools —
No spam, No sales and Unsubscribe anytime.
13 Feb 2024
19 Jan 2024
01 Jan 2024
This short post will show you how and why you should have a no-args constructor in your JPA Entity
© Kevin KOUOMEU 2024 | All rights reserved.