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

Spring Boot打war包的实例教程

程序员文章站 2022-06-29 13:02:22
spring boot除了可以打可执行jar包外,也支持传统的war包。本文介绍如何使用spring boot构建传统war包。 spring boot打war包步骤如下...

spring boot除了可以打可执行jar包外,也支持传统的war包。本文介绍如何使用spring boot构建传统war包。

spring boot打war包步骤如下:

1、在pom.xml里定义打包类型

<packaging>war</packaging>

2、添加spring boot启动器(也可通过parent)

  <dependencymanagement>
 <dependencies>
  <dependency>
  <!-- import dependency management from spring boot -->
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-dependencies</artifactid>
  <version>1.5.6.release</version>
  <type>pom</type>
  <scope>import</scope>
  </dependency>
 </dependencies>
 </dependencymanagement>

3、添加spring-boot-starter-web依赖

    <dependency>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-starter-web</artifactid>
  <exclusions>
  <exclusion>
   <groupid>org.springframework.boot</groupid>
   <artifactid>spring-boot-starter-tomcat</artifactid>
  </exclusion>
  </exclusions>
 </dependency>

4、添加打包插件

  <build>
 <plugins>
  <plugin>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-maven-plugin</artifactid>
  </plugin>
 </plugins>
 </build>

5、主类继承springbootservletinitializer

/**
 * war application
 */
@springbootapplication
public class warapplication extends springbootservletinitializer {
 
 public static void main(string[] args) {
 springapplication.run(warapplication.class, args);
 }
}

6、执行mvn clean package打包

$mvn clean package

7、将打好的war包拷贝到容器(如tomcat)运行即可。

这里需要简单说明下:

主应用可以重写springbootservletinitializer里面有configure方法,自定义配置spring boot。

  /**
 * configure the application. normally all you would need to do is to add sources
 * (e.g. config classes) because other settings have sensible defaults. you might
 * choose (for instance) to add default command line arguments, or set an active
 * spring profile.
 * @param builder a builder for the application context
 * @return the application builder
 * @see springapplicationbuilder
 */
 protected springapplicationbuilder configure(springapplicationbuilder builder) {
 return builder;
 }


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。