title: How to execute a gradle external task (Exec) in background?
body: <p>I'm using gradle and gulp to build and launch my JEE web application that is based on JDK6+, and the web container is Jetty.</p>
  
  <p>Gulp is used to process static resource, such as concat and minify javascript/css files. Also, there is a <code>watch</code> task in my gulp script, it is used to watch static file changes and rebuild them automatically.</p>
  
  <p>So to launch the application in Jetty, i need to do following things:</p>
  
  <ol>
  <li>Build static resource using gulp <code>[gulp task: build]</code>.</li>
  <li>Watch static file changes <code>[gulp task: watch]</code>, i don't call this gulp task directly, it is call by a gradle task via <em>Exec</em> class <code>[gradle task: watch]</code>.</li>
  <li>Build my java code and launch Jetty <code>[gradle task: jettyRun]</code>.</li>
  </ol>
  
  <p>Since i added the gradle task <code>watch</code> as a dependency of <code>jettyRun</code> task, so i though i only need to call <code>gradle jettyRun</code> from command line, my application will be launched. But the result is not same as i expected.</p>
  
  <hr>
  
  <p>Following is my script files:</p>
  
  <p><strong><em>build.gradle</em></strong></p>
  
  <pre><code>apply plugin: 'war'
  apply plugin: 'jetty'
  
  repositories {
      jcenter()
  }
  
  // omit the dependencies here
  
  task watch(type: Exec){
      workingDir "${projectDir}"
      // Pass build type to npm and gulp.
      commandLine "gulp", "watch"
  }
  jettyRun.dependsOn watch
  </code></pre>
  
  <p><strong><em>gulpfile.js</em></strong></p>
  
  <pre><code>var gulp = require('gulp');
  gulp.task('build', function(callback) {
      // removed the code to make this question as simple as possible.
      if (callback != null) callback();
  });
  
  gulp.task('watch', function(callback) {
      gulp.watch(['./src/static/**/*.js'], ['build']);
      if (callback != null) callback();
  });
  </code></pre>
  
  <hr>
  
  <p><strong>The result:</strong></p>
  
  <p><img src="https://i.stack.imgur.com/jxiL0.png" alt="The execute result"></p>
  
  <p><strong>Question:</strong></p>
  
  <p>Now, the process hungs when excutes the <code>gradle watch</code> task, there is no change to execute <code>jettyRun</code> task for gradle. I know this hang is caused by the watch process launched by gulp, because it is watching file changes. But i hope that gradle just only launch the gulp watch process and return immediately to execute the next <code>jettyRun</code> task! </p>
  
  <p>How to do this? Also, i want to see the output of watch task from stdout. </p>
  
  <p>I know there is a ProcessBuilder in java, i have tried it, but it doesn't work. Maybe I have done something wrong. :(</p>
  
  <p><a href="https://www.dropbox.com/s/km5b4ln9xhtd1g0/gradle-gulp.zip?dl=0" title="All files">Download all files</a></p>