Java|SpringBoot 项目开发时,让 FreeMarker 文件编辑后自动更新

正在维护的一个 SpringBoot 项目是前后端一体的,页面使用 FreeMarker 编写。在开发过程中,ftl 文件编辑后,每次都需要重启应用才能看到效果,效率非常低下。这里记录通过哪些配置后,可以让它们免重启自动更新。

  1. 在应用的 pom.xml 文件里,做如下修改:

    <dependencies>
        <!-- 添加以下依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
    
    <build>
        <finalName>${artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!-- 添加以下这一行 -->
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
    </build>
  2. 在 application-dev.properties 文件里添加如下内容:

    # freemarker hot reload
    spring.freemarker.cache=false
    spring.freemarker.settings.template_update_delay=0

    禁用 FreeMarker 缓存,有更改后即时更新。

  3. 修改 IDEA 配置,开启自动编译:

    5191689302a73ec4ed20efadde14db69.png

  4. 编译应用运行时的 Run/Debug Configurations:

    e0fe5bd5d132ff72fc2ca390418ae798.png

    将 On 'Update' action: Update classes and resources 和 On frame deactivation: Update classes and resources 配置打开。

关于 spring-boot-devtools 的相关用途与说明,可以参考 Spring 官方文档:https://docs.spring.io/spring-boot/docs/2.7.18/reference/html/using.html#using.devtools,可以看到,如果想要在开发过程中修改 Java 代码后免于手动重启,也可以借助于 spring-boot-devtools 的相关配置。

参考链接:

  • https://docs.spring.io/spring-boot/docs/2.7.18/reference/html/using.html#using.devtools

  • https://blog.csdn.net/silentwolfyh/article/details/85048745

  • https://www.cnblogs.com/ios9/p/14410299.html


如果读完文章有收获,可以关注我的微信公众号「闷骚的程序员」并🌟设为星标🌟,随时阅读更多内容。

帮忙点个「分享」或者「在看」吧!

⬇️⬇️⬇️

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值