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

SpringBoot结合devtools、AspectJ、Gradle与IntelliJ重启应用的问题

Solutions to Spring DevTools + IntelliJ + Gradle Integration Issues

1. Resource Files Not Deployed to Gradle's build/ Directory

To make IntelliJ use Gradle's output directories instead of its default out/:

  • Open Project Structure (Ctrl+Alt+Shift+S) → Navigate to Modules → Select your main application module
    • Go to the Paths tab: Uncheck "Inherit project compile output path", set Output path to build/classes/java/main and Test output path to build/classes/java/test
    • Switch to the Resources tab: Verify your resource directories (like src/main/resources) are mapped correctly, and their output path is set to build/resources/main
  • Ensure Delegate IDE build/run actions to Gradle is enabled in Settings > Build, Execution, Deployment > Build Tools > Gradle > Runner (confirm it's active even if you previously enabled it)

2. AspectJ Compile-Time Weaving (CTW) Not Working with IntelliJ Auto-Compile

IntelliJ's native compiler ignores Gradle's AspectJ configuration. Fix this by:

  • Enabling Delegate IDE build/run actions to Gradle (as above) — this forces IntelliJ to use Gradle's build process, which applies your AspectJ plugin and config
  • Ensure your build.gradle includes proper AspectJ setup (e.g., applying the aspectj plugin, defining aspect dependencies, configuring source sets for aspects)
  • Replace IntelliJ's "Build project automatically" with Gradle's continuous build: Run gradle build --continuous in your terminal. This triggers Gradle builds on file changes, ensuring AspectJ weaving is applied correctly, and DevTools will pick up changes from the build/ directory

3. Duplicate Restart Trigger on Long Compilations

Resolve double restarts by integrating a trigger file with Gradle:

  1. Add a Gradle task to update a trigger file after build completion:
    task triggerDevRestart {
        doLast {
            def triggerFile = file("${project.rootDir}/.restart-trigger")
            triggerFile.text = System.currentTimeMillis().toString()
        }
    }
    // Attach the task to compile and resource processing tasks
    tasks.withType(JavaCompile) {
        finalizedBy triggerDevRestart
    }
    tasks.processResources {
        finalizedBy triggerDevRestart
    }
    
  2. Configure DevTools to watch this trigger file in application.properties:
    spring.devtools.restart.trigger-file=.restart-trigger
    

This ensures DevTools only restarts once, after Gradle has fully completed building all files. Alternatively, adjust the restart delay to wait for compilation to finish:

spring.devtools.restart.poll-interval=2000
spring.devtools.restart.quiet-period=1000

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 19:25:17