50

I've been doing a P.O.C with Spring Boot.

So far it's been going really good and promising, but there's one major drawback: I'm using an embedded server (i.e., packaging the web app in a .jar), so when developing I have to rebuild the jar and restart the server every time I change the CSS, HTML or JS files. There's not hot-swap. This really slows down the UI development.

I can think of several quick fixes, such as loading static resources off a different domain and serving it from a local nginx, and some more variations like this, but isn't there a built-in option of some sort when working with IntelliJ/Eclipse?

1
  • If you don’t mind switching from spring boot, you can try mason github.com/metamug/mason It’s built to be fast and redeploy on change.
    – Sorter
    Commented Oct 18, 2019 at 15:44

12 Answers 12

28

There are several options. Running in an IDE (especially with debugging on) is a good way to do development (all modern IDEs allow reloading of static resources and usually also hotswapping of Java class changes). Spring Boot devtools is a cheap way to get quite a big boost (just add it to your classpath). It works by restarting your application in a hot JVM when changes are detected. It also switches off things like thymeleaf caches while it is running, so you don't have to remember to do that yourself. You can use it with an external css/js compiler process if you are writing that code with higher level tools.

Spring Loaded is no longer recommended, but probably still in use. More sophisticated agent-based tools work much better if you need hot swapping with zero delay (e.g. JRebel).

See the docs for some up to date content

5
  • Thanks dave i'll take a look into spring-loaded and the maven option for reloading static files, one question: i'm running idea12 and i can't find a way for repackaging a static resource when running an embedded tomcat in an annotation driven configuration, are you sure it should work?
    – Amnon
    Commented Jan 28, 2014 at 9:28
  • I'm not a regular IDEA user but I don't know of any problems. I worked on a project last year where everyone used it and never had any issues. It must be possible. Maybe you have to add src/main/resources explicitly to your classpath?
    – Dave Syer
    Commented Jan 28, 2014 at 9:57
  • I'm also having a problem getting this to work within IntelliJ. I followed the spring boot reference and made sure my cache setting was set to false for Thymeleaf, but whenever I change a template located under src/main/resources, I have to restart the application running the embedded container Commented May 18, 2014 at 2:59
  • 3
    I didn't realize I could get STS to hot-reload .class files just by launching in debug mode. Thanks!
    – duma
    Commented Aug 1, 2014 at 2:30
  • 1
    @RoyKachouh Dave is correct. See my answer for instructions (it's really simple!) Commented Sep 14, 2014 at 22:31
15

but isn't there a built-in option of some sort when working with IntelliJ/Eclipse?

What helped me in IntelliJ 15.0, windows 10, was the following sequence:

STEP 1: Added the following dependency in pom (This is mentioned everywhere but this alone dint solve it), as mentioned by @jonashackt

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

STEP 2: Then from File->Settings-> Build-Execution-Deployment -> Compiler (make sure main compiler option is selected and not any of its sub-options)

enable Make Project Automatically. Click ok and close the dialog Note : In latest version it will be Build project automatically

STEP 3: Hold Shift+Ctrl+A (on windows) you will see a search dialog with title "Enter Action or option name", type registry. Double click the first option that says "Registry..." it will open another window. Look for the following option:

compiler.automake.allow.when.app.running

and enable it, click close

STEP 4: Restart IDE

Elaborated from this source

5
  • 2
    This just restarts the SpringBoot on save file or?
    – powder366
    Commented Nov 2, 2017 at 13:43
  • no it doesn't, it lets you reflect the changes on saving.
    – shabby
    Commented Nov 3, 2017 at 9:58
  • 4
    Yes but when I save it restarts SpringBoot from start. I just wanted to class that changed to be loaded.
    – powder366
    Commented Nov 3, 2017 at 10:12
  • Thymeleaf templates & static resources can be reloaded without building, if you source them from the filesystem rather than the classpath: stackoverflow.com/a/72928208/768795
    – Thomas W
    Commented Jul 10, 2022 at 11:55
  • not having compiler.automake.allow.when.app.running with build: Build #IU-231.9161.38, built on June 20, 2023
    – Tony B
    Commented Jun 29, 2023 at 11:53
7

You can get hot swapping:

  • for java code: using spring-loaded
  • for Thymeleaf templates: disabling the cache

Check this post to see more details: http://blog.netgloo.com/2014/05/21/hot-swapping-in-spring-boot-with-eclipse-sts/

1
  • Thymeleaf templates & static resources can also be reloaded without building, if you source them from the filesystem rather than the classpath: stackoverflow.com/a/72928208/768795
    – Thomas W
    Commented Jul 10, 2022 at 11:55
6

I do not know how far this kind of support goes, but in case you use Eclipse IDE (or anyone reading this): starting up your Spring-Boot application via m2e in debug-mode (press the "Debug"-dropdown button and pick your maven run configuration item).

It works for me like a charm.

My maven run configuration item is configured as follows:

  • goal is set to "spring-boot:run"
  • base directory is the project directory

I am not using any further libraries (not even spring-boot-devtools).

That's it.

1
5

You can also use JRebel - it will reload all changes (better hotswap) including spring beans. It is easily integratred with both Intellij and Eclipse.

0
5

Assuming you are using gradle; use the following config in your build.gradle

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'application'

applicationDefaultJvmArgs = ["-agentlib:jdwp=transport=dt_socket,address=localhost:7000,server=y,suspend=n"]

mainClassName = "package.ApplicationRunner"

Run the application from the IDE or command line using the command gradle build run

Now the IDE can connect to the remote JVM (on port 7000) where the spring boot application runs. It also supports hot deployment of static files.

or even you can run the main class from intelliJ if the dependencies are properly managed in the IDE. The main class is the class that contains the main method which will call SpringApplication.run("classpath:/applicationContext.xml", args);

5

In Intellij, I can get this behavior. When the program is running in debug mode, select Run > Reload Changed Classes

Note: After Intellij completes the action, it might say Loaded classes are up to date. Nothing to reload. This is misleading, because it actually DID reload your classpath resources.

My environment/setup includes:
Intellij 13
Embedded Tomcat
Run/Debug configuration of type 'Application' (which just uses a main class)
Serving static html, css and js (no jsp)

5

Try using this spring-boot-devtools tag in pom.xml

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

http://mytechnologythought.blogspot.com/2017/07/how-to-run-server-of-spring-boot-auto.html

2
  • Why optional=true?
    – nabster
    Commented Apr 29, 2021 at 18:29
  • @nabster Optional dependencies are used when it's not possible (for whatever reason) to split a project into sub-modules. The idea is that some of the dependencies are only used for certain features in the project and will not be needed if that feature isn't used. Ideally, such a feature would be split into a sub-module that depends on the core functionality project.... Read more: maven.apache.org/guides/introduction/…
    – horvoje
    Commented Jun 21, 2021 at 16:49
3

From 1.3.0. (now in Milestone 2) on, you can use the spring-boot-devtools for that as a lightweigt approach - see the docs or this blogpost. Simply upgrade to >= 1.3.0. and add the following to your pom.xml:

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

Than start your SpringBootApplication with Run As... and you´re fine.

2

I recommend Thymeleaf (template engine), jRebel for personal developer. Thymeleaf template files are just HTML resources. So, they`re changed immediately after you edit template files.

0

If you're using maven, the spring-boot-maven-plugin in your pom.xml needs to be like this to get the hot swap:

  <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>springloaded</artifactId>
                <version>1.2.0.RELEASE</version>
            </dependency>
        </dependencies>
    </plugin>

and if you're using thymeleaf, add this to your application properties:

spring.thymeleaf.cache=false

But remember something: Don't use this in your production environment..

0

How to perform Hot Swap in Springboot Application

  1. When using gradle include following in the dependency: compile group: 'org.springframework.boot', name: 'spring-boot-devtools', version: '2.0.1.RELEASE' & providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')

  2. In application.properties add the property spring.devtools.restart.additional-paths=.

  3. Build Gradle and then run application as bootRun

The application is ready to perform hot swap on modification of classes

Not the answer you're looking for? Browse other questions tagged or ask your own question.