Springboot项目打包war配置详解

Springboot项目打包war配置详解

  • 1. 排除内置tomcat依赖
    • 2. 添加servlet依赖
    • 3. 修改打包方式
    • 4. 修改主启动类
    • 5. 完整pom.xml
    • 6. 效果图

1. 排除内置tomcat依赖

<dependency>
     <groupId>org.springframework.bootgroupId>
     <artifactId>spring-boot-starter-webartifactId>
     <exclusions>
         <exclusion>
             <groupId>org.springframework.bootgroupId>
             <artifactId>spring-boot-starter-tomcatartifactId>
         exclusion>
     exclusions>
 dependency>

2. 添加servlet依赖

<dependency>
    <groupId>jakarta.servletgroupId>
    <artifactId>jakarta.servlet-apiartifactId>
dependency>

3. 修改打包方式

<packaging>warpackaging>

4. 修改主启动类

@SpringBootApplication
public class Springboot3Application extends SpringBootServletInitializer {

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

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

5. 完整pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>3.1.2version>
        <relativePath/> 
    parent>
    <groupId>com.zlmgroupId>
    <artifactId>springboot3artifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>springboot3name>
    <description>springboot3description>
    <packaging>warpackaging>
    <properties>
        <java.version>17java.version>
    properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-starter-tomcatartifactId>
                exclusion>
            exclusions>
        dependency>

        <dependency>
            <groupId>jakarta.servletgroupId>
            <artifactId>jakarta.servlet-apiartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
            <scope>runtimescope>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-configuration-processorartifactId>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <configuration>
                    
                    <skip>trueskip>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombokgroupId>
                            <artifactId>lombokartifactId>
                        exclude>
                    excludes>
                configuration>
            plugin>
        plugins>
    build>

project>

6. 效果图

Springboot项目打包war配置详解_第1张图片

你可能感兴趣的:(spring,boot,后端,java)