Spring Boot simplifies the development of standalone, production-ready Spring-based applications. While most developers use Integrated Development Environments (IDEs) for development and testing, it’s often necessary to run Spring Boot applications directly from the command line for deployment, scripting, or continuous integration. This tutorial will guide you through the process, covering the most common scenarios and build tools.
Prerequisites
Before you begin, ensure you have the following:
- Java Development Kit (JDK): Java 8 or later is required. Verify your installation by running
java -versionin your terminal. - Build Tool: Spring Boot projects typically use either Maven or Gradle for dependency management and building. Choose one based on your project’s existing configuration.
1. Using Maven
If your Spring Boot project uses Maven, follow these steps:
-
Navigate to the Project Directory: Open your terminal and change the directory to the root of your Spring Boot project (where the
pom.xmlfile resides). -
Run the Application: Execute the following command:
mvn spring-boot:runThis command uses the
spring-boot-maven-pluginto build and run your application. Maven will handle dependency resolution, compilation, and execution. -
Packaging and Executing a JAR File (Alternative): You can also package your application into an executable JAR file and run it directly.
-
Package the Application:
mvn packageThis creates a JAR file in the
targetdirectory. -
Run the JAR File:
java -jar target/your-application-name.jarReplace
your-application-name.jarwith the actual name of your JAR file.
-
2. Using Gradle
If your Spring Boot project uses Gradle, follow these steps:
-
Navigate to the Project Directory: Open your terminal and change the directory to the root of your Spring Boot project (where the
build.gradlefile resides). -
Run the Application: Execute the following command:
./gradlew bootRunThis command uses the
spring-boot-gradle-pluginto build and run your application. Gradle will handle dependency resolution, compilation, and execution. If you are on Windows, usegradlew bootRuninstead of./gradlew bootRun. -
Packaging and Executing a JAR File (Alternative): You can also package your application into an executable JAR file and run it directly.
-
Package the Application:
./gradlew buildThis creates a JAR file in the
build/libsdirectory. -
Run the JAR File:
java -jar build/libs/your-application-name.jarReplace
your-application-name.jarwith the actual name of your JAR file.
-
Important Considerations
-
Executable JARs: Executable JARs include all the necessary dependencies within the JAR file, making them self-contained and easy to deploy.
-
Profiles: Spring Boot supports profiles, which allow you to configure different application behaviors for various environments (e.g., development, testing, production). You can activate a profile when running from the command line using the
--spring.profiles.activeargument:java -jar your-application.jar --spring.profiles.active=production -
Logging: Spring Boot’s default logging configuration uses Logback. You can customize logging behavior by configuring the
logback.xmlfile or using command-line arguments to control logging levels.
By following these steps, you can easily run your Spring Boot applications from the command line, enabling greater flexibility in deployment, scripting, and automation.