Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install/run a watch task in Gradle

I'd like to run a given task, every time a file in the folder src changes.

It seems that Gradle does not have a task like that, but there is the gradle-watch-plugin on github. Following the installation guide, I tried:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.bluepapa32:gradle-watch-plugin:0.1.5'
    }
}   

apply plugin: 'com.bluepapa32.watch'

task "sometask" << {
  println "My Own task."
}


watch {
    somename {
        files files('src')
        tasks 'sometask'
    }
}

Unfortunately this results in an error:

Starting:watch FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':watch'.
> org.gradle.tooling.BuildLauncher.withArguments([Ljava/lang/String;)Lorg/gradle/tooling/BuildLauncher;

So what's wrong with my build.gradle?

like image 271
Edward Avatar asked May 31 '16 12:05

Edward


People also ask

How do I run a Gradle program?

You can run the application by executing the run task (type: JavaExec). This will compile the main source set, and launch a new JVM with its classes (along with all runtime dependencies) as the classpath and using the specified main class.

How do I run a script in Gradle?

Running Gradle Commands To run a Gradle command, open a command window on the project folder and enter the Gradle command. Gradle commands look like this: On Windows: gradlew <task1> <task2> … ​ e.g. gradlew clean allTests.

How do I view tasks in Gradle?

Within IntelliJ IDEA, find the check task in the Gradle tool window under verification. Double click it to run.


1 Answers

This can be done without a plugin by enabling continuous mode in your build via the --continuous or -t command line argument. For example, given the following build script running gradle -t myTask will automatically watch for changes in the folder src and reexecute the task when those files change.

task myTask {
  inputs.files 'src'
  doLast {
    // do some stuff with files in 'src' folder
  }
}
like image 171
Mark Vieira Avatar answered Oct 18 '22 14:10

Mark Vieira