8

SpringBoot打可执行war包

 1 year ago
source link: https://eelve.com/archives/springbootwar
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

前情提要】最近做了一个Springboot项目,但是最后需要打成WAR包在容器中部署,下面就简单记录一下。


壹、修改pom文件

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">  <modelversion>4.0.0</modelversion>  <groupid>com.eelve</groupid>  <artifactid>springboot-war</artifactid>  <version>0.0.1-SNAPSHOT</version>   <!-- 打包方式 -->  <packaging>war</packaging>     <parent>        <groupid>org.springframework.boot</groupid>        <artifactid>spring-boot-starter-parent</artifactid>        <version>2.2.6.RELEASE</version>        <relativepath> <!-- lookup parent from repository -->    </relativepath></parent>     <dependencies>        <dependency>            <groupid>org.springframework.boot</groupid>            <artifactid>spring-boot-starter-web</artifactid>        </dependency>         <!-- 嵌入式tomcat相关jar将被放入到WEB-INF\lib-provided下 -->        <dependency>            <groupid>org.springframework.boot</groupid>            <artifactid>spring-boot-starter-tomcat</artifactid>            <scope>provided</scope>        </dependency>    </dependencies>     <build>        <plugins>            <!-- 打包插件 -->            <plugin>                <groupid>org.springframework.boot</groupid>                <artifactid>spring-boot-maven-plugin</artifactid>            </plugin>        </plugins>    </build></project>

贰、修改启动类

package com.eelve.springboot.war; import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.builder.SpringApplicationBuilder;import org.springframework.boot.web.support.SpringBootServletInitializer; @SpringBootApplicationpublic class SpringbootWarApplication extends SpringBootServletInitializer {     @Override    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {        return application.sources(SpringbootWarApplication.class);    }     public static void main(String[] args) {        SpringApplication.run(SpringbootWarApplication.class, args);    }}

后面的话】使用maven打包(clean package),生成的war包可以用于传统的部署方式(外部tomcat),也可以直接使用java -jar 的方式运行。


薏米笔记

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK