To create a Spring Boot project from the command line, you can use Spring Initializr or Maven. Below are the steps for both methods.
1. Using Spring Initializr (Web-based)
Spring Initializr is the easiest way to create a Spring Boot project using a web interface, but it can also be done from the command line.
Command Line (with curl): You can generate a Spring Boot project directly from the Spring Initializr by running this curl command:
curl https://start.spring.io/starter.zip \
-d dependencies=web \
-d language=java \
-d type=maven-project \
-d groupId=com.example \
-d artifactId=my-springboot-app \
-d name=my-springboot-app \
-d packageName=com.example.myspringbootapp \
-d javaVersion=17 \
-o my-springboot-app.zip
This command will download a .zip file with your Spring Boot project. Replace the dependencies with the libraries you want to include (e.g., web, jpa, mysql, etc.), and adjust the other options (like groupId, artifactId, etc.) to fit your project needs.
Once the ZIP file is downloaded, unzip it:
unzip my-springboot-app.zip -d my-springboot-app
cd my-springboot-app
Now, you can build and run your Spring Boot project.
Running the Project:
To build the project with Maven and run it:
./mvnw spring-boot:run
If you’re using Gradle:
./gradlew bootRun
2. Using Spring Boot CLI (Command Line Interface)
Spring Boot CLI provides a command-line interface to create and manage Spring Boot projects.
Install Spring Boot CLI:
To install Spring Boot CLI, you can use SDKMAN (a tool for managing parallel versions of multiple SDKs), or download it directly from the Spring website.
For SDKMAN installation:
sdk install springboot
Create a Spring Boot Application:
After installation, you can create a new Spring Boot project using the following command:
spring init --dependencies=web --groupId=com.example --artifactId=my-springboot-app --name=my-springboot-app
This will generate a Spring Boot project with the web dependency (Spring Web) and the specified groupId, artifactId, and name.
3. Using Maven Command (Manually Create Project Structure)
You can manually create a Spring Boot project using Maven by running the following command:
mvn archetype:generate -DgroupId=com.example -DartifactId=my-springboot-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
This creates a basic Java project structure. After that, you will need to configure it as a Spring Boot project by:
- Adding the Spring Boot dependencies to your pom.xml.
- Creating a main class with the @SpringBootApplication annotation.
- Configuring any additional Spring Boot dependencies like spring-boot-starter-web.
Here’s an example of the dependencies you might add in pom.xml for a Spring Boot application:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
4. Spring Boot Application Example
Regardless of how you create the project, here’s an example of a minimal Spring Boot main class to start your application:
package com.example.myspringbootapp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MySpringbootAppApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringbootAppApplication.class, args);
}
}
Final Steps to Run the Project:
- Build and run the project:
- If using Maven, use: ./mvnw spring-boot:run
- If using Gradle, use: ./gradlew bootRun
- Open your browser and visit http://localhost:8080 to see your Spring Boot application running.
Recap of Commands:
- Spring Initializr (via curl):
curl https://start.spring.io/starter.zip -d dependencies=web -d groupId=com.example -d artifactId=my-springboot-app -o my-springboot-app.zip
- Spring Boot CLI:
spring init --dependencies=web --groupId=com.example --artifactId=my-springboot-app --name=my-springboot-app
- Maven Create Project:
mvn archetype:generate -DgroupId=com.example -DartifactId=my-springboot-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Let me know if you need more assistance!