Spring Boot既打jar包又打war包如何做

你好,我是柳岸花开。

引言

在软件开发中,根据不同的部署需求,我们可能需要将应用打包成不同的格式。Spring Boot作为目前流行的Java应用开发框架,提供了一种简单的方式来打包应用。本文将介绍如何利用Maven Profiles在Spring Boot项目中灵活地打包成jar包或war包。

Maven Profiles简介

Maven Profiles是Maven提供的一种功能,允许用户根据不同的需求定义不同的构建配置。通过Profiles,我们可以轻松切换构建配置,而无需修改项目本身的配置。

Spring Boot与Maven结合

Spring Boot通过SpringBootServletInitializer类支持传统的Servlet容器部署。结合Maven Profiles,我们可以定义两种不同的打包方式:jar和war。

示例项目配置

以下是示例项目的Maven配置,展示了如何根据不同的Profiles来打包Spring Boot应用。

项目主类
public class PlatformLocalApplication extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(PlatformLocalApplication.class);
    }
}

当打包成war包时,此主类是必需的,以便应用可以在传统的Servlet容器中运行。

Maven配置
<packaging>${project.package}</packaging>

这里使用了Maven的属性替换功能,根据激活的Profile动态设置打包类型。

Jar Profile配置
<profile>
            <!--  mvn clean install -Dmaven.test.skip=true -Pjar  -->
            <id>jar</id>
            <!-- 激活这个profile为默认profile ,即默认clean install打jar包    -->
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <project.package>jar</project.package>
            </properties>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-web</artifactId>
                </dependency>
            </dependencies>
            <build>
                <resources>
                    <resource>
                        <directory>src/main/resources</directory>
                        <filtering>true</filtering>
                    </resource>
                </resources>
                <plugins>
                    <plugin>
                        <groupId>com.github.shalousun</groupId>
                        <artifactId>smart-doc-maven-plugin</artifactId>
                    </plugin>
                    <!-- compile plugin -->
                    <plugin>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <configuration>
                            <source>1.8</source>
                            <target>1.8</target>
                            <encoding>UTF-8</encoding>
                        </configuration>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <configuration>
                            <skip>true</skip>
                        </configuration>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-war-plugin</artifactId>
                        <version>2.6</version>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-jar-plugin</artifactId>
                        <configuration>
                            <excludes>
                                <exclude>application.yml</exclude>
                            </excludes>
                        </configuration>
                    </plugin>
                    <plugin>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-maven-plugin</artifactId>
                        <version>${springboot.version}</version>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>repackage</goal>
                                </goals>
                                <configuration>
                                    <classifier>exec</classifier>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <!-- 过滤后缀不需要转码的文件后缀名 pdf -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-resources-plugin</artifactId>
                        <configuration>
                            <encoding>UTF-8</encoding>
                            <useDefaultDelimiters>false</useDefaultDelimiters>
                            <nonFilteredFileExtensions>
                                <nonFilteredFileExtension>pdf</nonFilteredFileExtension>
                            </nonFilteredFileExtensions>
                        </configuration>
                    </plugin>
                </plugins>
                <finalName>platform-local</finalName>
            </build>
        </profile>

这个Profile是默认激活的,用于打包成jar格式。

War Profile配置
<!--  SpringBoot打war包,需要让主程序类 继承 SpringBootServletInitializer,并重写configure方法    -->
        <profile>
            <!--  mvn clean install -Dmaven.test.skip=true -Pwar  -->
            <id>war</id>
            <properties>
                <project.package>war</project.package>
            </properties>
            <dependencies>
                <!--  打war包要排除内嵌tomcat;并加上javax.servlet包   -->
                <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>
                <resources>
                    <resource>
                        <directory>src/main/resources</directory>
                        <filtering>true</filtering>
                    </resource>
                </resources>
                <finalName>platform-local</finalName>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-war-plugin</artifactId>
                        <version>3.2.0</version>
                    </plugin>
                </plugins>
            </build>
        </profile>

这个Profile用于打包成war格式,需要通过命令行参数-Pwar来激活。

打包命令
  • 打包成jar包:
    mvn clean install -Dmaven.test.skip=true -Pjar
  • 打包成war包:
    mvn clean install -Dmaven.test.skip=true -Pwar
结语

通过Maven Profiles,我们可以非常灵活地控制Spring Boot项目的打包过程。无论是需要快速部署的jar包,还是需要在传统Servlet容器中运行的war包,都可以轻松实现。希望本文能够帮助开发者更好地利用Maven和Spring Boot的强大功能。

👇关注我,下期了解👇

Spring配置类源码深度解析

alt

回复 222,获取Java面试题合集

关于我

一枚爱折腾的Java程序猿,专注Spring干货。把路上的问题记录下来,帮助那些和我一样的人。

好奇心强,喜欢并深入研究古天文。

崇尚 个人系统创建,做一些时间越长越有价值的事情。思考把时间留下来。

本文由 mdnice 多平台发布

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值