关于Gradle中Spring Boot Devtools的runtimeClasspath配置疑问
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:
- Packaging behavior: Spring Boot’s Gradle plugin automatically excludes dependencies from the
developmentOnlyconfiguration when building the executable production jar. So even if you didn’t link it toruntimeClasspath, DevTools still won’t end up in your production artifact. - 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

