What are the ways to run Spring Boot?

Table of contents

1. Package with commands or run in a container

2. Run with Maven plugin

3. Run with the Gradle plugin

Fourth, directly execute the main method to run


 

1. Package with commands or run in a container

There are several ways to run SpringBoot projects by packaging and putting them in containers:

  1. Packaging as a Jar file:
    Use Maven or Gradle and other construction tools to execute the packaging command to package the SpringBoot project into an executable Jar file. Use the following commands on the command line:

    mvn package     // 使用Maven打包
    gradle build    // 使用Gradle打包

    After the packaging is complete, you can find the generated Jar file in the target or build directory. Then run the project with:

    java -jar your-project.jar

  2. Packaged as a War file:
    By default, the SpringBoot project will run as an embedded Servlet container, but it can also be packaged as a War file for deployment to an external Servlet container (such as Tomcat, Jetty, etc.).
    Add the following configuration to the pom.xml file:

    <packaging>war</packaging>

    Execute packaging commands through build tools such as Maven or Gradle:

    mvn package     // 使用Maven打包
    gradle build    // 使用Gradle打包

    After the packaging is complete, deploy the generated War file to the webapps directory of the external Servlet container and restart the container.

  3. Use Docker container:
    The SpringBoot project can be packaged as a mirror and run in a Docker container. First, you need to write a Dockerfile to define the process of building the image. An example Dockerfile is as follows:

    FROM openjdk:8-jdk-alpine
    ADD target/your-project.jar app.jar
    ENTRYPOINT ["java", "-jar", "app.jar"]

    Execute the following command in the root directory of the SpringBoot project to build the image:

    docker build -t your-image-name .

    After the build is complete, the image can be run with the following command:

    docker run -p 8080:8080 your-image-name

     

2. Run with Maven plugin

To use the Maven plug-in to run the SpringBoot project, you need to configure the corresponding plug-in in the project's pom.xml file. Here are the steps to configure and run a SpringBoot project using the Maven plugin:

<build>Add tags inside the tags in the pom.xml file <plugins>and configure the plugin there spring-boot-maven-plugin. Examples are as follows:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

Save the pom.xml file and refresh the Maven project for the changes to take effect.

Switch to the root directory of the project on the command line and execute the following command to start the SpringBoot project:

mvn spring-boot:run

The Maven plugin will automatically compile and package the project, and start the embedded Servlet container to run the SpringBoot application.

Note, make sure that the project's dependencies and other necessary configuration information have been properly configured before running the project. Also, make sure you have properly installed and configured Maven environment.

 

3. Run with the Gradle plugin

To run a SpringBoot project with a Gradle plugin, you need to configure the corresponding plugin in the project's build.gradle file. Here are the steps to configure and run a SpringBoot project with the Gradle plugin:

Add in build.gradle file apply plugin: 'org.springframework.boot', this will apply Spring Boot plugin.

Add Spring Boot dependencies in the dependencies section. For example

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    // 其他依赖项...
}

Switch to the root directory of the project on the command line and execute the following command to start the SpringBoot project:

./gradlew bootRun

If using the Windows operating system, execute the following command:

gradlew.bat bootRun

The Gradle plugin will automatically compile and package the project, and start an embedded Servlet container to run the Spring application.

Note that before running the project, make sure the project's dependencies and other necessary configuration information are correct. Also, make sure your Gradle environment is properly installed and configured.

 

Fourth, directly execute the main method to run

To run a Spring Boot project by executing the main method directly, you can follow these steps:

Make sure you have properly configured your Spring Boot project and that there is a class with a main method in the entry class of the project.

Open the entry class and find the class containing the main method. In general, entry classes are marked with annotations @SpringBootApplication.

In the class containing the main method, find the definition of the main method. Its signature should resemble the following code:

public static void main(String[] args) {
    SpringApplication.run(YourSpringBootApplication.class, args);
}

where YourSpringBootApplication.classis the entry class for your Spring Boot application.

Make sure your project has been built and an executable JAR file has been generated.

Open a terminal or command line interface and navigate to the directory where the JAR file is located.

Enter the following command at the command line to run the Spring Boot project:

java -jar your-project.jar

where your-project.jaris the JAR file name of your project.

Hit enter, and the Spring Boot application will be up and running.

Through the above steps, you can run the Spring Boot project by directly executing the main method. Remember to make sure that the project has been properly configured and an executable JAR file has been generated before running.

Guess you like

Origin blog.csdn.net/2301_77899321/article/details/132195621