spring boot 打包

Posted 沙中世界

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring boot 打包相关的知识,希望对你有一定的参考价值。

打成jar直接运行比较简单,这里特别说明的是打成war包,部署在外部的tomcat方式:

1、修改启动代码

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan("com.xx.xx")
@Controller
// mapper 接口类扫描包配置
public class Application extends SpringBootServletInitializer {

    @RequestMapping("/")
    String home() {
        return "index";
    }

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

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

2、修改pom.xml

<packaging>war</packaging>

去除一些可能冲突的配置项:
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>com.fasterxml.jackson.core</groupId>
                    <artifactId>jackson-databind</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>log4j-over-slf4j</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
            <version>1.5.1.RELEASE</version>
        </dependency>

增加servlet(可能不是必须的)

<dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.0</version>
        </dependency>

tomcat外部依赖:

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

war包名称:

<finalName>ROOT</finalName>


防止没有web.xml报错:
<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>

 

以上是关于spring boot 打包的主要内容,如果未能解决你的问题,请参考以下文章

spring boot 打包

打包Spring Boot工程

IDEA Spring boot Maven打包发布

Spring boot项目的打包发布

Gradle 如何打包 Spring Boot 可执行 JAR

Spring Boot Maven 多模块打包