Gretty, Spring MVC and hot deployment

928 Views Asked by At

I am learning Spring MVC and trying to use it together with Gradle and Gretty plugin. I have successfully created a "Hello World" project, however I am not able to use hot deployment with Gretty, despite setting the managedClassReload=true. I run the application by using appRun gretty task from IntelliJ. My build.gradle is as follows:

apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'war'
apply from: 'https://raw.github.com/gretty-gradle-plugin/gretty/master/pluginScripts/gretty.plugin'

group = 'lukeg'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
mainClassName = 'lukeg.LearnApplication'

repositories {
    mavenCentral()
    maven {
        url 'https://repo.spring.io/libs-snapshot'
    }
}


dependencies {
    compileOnly('org.projectlombok:lombok:+')
    compile('org.springframework:spring-webmvc:4.3.17.RELEASE')
    compile("org.aspectj:aspectjweaver:1.8.11")
    compile('org.springframework:spring-context:4.3.18.BUILD-SNAPSHOT')
    providedCompile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'
}

gretty {
    httpPort = 8080
    contextPath = '/'
    servletContainer = 'tomcat9'
    //reloadOnClassChange=true
    managedClassReload=true
    loggingLevel='DEBUG'
}

It does not matter whether I use tomcat9 or jetty9 for servlet container: the logs do not show that the changes to source files in project are detected by Gretty.

Interesingly enough, when I comment out the managedClassReload=true line and uncomment reloadOnClassChange=true the changes to source files are detected and the project is automatically reloaded.

What can be the cause for gretty's hot deployment not working? Does springloaded not work together with Spring MVC?

1

There are 1 best solutions below

0
On BEST ANSWER

First of all, there is no need to depend on the plugin script you are gathering from github since org.gretty is available in the official Gradle plugin repository for some time already:

plugins {
  id "org.gretty" version "2.1.0"
}

Since you are running your app inplace using appRun, your changes will not be reloaded.
You have to run you application as war, using appRunWar.

This is not mentioned in the documentation. But in the Gretty source code.
You can check the Gretty code which causing your issue in the BaseScannerManager:

if(wconfig.reloadOnClassChange) 
{
    if(managedClassReload) 
    {
        if(wconfig.inplace) // <-- your problem, you are running inplace
        {
            log.info 'file {} is in managed output of {}, servlet-container will not be restarted', f, wconfig.projectPath
        }
        else 
        {
            log.info 'file {} is in output of {}, but it runs as WAR, servlet-container will be restarted', f, wconfig.projectPath
            webAppConfigsToRestart.add(wconfig)
        }
    } 
    else 
    {
       log.info 'file {} is in output of {}, servlet-container will be restarted', f, wconfig.projectPath
       webAppConfigsToRestart.add(wconfig)
    }
}