如何优雅的打包 Spring Boot 应用

阅读前请先思考下如果让你将 Spring Boot 应用打成一个压缩包、解压直接运行如何实现?
下面看老司机是如何做的。

0x 下载 Spring Boot 程序示例

这里写图片描述

  • 解压下载的文件,删除无用的文件只保留src目录pom.xml.gitignore

这里写图片描述

  • packaging-demo目录按住shift在空白区域点击鼠标右键选择在此处打开命令窗口
  • 在打开的明了窗口执行mvn clean compile提前下载依赖的文件,加速IDEA导入速度

1x 导入IDEA

这里写图片描述

2x 项目中添加配置文件

  • src/main/resources目录添加以下配置文件

    1. application.properties (修改默认端口号、添加其他配置)
    2. messages_en.properties (一些额外的配置信息)
  • 首先在src/main/resources建立env目录,然后在env目录下建立dev、test、pre、prod目录,分别对应开发、测试、预发布、生产环境,最后在每个目录下建立env-config.properties文件用于配置不同环境相关信息

这里写图片描述

  • 配置文件内容

    1. application.properties
# Server Port Configuration
server.port=8888
#Management Configuration
management.server.port=9001
management.server.servlet.context-path=/management
# Logging Configuration
logging.level.org.springframework.web=ERROR
#Access log configuration
server.tomcat.accessLogEnabled=true
server.tomcat.basedir=../logs
server.tomcat.accessLogPattern=%t %s %a %D "%r" %b
  1. messages_en.properties
#Spring boot greeting message
message.greetings=Greetings from Spring Boot !!! TankBing ...
  1. env-config.properties 注意每个环境下都要配置
com.tankbing.env=开发 dev
com.tankbing.name=会编程的坦克兵
com.tankbing.introduce=这里只有干货,带你体验不一样的程序人生

3x 编码

  1. 加载配置文件 MessageConfigProperties.java
@Component
@ConfigurationProperties(prefix = "message")
@PropertySource(value = {"classpath:messages_en.properties"})
public class MessageConfigProperties {
    private String greetings;

    public String getGreetings() {
        return greetings;
    }

    public void setGreetings(String greetings) {
        this.greetings = greetings;
    }
}
  1. HelloController.java 注入 MessageConfigProperties 输出欢迎信息到前端
@RestController
public class HelloController {
    private static final Logger LOGGER = LoggerFactory.getLogger(HelloController.class);
    @Autowired
    private MessageConfigProperties messageConfigProperties;

    @GetMapping("/")
    public String index() {
        String greetingsMessage = messageConfigProperties.getGreetings();
        LOGGER.info("Inside index() method, returning :" + greetingsMessage);
        return greetingsMessage;
    }
}

4x 创建启动脚本

src/main/resources 目录下创建 bin 目录,编写启动脚本

  1. start.bat
set ENV=%1
IF [%1]==[] (
set ENV=dev
)
echo %ENV%
java -Xms256m -Xmx512m -server -Dspring.config.location=../config/application.properties,../config/messages_en.properties,../config/env/%ENV%/env-config.properties -jar ../lib/packaging-demo-0.0.1-SNAPSHOT.jar
  1. start.sh
ENV=$1
if [ $ENV == '' ]
then
$ENV = "dev";
fi
java -Xms256m -Xmx512m -server -Dspring.config.location=../config/application.properties,../config/messages_en.properties,../config/env/%ENV%/env-config.properties -jar ../lib/packaging-demo-0.0.1-SNAPSHOT.jar

5x 使用 Maven-Assembly-Plugin 进行打包

  1. src/main/resources 目录新建 distribution 目录,添加 distribution.xml 文件
<assembly>
    <id>assembly</id>
    <formats>
        <format>tar</format>
        <format>zip</format>
    </formats>
    <fileSets>
        <fileSet>
            <directory>src/main/resources/bin</directory>
            <outputDirectory>bin</outputDirectory>
            <includes>
                <include>start.sh</include>
                <include>start.bat</include>
            </includes>
            <fileMode>0755</fileMode>
        </fileSet>
        <fileSet>
            <directory>src/main/resources</directory>
            <outputDirectory>config</outputDirectory>
            <includes>
                <include>*.properties</include>
                <include>*.xml</include>
                <include>env/dev/*</include>
                <include>env/qa/*</include>
                <include>env/uat/*</include>
                <include>env/prod/*</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>target</directory>
            <outputDirectory>lib</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>src/main/resources</directory>
            <outputDirectory>logs</outputDirectory>
            <fileMode>0755</fileMode>
            <excludes>
                <exclude>**/*</exclude>
            </excludes>
        </fileSet>
    </fileSets>
</assembly>
  1. pom.xml 添加 assembly 打包插件
<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <descriptors>
            <descriptor>src/main/resources/distribution/distribution.xml</descriptor>
        </descriptors>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

6x 构建项目

mvn clean package -Dmaven.test.skip=true

这里写图片描述

7x 运行程序

  1. 拷贝压缩文件ZIP/TAR到需要部署的机器上
  2. 解压文件 tar –zxvf packaging-demo-assembly.tar
  3. 进入 bin 目录执行 start.sh dev/test/pre/prod
8x 测试是否符合预期

打开浏览器输入 http://localhost:8888 访问
这里写图片描述

github

源码地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值