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.
Prerequisites
- Ensure that you have Maven installed on your machine. If not, you can download it from the official Maven website: Maven Downloads
- Make sure your Spring Boot project is set up with a
pom.xml
file and includes the necessary dependencies.
Steps
1. Open Command Prompt or Terminal
Navigate to the root directory of your Spring Boot project using your preferred command prompt or terminal.
2. Run Maven Command
Execute the following Maven command to launch your Spring Boot application:
mvn spring-boot:run
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:
- Open your terminal or command prompt.
- Navigate to the directory where your Spring Boot project is located.
- Use the following command:
nohup mvn spring-boot:run &
Explanation of the command:
nohup
: It stands for "no hang up" and is used to run a command immune to hangups, with output to a non-tty (background).&
: This symbol is used to run the command in the background.
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 &
3. (Optional) Specify a Profile
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
4. Stop the Spring Boot App
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'
Additional Notes
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.
I hope you enjoyed reading this, and I'm curious to hear if this tutorial helped you. Please let me know your thoughts below in the comments. Don't forget to subscribe to my newsletter to avoid missing my upcoming blog posts.
You can also find me here LinkedIn • Twitter • GitHub or Medium