You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于Gradle中Spring Boot Devtools的runtimeClasspath配置疑问

Why does Spring Boot DevTools Gradle config need runtimeClasspath extendsFrom developmentOnly?

Great question—let’s unpack this to understand what that runtimeClasspath configuration is doing, and why it matters even if your build and executable jar seem to work without it.

First, let’s clarify what runtimeClasspath is: yes, it’s a core configuration from the Gradle Java plugin. It defines the set of dependencies that Gradle uses when running your application (like with the bootRun task in Spring Boot), and it’s separate from the configurations used for compiling or packaging production artifacts.

What does runtimeClasspath { extendsFrom developmentOnly } do?

The developmentOnly configuration is a custom one you’ve declared to hold dependencies that should only be used during development (like DevTools). By default, dependencies in developmentOnly are not included in the runtimeClasspath.

Adding that line ensures that when you run your app locally during development (e.g., via ./gradlew bootRun), the DevTools jar is included in the runtime classpath. This is critical because DevTools relies on being present at runtime to enable features like:

  • Automatic application restarts when code changes
  • Live reload for static resources
  • Enhanced developer-focused error pages
  • Disabling template caching (so you see template changes immediately)

Why did your build and executable jar still work without it?

You noticed your build succeeded and the executable jar runs fine—here’s why:

  1. Packaging behavior: Spring Boot’s Gradle plugin automatically excludes dependencies from the developmentOnly configuration when building the executable production jar. So even if you didn’t link it to runtimeClasspath, DevTools still won’t end up in your production artifact.
  2. Build vs. runtime: The build process itself doesn’t require DevTools to be in the runtime classpath—it only matters when you’re actually running the app in a development context.

What happens if you omit that configuration?

While your build and production jar are unaffected, you’ll lose all DevTools features when running the app locally with bootRun. For example:

  • You’ll have to manually restart the app every time you make code changes
  • Template changes won’t reflect without a restart
  • You won’t get the developer-friendly error pages

Recap of the two configurations

Your simplified config works for building, but misses development runtime support:

configurations {
    developmentOnly
}
dependencies {
    developmentOnly("org.springframework.boot:spring-boot-devtools")
}

The full config ensures DevTools works as intended during local development, while still keeping it out of production:

configurations {
    developmentOnly
    runtimeClasspath {
        extendsFrom developmentOnly
    }
}
dependencies {
    developmentOnly("org.springframework.boot:spring-boot-devtools")
}

内容的提问来源于stack exchange,提问作者leeyuiwah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:50:11