SpringBoot2同时支持打war包和jar包

场景

  一般我们使用SpringBoot开发都是打包为jar,但有时候总是需要打成war包,甚至两种都要同时支持,也就有了本篇文章。

实现思路
	使用 SpringBoot 的 profiles 配置实现。
实操
  1. 修改pom.xml
<groupId>xxx</groupId>
<artifactId>xxx</artifactId>
<version>xxx</version>
<packaging>${project.packaging}</packaging>

<profiles>
    <profile>
      <!--  mvn clean package -Dmaven.test.skip=true -Pjar  -->
      <id>jar</id>
      <!-- 激活这个 profile 为默认 profile,即默认 clean package 打jar包    -->
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
        <project.packaging>jar</project.packaging>
      </properties>
      <build>
        <finalName>ms-auth</finalName>
        <plugins>
          <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.5.6</version>
            <executions>
              <execution>
                <goals>
                  <goal>repackage</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>

    <profile>
      <!--  mvn clean package -Dmaven.test.skip=true -Pwar  -->
      <id>war</id>
      <properties>
        <project.packaging>war</project.packaging>
      </properties>
      <dependencies>
        <!-- war 包要排除内嵌 tomcat,并加上 javax.servlet 包,如有 javax.websocket 还需加上 javax.websocket -->
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
          <exclusions>
            <exclusion>
              <artifactId>spring-boot-starter-tomcat</artifactId>
              <groupId>org.springframework.boot</groupId>
            </exclusion>
          </exclusions>
        </dependency>
        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>3.1.0</version>
          <scope>provided</scope>
        </dependency>
        <dependency>
          <groupId>javax.websocket</groupId>
          <artifactId>javax.websocket-api</artifactId>
          <version>1.1</version>
          <scope>provided</scope>
        </dependency>
      </dependencies>
      <build>
        <finalName>ms-auth</finalName>
        <plugins>
          <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.5.6</version>
            <executions>
              <execution>
                <goals>
                  <goal>repackage</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
  1. 修改启动类
// SpringBoot打war包,需要让主程序类继承 SpringBootServletInitializer,并重写configure方法
public class App extends SpringBootServletInitializer {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(App.class);
    }
}
  1. 修改 Filter 实现类
// Filter 实现类要实现 init 和 destroy 方法
public class XXXFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        
    }
    @Override
    public void init(FilterConfig filterConfig) {

    }
    @Override
    public void destroy() {

    }
}

至此,SpringBoot 即可同时支持 war 包和 jar 包。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值