欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringBoot - 打包(1) - war

程序员文章站 2022-05-24 19:58:15
...

记录下打包步骤:

1. 修改pom.xml

(1)打包方式jar修改为war

<!-- <packaging>jar</packaging> -->
<packaging>war</packaging>

(2)使用外置的tomcat相关jar

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

(3)maven插件

<finalName/>标签用于指定war包名称,默认命名规则:<groupId>-<artifactId>.war

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

(4) 跳过测试

<properties>
    ...
    <!-- maven方式跳过maven test, 等同$ mvn package -Dmaven.test.skip=true -->
    <!-- <maven.test.skip>true</maven.test.skip> -->
    <!-- surefire plugin方式跳过maven test,等同$ mvn package -DskipTests -->
    <skipTests>true</skipTests>
</properties>

 

2. 新建类ServletInitializer

该类继承SpringBootServletInitializer,与启动类放置同一级;重写configure方法,因为需要类似于web.xml配置方式来启动Spring上下文。

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        // SpringBoot启动类
        return builder.sources(Application.class);
    }
    
}

 

3. 修改启动类Application

@ServletComponentScan注解用于扫描Servlet、Filter等,否则不起作用

@ServletComponentScan("com.momo.springboot.config.servlet")
@SpringBootApplication
public class Application {

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

 

4.打包

idea点击package,在项目/target目录下可以找到打包好的war包

SpringBoot - 打包(1) - war

相关标签: SpringBoot war