BootRun Hot Deploy with Spring Loaded in IntelliJ

Setting up Spring Loaded is something a lot of developers probably do in 10 minutes. On the other hand you can be like me, in which case it can take considerably longer for one reason or another. Hopefully this post describing how I setup Spring Loaded in IntelliJ will help you get closer to that 10 minute mark.

Just so you know, this post will probably be outdated soon when we all start using the Automatic Restart feature in Spring Boot 1.3 DevTools. I don't know though, cause Spring Loaded is pretty awesome.

What Spring Loaded does is make it possible for you to edit your Java class files while your app is running, rebuild the project in IntelliJ (or Eclipse), refresh your browser, and BAM! App updated!

You actually don't have to use Spring Loaded to get the same effect. In IntelliJ, for instance, you can setup a Tomcat server for Hot Swapping by deploying an Exploded War Artifact and set it to update classes and resources on frame deactivation (see this StackOverflow answer.) We want to keep things Bootiful here so we're going to launch our app with a Gradle BootRun task and use Spring Loaded.

Our demonstration is a simple “hello!” model attribute displayed temporarily on the NixMash Spring Home Page. YOWSA!

Here's our Controller method.

With Spring Loaded we're going to launch the app with Gradle BootRun, display it in Chrome, make a change to the Controller Method, Make the Project in IntelliJ, then refresh the browser where we'll instantly see our changes. Before going any further, you can do this in either Run or Debug mode. And don't get all caught up in IntelliJ Plugins like Live Edit or DCEVM. Apart from the Make Project step in IntelliJ shown below, Spring Loaded does it all for you.

You can see that Make Project is a pretty speedy process. The 3 seconds shown below is actually a long-running update!

Once we see the compilation message in the event log we can refresh the browser and see the change we made in the Controller code.

Configuring Spring Loaded

If you recall the layout of NixMash Spring, we've got two modules. JPA handles most of the data and services work while MVC covers the web side. We can configure Spring Loaded in the root build.gradle file or in the MVC module build.gradle. We'll look at both.

Root build.gradle

If you configure Spring Loaded in the root you'd follow the Spring Hot Swapping Documentation and it should work for you. Here are the highlights of that. Note that this configuration does not work in the MVC module build.gradle.

buildscript {
    repositories { jcenter() }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.3.RELEASE")
        classpath("org.springframework:springloaded:1.2.4.RELEASE")
    }
}
apply plugin: 'idea'

idea {
	module {
	    inheritOutputDirs = false
	    outputDir = file("$buildDir/classes/main/")
	}
}

Web Module build.gradle

If you configure Spring Loaded in the module, you could do it like this and not touch the root build.gradle at all. Here's the source for this nifty Gradle work from Max Bruchmann.

configurations { springLoaded }
dependencies {
    springLoaded("org.springframework:springloaded:1.2.4.RELEASE")
}

bootRun {

    def springLoadedFile = configurations.springLoaded.find
            { it.name == "springloaded-1.2.4.RELEASE.jar" }

    systemProperty 'server.port', '8084'
    systemProperty 'spring.profiles.active', 'mysql'
    jvmArgs "-javaagent:$springLoadedFile", "-noverify"
    main = 'com.nixmash.springdata.mvc.WebInitializer'
}

Oh right, the updated page with the code changes. Don't want to disappoint! YOWSA TWO TIMES!

One final reminder when using DevTools with IntelliJ is that, unlike Eclipse, IntelliJ doesn't automatically restart your app. You need to a BUILD -> MAKE PROJECT for a quick restart. That's stated here in the Spring Developer's Tools Guide.

All of the code discussed in this post is available in the v0.2.1 branch of NixMash Spring on GitHub.

Posted September 17, 2015 03:45 PM EDT

More Like This Post