Javalin: Any way to convince Jetty instance to hot reload when running via gradle?

Created on 11 Sep 2017  Â·  13Comments  Â·  Source: tipsy/javalin

I am using Gradle and the kotlin and application plugins to start my Javalin app. I would like to convince Gradle and Jetty to watch and build my code, and hot-reload the Jetty server.

I was hoping that Gradle's "continuous build" feature could be used to trigger rebuilds, and that putting the Jetty server in debug mode might help, but neither of those seem to work. The continuous build seems to be blocked by Jetty taking over the thread, and only works for other commands that "end", such as my unit tests. Perhaps the debug params will only work with the Gradle jetty plugin?

Anyway, I realize this isn't really an issue with Javalin itself, but I was hoping someone in the community might have some insight on how I could improve my Javalin setup to allow for hot-reloads.

HELP WANTED

Most helpful comment

@PascalLeMerrer For static files you don't need to restart the server, you can either just build (will copy files to jar), or use enableStaticFiles("/path", Location.EXTERNAL). If you use an external location, all you have to do is save and refresh.

All 13 comments

Hi @geekytime. Unfortunately I don't have any experience with this myself, neither with maven nor with gradle, but I would love to get it working. I was under the impression that you would need something like JRebel or DCEVM though, but maybe this isn't the case? It would be great if you could look into it.

https://github.com/HotswapProjects/HotswapAgent <- might be worth looking into.

@geekytime did you have any success with this?

No. I tried for a few days and then gave up.

Kotlin and Javalin make it pleasant to _code_ a web server on the JVM. Unfortunately, between slow compile/startup times, lack of hot-reloading, and having to deal with maven or gradle; _running_ a web server on the JVM will likely always be an exercise in frustration. 😖

@geekytime are you using IntelliJ or eclipse? Rebuild+restart should take less than a second. There is also debug-mode in IntelliJ which should give some (limited) hotswapping.

No, I don't use IntelliJ or Eclipse. I should probably just give in and use an IDE the next time I work on a JVM project.

@ShikaSD @iProdigy do either of you have any experience with this? Or any interest in finding out how we can achieve automatic class reloading?

I notice in this thread you already found HotswapAgent - but I still thought I'd mention this project I came across which seems to use HotswapAgent to do hot page reloading. Perhaps it can give you a clue ...

https://github.com/kwebio/core/blob/master/src/main/kotlin/io/kweb/dev/hotswap/KwebHotswapPlugin.kt

I found these instructions for "Kweb":

Step 1 : Install Dynamic Code Evolution VM (DCEVM) Java patch
Go to https://github.com/dcevm/dcevm/releases and download the latest DCEVM light installer (DCEVM-light-8u112-installer.jar at the time of writing).

Run the installer by double-clicking on it.

Note for Mac users: You may get an error like “DCEVM-light-8u112-installer.jar” can’t be opened because it is to an unidentified developer, if so you should control-click the file and select “Open” to the drop-down menu, then click “Open”.

You must now select your Java Development Kit directory. If it isn’t already listed click Add installation directory, for example on a Mac it may be /Library/Java/JavaVirtualMachines/jdk1.7.0_121.jdk/Contents/Home.

Click Install DCEVM as altjvm.

When the installation is complete you can quit the DCEVM installer.

Step 2 : Download HotSwapAgent
Go here and download the latest hotswap-agent .jar, (hotswap-agent-1.1.0-SNAPSHOT.jar at the time of writing).

Save it somewhere appropriate, perhaps in a directory called javalibs in your home directory (it doesn’t really matter where you put it but you’ll need the full path and filename later).

Step 3 : Modify your project’s run configuration
In intelliJ select Edit Configurations... under the Run menu, and paste the following into the VM Options field:

-XXaltjvm="dcevm" -javaagent:PATH_TO/hotswap-agent.jar=disablePlugin=AnonymousClassPatch

Be sure to edit PATH_TO/hotswap-agent.jar to be the full path and name of the hotswap-agent .jar file you downloaded in Step 2.

Note that =disablePlugin=AnonymousClassPatch was necessary at the time of writing to prevent a non-fatal error, but this bug in HotSwapAgent has been reported and may be fixed by now.

Step 4 : Configure Kweb to refresh webpages automatically
Set the refreshPageOnHotswap Kweb constructor parameter to true:

    Kweb(port = 1234, refreshPageOnHotswap = true) {

Most of it looks pretty general. Might have to look at what Kweb does for refreshPageOnHotswap = true.

Edit: @gkopff hah, thanks!

The Kweb author posted on reddit: https://www.reddit.com/r/Kotlin/comments/8d6afi/kweb_rapidly_build_rich_web_apps_in_pure/ and I PM'd them a link to this issue to see if they were interested in helping out.

Kweb author here, it's been a while since I did the hotswap stuff, it was a bit fiddly but I managed to get it working eventually. @gkopff has already provided the relevant pointers, happy to answer any questions.

According to me, this would be an important feature to add to Javalin for it to be a nice backend for SPA development

@PascalLeMerrer For static files you don't need to restart the server, you can either just build (will copy files to jar), or use enableStaticFiles("/path", Location.EXTERNAL). If you use an external location, all you have to do is save and refresh.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bvicenzo picture bvicenzo  Â·  3Comments

spinscale picture spinscale  Â·  3Comments

JosefEvAlloc picture JosefEvAlloc  Â·  5Comments

vikascn picture vikascn  Â·  4Comments

mkpaz picture mkpaz  Â·  4Comments