jruby-gradle.github.io/news/2015/09/01/gradle-spotlight-continuous.../index.html

210 lines
6.5 KiB
HTML

<html>
<head>
<link href='/stylesheets/foundation.css' rel='stylesheet'>
<link href='/stylesheets/jrubygradle.css' rel='stylesheet'>
<link href='/news.atom' rel='alternate' title='JRuby/Gradle News' type='application/atom+xml'>
<title>
Gradle Feature Spotlight: Continuous Build
</title>
</head>
<body>
<a href='https://github.com/jruby-gradle'>
<img alt='Fork me on GitHub' data-canonical-src='https://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png' src='https://camo.githubusercontent.com/365986a132ccd6a44c23a9169022c0b5c890c387/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f7265645f6161303030302e706e67' style='position: absolute; top: 0; right: 0; border: 0;'>
</a>
<div id='main_nav'>
<ul>
<li>
<a href='/'>
Home
</a>
</li>
<li>
<a href='/docs/'>
Documentation
</a>
</li>
<li>
<a href='/community/'>
Community
</a>
</li>
<li>
<a href='/news/'>
News
</a>
</li>
<li>
<a href='/download/'>
Download
</a>
</li>
<li>
<a href='https://travis-ci.org/jruby-gradle/jruby-gradle-plugin'>
<img src='https://travis-ci.org/jruby-gradle/jruby-gradle-plugin.svg?branch=master'>
</a>
</li>
<li>
<a href='https://bintray.com/jruby-gradle/plugins'>
<img src='https://api.bintray.com/packages/jruby-gradle/plugins/jruby-gradle-plugin/images/download.svg'>
</a>
</li>
</ul>
</div>
<div id='content'>
<h1>
Gradle Feature Spotlight: Continuous Build
</h1>
<div class="paragraph">
<p>Earlier this year the <a href="http://gradle.org">Gradle</a> project released version
<a href="https://docs.gradle.org/2.5/release-notes">2.5</a> with a heap of new features
and improvements. One of the most touted of those features is an incubating
feature (read: beta) named
<a href="https://docs.gradle.org/current/userguide/continuous_build.html">Continuous
Build</a> which automatically re-executes tasks after a file change. Rubyists may
recognize that this functionality is similar to what
<a href="http://guardgem.org/">the guard gem</a> provides.</p>
</div>
<div class="paragraph">
<p>What makes "continuous build" special is that it makes use of the <em>existing</em>
build data present in your Gradle build. Using a
<a href="https://docs.gradle.org/current/userguide/more_about_tasks.html#sec:task_inputs_outputs">task&#8217;s
inputs</a> the continuous build feature can automatically "watch" the appropriate
files to re-execute your task, or your tasks dependent tasks, automatically as
they change!</p>
</div>
<div class="paragraph">
<p>For users JRuby/Gradle this means that upgrading to Gradle 2.5 or later, and
ensuring your <code>build.gradle</code> declares task inputs and continuous build will
"just work!" Consider the following example for running
<a href="http://rspec.info">RSpec</a> tests:</p>
</div>
<div class="listingblock">
<div class="title">build.gradle</div>
<div class="content">
<pre class="CodeRay highlight nowrap"><code data-lang="gradle">buildscript {
repositories { jcenter() }
dependencies {
classpath &quot;com.github.jruby-gradle:jruby-gradle-plugin:1.0.3&quot; <i class="conum" data-value="1"></i><b>(1)</b>
}
}
apply plugin: 'com.github.jruby-gradle.base' <i class="conum" data-value="2"></i><b>(2)</b>
dependencies {
jrubyExec 'rubygems:rspec:3.3.0' <i class="conum" data-value="3"></i><b>(3)</b>
}
import com.github.jrubygradle.JRubyExec
task spec(type: JRubyExec) {
group 'JRuby'
description 'Execute the RSpecs in JRuby'
script 'rspec'
inputs.source fileTree('spec').include('**/*.rb'), fileTree('lib').include('**/*.rb') <i class="conum" data-value="4"></i><b>(4)</b>
}</code></pre>
</div>
</div>
<div class="colist arabic">
<table>
<tr>
<td><i class="conum" data-value="1"></i><b>1</b></td>
<td>Specify our dependency on the JRuby/Gradle <a href="/base/">base plugin</a></td>
</tr>
<tr>
<td><i class="conum" data-value="2"></i><b>2</b></td>
<td>Apply the plugin to our current project</td>
</tr>
<tr>
<td><i class="conum" data-value="3"></i><b>3</b></td>
<td>Define our RSpec gem dependency</td>
</tr>
<tr>
<td><i class="conum" data-value="4"></i><b>4</b></td>
<td>Set our task inputs to the <code>.rb</code> files in <code>spec/</code> and in <code>lib/</code></td>
</tr>
</table>
</div>
<div class="paragraph">
<p>Using the <code>build.gradle</code> above, I can auto-execute my tests whenever a Ruby file
inside of the <code>spec/</code> (my tests) or <code>lib/</code> (my code under test) with the
following command:</p>
</div>
<div class="literalblock">
<div class="content">
<pre class="nowrap">% ./gradlew -t spec</pre>
</div>
</div>
<div class="paragraph">
<p>Here&#8217;s some example output from my example project:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="nowrap">example-project git:(master) % ./gradlew -t spec
Continuous build is an incubating feature.
:spec
Randomized with seed 37453
..............................................
Finished in 0.52 seconds (files took 3.82 seconds to load)
46 examples, 0 failures
Randomized with seed 37453
BUILD SUCCESSFUL
Total time: 8.77 secs
Waiting for changes to input files of tasks... (ctrl-d to exit)</pre>
</div>
</div>
<div class="paragraph">
<p>At this point the Gradle process is patiently waiting until I write my most
recent changes, then it kicks off the same task:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="nowrap">Change detected, executing build...
:spec
Randomized with seed 64935
..............................................
Finished in 0.502 seconds (files took 3.5 seconds to load)
46 examples, 0 failures
Randomized with seed 64935
BUILD SUCCESSFUL
Total time: 7.341 secs
Waiting for changes to input files of tasks... (ctrl-d to exit)</pre>
</div>
</div>
<div class="paragraph">
<p>Pretty neat huh?</p>
</div>
<div class="paragraph">
<p>What makes this functionality exceptionally powerful for JRuby/Gradle users is
that it respects the task inputs but also the task dependency graph. If my <code>spec</code>
task declares a dependency on the <code>compileJava</code> task, whenever my Java source
code changes, that will trigger a re-execution of <code>compileJava</code> and in turn
<code>spec</code>!</p>
</div>
<div class="paragraph">
<p>So when you&#8217;re authoring tasks, even if you&#8217;re not this feature right now, be
sure to declare task inputs. That build metadata can unlock lots of interesting
functionality as Gradle continues to improve!</p>
</div>
<div class="paragraph">
<p>Continuous build is one of many examples of powerful Gradle functionality which
can easily used in JRuby/Gradle, I hope you find it useful!</p>
</div>
</div>
<hr>
</body>
</html>