laitimes

Packaging and running SpringBoot programs: Build an efficient deployment process

1 Introduction

In modern application development, an efficient packaging and deployment process is critical to the development, testing, and go-live of a project. As a rapid development framework, Spring Boot provides convenient packaging tools and embedded web servers, making packaging and running easier.

This article will look at how to package Spring Boot applications to help developers build an efficient deployment process.

2. Package the Spring Boot application

Spring Boot provides a variety of ways to package applications, such as Maven plugins and Gradle plugins. Let's take Maven as an example:

Maven plugins

In your project's pom .xml file, add the Maven plugin configuration:

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

Package by executing the following command:

mvn clean package
           

After the packaging is complete, an executable JAR file will be generated in the target directory.

Packaging and running SpringBoot programs: Build an efficient deployment process

3. Run the Spring Boot application

Spring Boot applications can be run in a variety of ways, including using an embedded web server, deploying as a WAR file to an external servlet container, and more.

Use an embedded web server

Run the app with the following command:

java -jar your-application.jar
//或
java –jar your-application.jar --server.port=80
//或
java –jar springboot.jar –-server.port=80 --logging.level.root=debug
           
Packaging and running SpringBoot programs: Build an efficient deployment process

This will start the embedded Tomcat server and run the Spring Boot application.

Packaging and running SpringBoot programs: Build an efficient deployment process

External servlet containers

If you want to deploy your app to an external servlet container, you'll need to package your app as a WAR file. Add the following configuration in pom.xml:

<packaging>jar</packaging>
//修改为
<packaging>war</packaging>
           

Introduce dependencies

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <!-- 去除内嵌tomcat -->
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <!--添加servlet的依赖-->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>

</dependencies>
           

Add a startup class

If the war package is published, you need to add the SpringBootServletInitializer subclass and rewrite its configure method, or extend the class where the main function is located to SpringBootServletInitializer and rewrite the configure method

At that time, when the project was uploaded to the tomcat server when it was packaged as war, it always reported 404 error, which was that this step was ignored!!

@SpringBootApplication
public class JavaToJarAdmin  extends SpringBootServletInitializer {
    public static void main(String[] args) {
        SpringApplication.run(JavaToJarAdmin.class, args);
    }
 
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(JavaToJarAdmin.class);
    }
 
}
           

Then execute the Maven command to pack:

mvn clean package
           

After the packaging is complete, an executable WAR file is generated in the target directory.

Packaging and running SpringBoot programs: Build an efficient deployment process

Run the war file using an external Tomcat (drop the war file directly into the tomcat webapps directory)

Packaging and running SpringBoot programs: Build an efficient deployment process

Start Tomcat

Packaging and running SpringBoot programs: Build an efficient deployment process

Access http://localhost:8080/war package name/

http://localhost:8080/JavaToJar-1.0-SNAPSHOT/
Packaging and running SpringBoot programs: Build an efficient deployment process

Caution:

If you plan your project as a war package and deploy it to an external tomcat, you can't directly access the configuration file in the Spring Boot project.

The server.port configured in application.yml is configured with the port number of Spring Boot's built-in tomcat, and after being deployed as a war package on an independent tomcat, the configured server.port does not work. Be sure to pay attention to this.

4 Epilogue

Through the introduction of this article, you will learn how to use the Maven plugin to package Spring Boot applications, flexible packaging and deployment. Combining an embedded web server with an external servlet container will help build a robust deployment process and improve development and operational efficiency.

Source: blog.csdn.net/m0_53127626/article/details/135696635

Read on