1

I'm using Gretty to run my web application via gradle appRun. I'm also using the Gradle Asset Pipeline plugin to compile my Less files to CSS.

I want to integrate with Gretty's Fast reload feature so that when I change a Less file, it automatically compiles it and copies the CSS to the in-place web-app.

I have implemented a solution using Gretty's onScanFilesChanged setting in my build.gradle file:

buildscript {
    dependencies {
        classpath 'org.akhikhl.gretty:gretty:1.2.4'
        classpath 'com.bertramlabs.plugins:asset-pipeline-gradle:2.7.0'
        classpath 'com.bertramlabs.plugins:less-asset-pipeline:2.7.0'
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'war'
apply plugin: 'org.akhikhl.gretty'
apply plugin: 'com.bertramlabs.asset-pipeline'

dependencies {
    // ...
}

assets {
    excludes = ['bootstrap/**']
}

war.dependsOn assetCompile

gretty {
    servletContainer = 'tomcat8'
    enableNaming = true
    contextPath = '/'

    // This affects the war task as well
    webappCopy {
        from 'build/assets', { into 'stylesheet' }
    }

    afterEvaluate {
        prepareInplaceWebAppFolder.dependsOn assetCompile
    }

    scanDir "src/assets"
    fastReload "src/assets"
    onScanFilesChanged { List<String> files ->
        if (files.findAll { it.endsWith ".less" }.size() > 0) {
            assetCompile.compile()
        }
    } 
}

Is there a neater way to do this that doesn't involve so much code in the build.gradle file?

Nathan
  • 1,418
  • 16
  • 32

1 Answers1

0

The behavior you are describing is what Gretty is doing by default. The documentation states:

fastReload: When set to true (the default), webAppDir folder (which is typically src/main/webapp) is set as being fast-reloaded. That means: whenever some files within webAppDir are changed, these files are copied into running web-app without web-app restart.

Means, any change in a subdirectory of src/main/webapp will trigger Gretty's fast reload, but any change which is made outside of this directory triggers a server restart.

A smarter approach to your problem would be overriding the output path of gradle assetsCompile to a subdirectory of src/main/webapp, or to hook a copy task to it like this in your build.gradle file:

task copyAssets(type: Copy) {
    from buildDir + '/assets'
    into webAppDir + '/stylesheet'
}

copyAssets.shouldRunAfter assetsCompile
UnlikePluto
  • 3,101
  • 4
  • 23
  • 33