Package the springboot project into a war package

Package the springboot project into a war package

The first step is to modify the packaging type and add the following dependencies

 <packaging>war</packaging>

The second step is to remove the tomcat built in spring boot and add spring-boot-starter-web

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
</dependency>

Change to the following configuration

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

The third step is to add servlet dependencies

<dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
</dependency>

The fourth step is to modify the package name after packaging

 <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <finalName>项目包名</finalName>
    </build>

The fifth step, modify the startup class

@SpringBootApplication(exclude= {
    
    DataSourceAutoConfiguration.class})
public class TestApplication extends SpringBootServletInitializer {
    
    


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

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
    
    
        return builder.sources(TestApplication.class);
    }
}

There is another pitfall I encountered during the packaging process. If Lombok and log4j are used in the springboot project at the same time, jar package conflicts will occur after packaging and running, so don’t use the log.info function of log4j in the end. Lombok also has its own annotations log log printing function, import lombok.extern.slf4j.Slf4j;! ! ! !

The last step is to execute the maven command, first execute:

mvn clean

then execute

mvn package

The following content appears to indicate that the packaging is successful:
insert image description here
then put the war package under the tomcat webapp, and then start tomcat to access it! ! !

If you have any questions, you can leave a message in the comment area! ! !

Guess you like

Origin blog.csdn.net/qq_44874270/article/details/119026003