/ Backend Development

How to automatically restart a Spring Boot project in IntelliJ IDEA (using Spring DevTool)

Blog Image
Creator Image
Bashar Alshaibani
13 Jun 2024 -
5 min Reading time

To automatically restart a Spring Boot project in IntelliJ IDEA and check static and template changes, you can use Spring Boot DevTools. This tool provides a convenient way to automatically restart the application when files change. Here's how you can set it up:

Step-by-Step Guide:

  1. Add Spring Boot DevTools to your project:

    Make sure spring-boot-devtools is included in your pom.xml (for Maven) or build.gradle (for Gradle).

    For Maven:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>
    

    For Gradle:

    implementation 'org.springframework.boot:spring-boot-devtools'
    
  2. Enable Automatic Build in IntelliJ IDEA:

    • Open IntelliJ IDEA.
    • Go to File > Settings (or IntelliJ IDEA > Preferences on macOS).
    • Navigate to Build, Execution, Deployment > Compiler.
    • Check the box Build project automatically.

    To enable this for ongoing changes:

    • Press Ctrl+Shift+A (or Cmd+Shift+A on macOS) to open the "Actions" popup.
    • Type registry and select Registry....
    • In the registry, find and check compiler.automake.allow.when.app.running.
    • If the above option is not available, go to File > Settings > Advanced Settings and check Allow auto-make to start even if developed application is currently running
  3. Use Spring Boot DevTools:

    When you have spring-boot-devtools in your classpath, your application will automatically restart whenever files on the classpath change. This includes changes in .java, .properties, and .yml files.

  4. Optional: Configure DevTools for Static Resources:

    By default, Spring Boot DevTools will detect changes to static files (e.g., HTML, CSS, JavaScript) and automatically refresh the browser. If it’s not working as expected, you may need to adjust your settings:

    • Ensure that static files are placed in the correct directories, such as src/main/resources/static or src/main/resources/templates.
    • DevTools will monitor the /META-INF/maven and /resources directories. If your resources are elsewhere, you may need to adjust the spring.devtools.restart.additional-paths property.
  5. Running the Application:

    • Run your Spring Boot application normally from IntelliJ IDEA.
    • Make changes to your static resources or templates.
    • The application should automatically restart, and you should see the changes immediately.

Tips:

  • Check your console: When you make changes, check the IntelliJ IDEA console to ensure that the application is restarting as expected.
  • Browser Cache: Sometimes the browser cache can prevent you from seeing the latest changes. Make sure to clear the cache or use the browser’s "Hard Reload" feature (usually Ctrl+Shift+R or Cmd+Shift+R).

By following these steps, your Spring Boot application will automatically restart in IntelliJ IDEA when you make changes to static files and templates, allowing you to see your changes immediately.