2019-03-30

将Spring Boot项目打包成war包,部署到Tomcat

修改打包格式 <packaging>war</packaging> 修改依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <!--打包的时候可以不用包进去,别的设施会提供。事实上该依赖理论上可以参与编译,测试,运行等周期。
      相当于compile,但是打包阶段做了exclude操作-->
    <scope>provided</scope>
</dependency>

为了防止应用上下文所导致的项目访问资源加载不到添加<build>

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

新建一个 SpringBootApplication.java,继承SpringBootServletInitializer实现configure:

public class SpringBootApplication extends SpringBootServletInitializer {

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

}