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 -version
in 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.xml
file resides). -
Run the Application: Execute the following command:
mvn spring-boot:run
This command uses the
spring-boot-maven-plugin
to 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 package
This creates a JAR file in the
target
directory. -
Run the JAR File:
java -jar target/your-application-name.jar
Replace
your-application-name.jar
with 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.gradle
file resides). -
Run the Application: Execute the following command:
./gradlew bootRun
This command uses the
spring-boot-gradle-plugin
to build and run your application. Gradle will handle dependency resolution, compilation, and execution. If you are on Windows, usegradlew bootRun
instead 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 build
This creates a JAR file in the
build/libs
directory. -
Run the JAR File:
java -jar build/libs/your-application-name.jar
Replace
your-application-name.jar
with 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.active
argument: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.xml
file 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.