Watch Directories, And Trigger Builds When A Change Is Detected

It’d be really useful to be able run Gradle in daemon mode, but have it ‘watch’ one or more directories. When a change occurs in one of those directories, it would then trigger a Gradle build with a set of tasks (configurable).

The use case might be: I’m developing a JavaScript browser based app using Backbone.js. I’ve structured all my modules to be in different files, and whenever I change a file in my source tree, I’d like Gradle to automatically notice the change, and trigger a build, which lints my JS, compiles LESS to CSS, uses the r.js optimizer to produce a single .js file, etc. It might also copy the resulting artifacts to a development web server I’m running (restarting if necessary).

Another use case: I’m building a Spring MVC app. I’d like Gradle to automatically build my project when I save changes, so that I can quickly determine when I introduce a breaking change. Admittedly, this use case is a little weaker (who would want to have build failures when you have a partially completed file?).

Hi Joe,

This indeed would be a useful feature, and is something that we’ve been talking about internally for some time. Unfortunately, it’s not really feasible to do this with Gradle today.

You’ll have to find an external process that watches for changes and kicks of the Gradle build. You could either kick off the Gradle build via the command line, or via the Tooling API.

Thanks for the well written suggestion! What you are describing is a highly desirable feature that has been discussed before and is definitely on our list. The fact that Gradle tasks already know, or can be made to know, all their inputs will make this an even more compelling feature.

Hi,

I wrote a “Proof-of-concept” plugin for this, (https://github.com/ylemoigne/gradle-continuous-plugin)

Anyone from gradleware can tell me if I’m going in the right direction ?

That’s an interesting implementation. I’ve added some comments on GitHub.

How do you find that it works in practice?

Hi,

Thanks for your comment. I will rework my plugin soon.

In practice, it work correctly.

The most annoying point is that task like ‘compileJava’ return list of java sources as input, not the sourceSets, so hoping to use only task input to declare watches is quite compromised.

Any news on that topic?

Have a look at https://github.com/bluepapa32/gradle-watch-plugin

Hi, Any new info regarding this?

Hi, any new info about this?

Nice, I got it working in just couple of minutes for compiling TypeScript. So far works like a charm :slight_smile: