Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot + Spring-Loaded (IntelliJ, Gradle)

I'd like to use hot swap with my Spring Boot project. Somehow I am not able to make it working by running it in my IDE (IntelliJ), despite of having this topic covered by documentation. I simply run the class with my main method with VM attributes:

-javaagent:/path/to/jar/springloaded.jar -noverify 

My question is, how do I make it work? :-)

Further question is how to use spring loaded with Gradle and IntelliJ. I find it quite inconvenient to force the developer to download the JAR manually, place it somewhere and point to it with a JVM parameter. Is there any better way (should I configure my own task which does the job and run it from my IDE as a Gradle task)?

like image 301
kuceram Avatar asked Jun 23 '14 16:06

kuceram


People also ask

How do I run gradle Spring Boot in IntelliJ?

In the Project tool window, double click the build. gradle file to open it in the editor. in the editor to load the changes to your project. In the Gradle tool window, open the project's node, then the Tasks node and double-click the build task to run it.

How do I add a Spring Boot to an existing gradle project?

Adding Spring Boot Support Into Our Gradle Project Add the Spring Boot Gradle plugin (version 1.2. 5. RELEASE) to the classpath of the build script. Apply the Spring Boot Gradle plugin.

Can gradle be used with Spring Boot?

Introduction. The Spring Boot Gradle Plugin provides Spring Boot support in Gradle. It allows you to package executable jar or war archives, run Spring Boot applications, and use the dependency management provided by spring-boot-dependencies . Spring Boot's Gradle plugin requires Gradle 6.8, 6.9, or 7.


2 Answers

You need to configure the project as stated in the documentation:

http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#howto-reload-springloaded-gradle-and-intellij-idea

After that, you must configure your IDE to output the compiled classes in build/classes/main (with Idea plugin, you can configure the outputDir as specified in the above link, and then invoke gradle idea to have it done).

Then, if you launch the task (run / bootRun) or run the main class from the IDE's using the debug mode, hot code reloading should work when a class is compiled.

The gotcha here is that IntelliJ, unlike Eclipse, doesn't automatically compile a class when it is saved (even if you configure the compiler to "Build on save", it won't do it when is Running/Debugging). This is apparently a design decission made by IntelliJ - as stated here Intellij IDEA Java classes not auto compiling on save (CrazyCoder answer) .

It would be ideal if spring boot provided a configuration option to monitor your source code files and recompile them when they change - that is what Grails does. But I think such a think does not exist yet, and maybe is not even possible to combine that with gradle, which is the responsible of managing the classpath and that kind of things.

So there are two options as far as I can tell:

  • You remember to compile everything you edit (adding an easier Compile shortcut as suggested in the previous StackOverflow link might help).
  • You put some filesystem monitor (inotify-tools for Linux, launchd for Mac OS X are examples) that invokes gradle compileJava/compileGroovy when a change is detected in any source code file.

First is tedious, second is slow :) . Actually there's another option: you change your IDE :-D (or install the EclipseMode IntelliJ plugin).

like image 147
Deigote Avatar answered Sep 28 '22 04:09

Deigote


If one wants to be able to run the application solely from IntelliJ (using Right Click -> Debug on the main method) and not involve Spring Boot's Gradle tasks at all, you simply need to do the following:

  1. Configure the run configuration in IntelliJ to use the SpringLoaded agent. This is easy to do and an example is shown in the following screenshot:

enter image description here

Notice how I have added a VM Option: -javaagent:/path/to/springloaded-${version}.jar -noverify (which you can download here)

  1. Debug using Right Click -> Debug like the following screenshot:

enter image description here

  1. Everytime you make a change and want to reload it, just compile the project. The default shortcut is Cntrl+F9, but you can also access it from the menu Build -> Make Project
like image 36
geoand Avatar answered Sep 28 '22 02:09

geoand