首页 > 代码库 > Spring Boot 打war包部署,打jar包

Spring Boot 打war包部署,打jar包

官方文档:http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-create-a-deployable-war-file

一:更改程序入口类 Application.java 使其继承SpringBootServletInitializer,并重写configure方法

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

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

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

}

二:更改pom.xml

<packaging>war</packaging>

三:确保内置servlet container 不会干涉发布该war包的servlet container,方案是标记内置servlet container 的依赖为provided

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

最后,将打好的war包放到tomcat下即可.

其实哪需要这么麻烦,我们创建工程的时候就可以建立war工程呀。

技术分享

打成可以运行jar包

还原上面第一步的操作,在pom.xml中添加maven-plugin

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

运行:Java -jar myapp.jar

 

Spring Boot 打war包部署,打jar包