<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ted Naleid</title>
	<atom:link href="http://naleid.com/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://naleid.com/blog</link>
	<description>Groovy, Grails and OS X tips and tricks</description>
	<lastBuildDate>Wed, 16 May 2012 00:54:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Upgrading to Grails 2 Unit Testing</title>
		<link>http://naleid.com/blog/2012/05/01/upgrading-to-grails-2-unit-testing/</link>
		<comments>http://naleid.com/blog/2012/05/01/upgrading-to-grails-2-unit-testing/#comments</comments>
		<pubDate>Wed, 02 May 2012 04:15:28 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
				<category><![CDATA[grails]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=580</guid>
		<description><![CDATA[Grails 2 has a lot of great new unit testing features that make many test scenarios easier. The grails documentation does an OK job of describing some of the new features, but there really wasn&#8217;t anywhere that I could find that had a comprehensive list of changes you should make to your code when migrating [...]]]></description>
			<content:encoded><![CDATA[<p>Grails 2 has a lot of great <a href="http://grails.org/doc/latest/guide/testing.html#9.3 Functional Testing" onclick="pageTracker._trackPageview('/outgoing/grails.org/doc/latest/guide/testing.html_9.3_Functional_Testing?referer=');">new unit testing features</a> that make many test scenarios easier.</p>
<p>The grails documentation does an OK job of describing some of the new features, but there really wasn&#8217;t anywhere that I could find that had a comprehensive list of changes you should make to your code when migrating from grails 1.3.X to 2.0.X.</p>
<p>This blog post is the list of changes that I wished I had when I started to migrate our code.<span id="more-580"></span></p>
<p>I found these pages helpful in figuring things out:</p>
<ul>
<li>The <a href="http://grails.org/doc/latest/guide/testing.html#9.3 Functional Testing" onclick="pageTracker._trackPageview('/outgoing/grails.org/doc/latest/guide/testing.html_9.3_Functional_Testing?referer=');">Grails 2 Testing Documentation</a></li>
<li>Peter Ledbrook&#8217;s <a href="http://pledbrook.github.com/grails-howtos/en/upgradeToGrails2.html" onclick="pageTracker._trackPageview('/outgoing/pledbrook.github.com/grails-howtos/en/upgradeToGrails2.html?referer=');">&#8220;Upgrade to Grails 2&#8243;</a> blog post</li>
<li>The Grails 2 <a href="http://www.grails.org/doc/2.0.x/guide/gettingStarted.html#upgradingFromPreviousVersionsOfGrails" onclick="pageTracker._trackPageview('/outgoing/www.grails.org/doc/2.0.x/guide/gettingStarted.html_upgradingFromPreviousVersionsOfGrails?referer=');">&#8220;Upgrading From Previous Versions of Grails&#8221;</a> section of the user guide.</li>
<li>Rob Fletcher&#8217;s <a href="http://blog.freeside.co/blog/2011/11/29/grails-2-upgrade-part-1/" onclick="pageTracker._trackPageview('/outgoing/blog.freeside.co/blog/2011/11/29/grails-2-upgrade-part-1/?referer=');">Upgrading to Grails 2: Part 1</a></li>
<li>This google plus thread on <a href="https://plus.google.com/117411438136918964913/posts/axyCZk1P5L5" onclick="pageTracker._trackPageview('/outgoing/plus.google.com/117411438136918964913/posts/axyCZk1P5L5?referer=');">people&#8217;s experiences upgrading to grails 2</a>
<li><a href="http://jira.grails.org/browse/GRAILS-7617" onclick="pageTracker._trackPageview('/outgoing/jira.grails.org/browse/GRAILS-7617?referer=');">GRAILS-7617</a> &#8211; TestFor(ControllerClass) annotation doesn&#8217;t provide renderArgs</li>
</ul>
<p>The company I&#8217;m consulting at made a large investment in unit testing in the grails 1.2/1.3 days with 500-600 unit tests that heavily leverage custom extended base classes, `Mixin`s, and the grails 1.3 way of doing unit testing.   This investment helped us keep most of our testing quick in unit tests, but has made this the most painful grails upgrade cycle I&#8217;ve been through (more painful than the grails 1.0->1.1 upgrade, which was the previous worst).</p>
<p>There have been surprisingly few changes to the actual production code to get things going, the vast majority of changes have been in how unit testing works.</p>
<h3>General Test Upgrade Tips</h3>
<ul>
<li>If you use an IDE with a debugger, it&#8217;s <strong>very</strong> useful to have a separate copy of your code that works against grails 1.3.7 so that you can have 2 instances of IntelliJ and can step through line by line in each codebase to see where things diverge.</li>
<li>Methods with <code>@Test</code>, <code>@Before</code>, and <code>@After</code> MUST be declared <code>public void</code> or else they won&#8217;t be picked up</li>
<li>I had real problems with using the new grails annotations (like <code>@Mock</code>) while extending a custom super-class, I haven&#8217;t dug to the bottom of this yet, but the grails annotations seem to have trouble with class hierarchies, avoid them if possible</li>
<li>To that end, if you use the new grails annotations (like <code>@TestFor</code>), you MUST remove the old <code>extends GrailsUnitTestCase</code></li>
<li>Integration tests should also never extend <code>GrailsUnitTestCase</code>, they should instead extend <code>GroovyTestCase</code> and just use <code>assert</code></li>
<li>I&#8217;ve hit a number of test pollution issues when refactoring my code, where one test hasn&#8217;t properly cleaned up after itself affecting other tests downstream.  If your test passes in isolation, you&#8217;ll need to run the tests in the same order as <code>grails test-app</code> does and slowly take out tests till your tests passes.  Then you know which test is causing the pollution.  The grails 2 test runner doesn&#8217;t show you the test order, but you can get it by setting a breakpoint in the <code>JUnit4GrailsTestType.doPrepare</code> method (line 54 as of grails 2.0.3) and executing this in your IDE: <code>testClasses.name.each { println it }</code>.  Then you can take that list, join it together into one line and prepend <code>grails test-app</code> on it to run them in order.</li>
</ul>
<p>Another way to get the in-order list of test classes from the test reports after a failed test using a little grep and sed:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">grep</span> testsuite target<span style="color: #000000; font-weight: bold;">/</span>test-reports<span style="color: #000000; font-weight: bold;">/</span>TESTS-TestSuites.xml <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-v</span> testsuites <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cut</span> -d\  <span style="color: #660033;">-f8-9</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-E</span> <span style="color: #ff0000;">'s/name=&quot;(.*)&quot; package=&quot;(.*)&quot;/\2.\1/'</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> .</pre></div></div>

<h3>Mocking Class Methods by mucking with the metaClass</h3>
<p>Groovy is now more stringent on closure arguments matching the type signature of a method it&#8217;s meant to mock.  If the method you&#8217;re mocking types it&#8217;s variables, you need to match that with your closure.</p>
<p>grails 1.3.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">MyDomain.<span style="color: #006600;">metaClass</span>.<span style="color: #ff0000;">'static'</span>.<span style="color: #993399; font-weight: bold;">executeUpdate</span> <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span> theQuery, theParams <span style="color: #66cc66;">-&gt;</span> <span style="color: #000000; font-weight: bold;">return</span><span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Grails 2.0.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">MyDomain.<span style="color: #006600;">metaClass</span>.<span style="color: #ff0000;">'static'</span>.<span style="color: #993399; font-weight: bold;">executeUpdate</span> <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #aaaadd; font-weight: bold;">String</span> theQuery, <span style="color: #aaaadd; font-weight: bold;">Collection</span> theParams <span style="color: #66cc66;">-&gt;</span> <span style="color: #000000; font-weight: bold;">return</span><span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Otherwise it won&#8217;t find your closure and will still call the regular method you&#8217;re trying to mock.  Put a breakpoint/<code>println</code> in your mocked methods to ensure that they&#8217;re being called by the code under test.</p>
<h2>Unit Testing Domain Classes in Grails 2</h2>
<h3>mockDomain</h3>
<p>grails 1.3.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> MyTests <span style="color: #66cc66;">&#123;</span> 
    ...
    <span style="color: #006600;">mockDomain</span><span style="color: #66cc66;">&#40;</span>Person<span style="color: #66cc66;">&#41;</span>
    mockDomain<span style="color: #66cc66;">&#40;</span>Vehicle<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Grails 2.0.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">@Mock<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>Person, Vehicle<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #000000; font-weight: bold;">class</span> MyTests <span style="color: #66cc66;">&#123;</span>
   ...</pre></div></div>

<p>If you&#8217;ve given <code>mockDomain</code> instances to save, you&#8217;ll need to save them manually now.</p>
<p>grails 1.3.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">mockDomain<span style="color: #66cc66;">&#40;</span>Person, <span style="color: #66cc66;">&#91;</span><span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #66cc66;">&#40;</span>name: <span style="color: #ff0000;">&quot;Bob&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>If you keep the old style of domain mocking through <code>mockDomain</code> you need to ensure that you aren&#8217;t calling it twice on the same class (say in setUp and then again in a test method, or in a superclass and a subclass).  This causes really hard to find test pollution errors downstream with the grails 2.0.3 code.  It appears that it&#8217;s messing with the metaClass again and re-mocking it, but then when the test is done not fully cleaning up after both metaClass modifications.  I consider this a bug as <code>mockDomain</code> should be idempotent; if you&#8217;ve already mocked a class calling <code>mockDomain</code> again shouldn&#8217;t remock the class, but this is what we have now.</p>
<p>Grails 2.0.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">@TestMixin<span style="color: #66cc66;">&#40;</span>GrailsUnitTestMixin<span style="color: #66cc66;">&#41;</span>
@Mock<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>Person<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #000000; font-weight: bold;">class</span> MyTests <span style="color: #66cc66;">&#123;</span>
    ...
    @Test <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> testPerson<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #66cc66;">&#40;</span>name: <span style="color: #ff0000;">&quot;Bob&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span>failOnError: <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>
    ...</pre></div></div>

<p>One caveat is that validation is now enforced on these objects, you can either pass in <code>validate:false</code> on the object, or use the <a href="https://bitbucket.org/tednaleid/grails-test-data/wiki/Home" onclick="pageTracker._trackPageview('/outgoing/bitbucket.org/tednaleid/grails-test-data/wiki/Home?referer=');">grails BuildTestData plugin</a> to create your valid objects:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">@TestMixin<span style="color: #66cc66;">&#40;</span>GrailsUnitTestMixin<span style="color: #66cc66;">&#41;</span>
@Build<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>Person<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #000000; font-weight: bold;">class</span> MyTests <span style="color: #66cc66;">&#123;</span>
    ...
    @Test <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> testPerson<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        Person.<span style="color: #006600;">build</span><span style="color: #66cc66;">&#40;</span>name: <span style="color: #ff0000;">&quot;Bob&quot;</span><span style="color: #66cc66;">&#41;</span>
        ...</pre></div></div>

<p>The Build Test Data plugin has recently been enhanced to work with grails unit tests, and I recommend using it&#8217;s <code>@Build</code> annotation in place of <code>@Mock</code> in pretty much every case.  It just makes things so much easier by building fully valid objects automatically for you.</p>
<h2>Controller Unit Tests in Grails 2</h2>
<h3>mockConfig</h3>
<p>grails 1.3.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">mockConfig <span style="color: #ff0000;">&quot;&quot;&quot;
    grails.foo.bar='baz'
&quot;&quot;&quot;</span></pre></div></div>

<p>Grails 2.0.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">grailsApplication.<span style="color: #006600;">config</span>.<span style="color: #006600;">grails</span>.<span style="color: #006600;">foo</span>.<span style="color: #006600;">bar</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'baz'</span></pre></div></div>

<h3>mockParams</h3>
<p>grails 1.3.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">mockParams.<span style="color: #006600;">foo</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;bar&quot;</span></pre></div></div>

<p>Grails 2.0.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">params.<span style="color: #006600;">foo</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;bar&quot;</span></pre></div></div>

<p>So you can replace all <code>mockParams</code> with just <code>params</code>.</p>
<h3>mockFlash</h3>
<p>grails 1.3.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">assertEquals <span style="color: #ff0000;">&quot;Expected Message&quot;</span>, mockFlash.<span style="color: #006600;">error</span></pre></div></div>

<p>Grails 2.0.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #ff0000;">&quot;Expected Message&quot;</span> <span style="color: #66cc66;">==</span> flash.<span style="color: #006600;">error</span></pre></div></div>

<p>So you can replace all <code>mockFlash</code> with just <code>flash</code>.</p>
<h3>renderArgs and redirectArgs</h3>
<p>The very useful <code>renderArgs</code> and <code>redirectArgs</code> mock objects are gone in grails 2.0 and I find the new way of testing without them to be harder than it was.</p>
<h3>renderArgs.view</h3>
<p>grails 1.3.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #ff0000;">'edit'</span> <span style="color: #66cc66;">==</span> renderArgs.<span style="color: #006600;">view</span></pre></div></div>

<p>Grails 2.0.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">assert</span> view.<span style="color: #006600;">endsWith</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'/edit'</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<h3>redirectArgs</h3>
<p>grails 1.3.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">assertEquals <span style="color: #ff0000;">'person'</span>, redirectArgs.<span style="color: #006600;">controller</span>
assertEquals <span style="color: #ff0000;">'show'</span>, redirectArgs.<span style="color: #006600;">action</span>
assertEquals personInstance.<span style="color: #006600;">id</span>, redirectArgs.<span style="color: #006600;">id</span></pre></div></div>

<p>Grails 2.0.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">assert</span> response.<span style="color: #006600;">redirectUrl</span>.<span style="color: #006600;">endsWith</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;/person/show/${personInstance.id}&quot;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>(or just use <code>contains</code> or <code>.split('/')</code> to find parts of the url)</p>
<h3>renderArgs.model</h3>
<p>grails 1.3.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">assert</span> renderArgs.<span style="color: #006600;">model</span>.<span style="color: #006600;">person</span>.<span style="color: #006600;">id</span> <span style="color: #66cc66;">==</span> person.<span style="color: #006600;">id</span></pre></div></div>

<p>Grails 2.0.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">assert</span> model.<span style="color: #006600;">person</span>.<span style="color: #006600;">id</span> <span style="color: #66cc66;">==</span> person.<span style="color: #006600;">id</span></pre></div></div>

<p>This seems to work unless you&#8217;re rendering a template in your controller rather than a view. In that case, the model isn&#8217;t populated and cannot be accessed as far as I can tell.  This is documented in <a href="http://jira.grails.org/browse/GRAILS-8659" onclick="pageTracker._trackPageview('/outgoing/jira.grails.org/browse/GRAILS-8659?referer=');">GRAILS-8659</a> which is marked as &#8220;Not a Bug&#8221;, something I disagree with as the workaround is much more painful.</p>
<p>The workaround, per Graeme Rocher on that ticket is to mock the template rendering:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">views<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'/test/_bar.gsp'</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'Hello ${name}'</span>
controller.<span style="color: #006600;">renderTemplate</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #000000; font-weight: bold;">assert</span> response.<span style="color: #006600;">contentAsString</span> <span style="color: #66cc66;">==</span> <span style="color: #ff0000;">&quot;Hello John&quot;</span></pre></div></div>

<p>Where controller:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">def</span> renderTemplate<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    render template:<span style="color: #ff0000;">&quot;bar&quot;</span>, model:<span style="color: #66cc66;">&#91;</span>name:<span style="color: #ff0000;">&quot;John&quot;</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>The naming convention of what you set in the map should be in the format &#8220;/person/_editAddress.gsp&#8221;, where you&#8217;re in the <code>PersonController</code> and the template name is <code>editAddress</code> but there were a few situations where I had trouble getting this right.</p>
<p>If you want to be sure what value to set the path to in the <code>views</code> map, set a breakpoint in <code>RenderDynamicMethod.java</code> around like 319 where it does this check:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #aaaadd; font-weight: bold;">String</span> templateUri <span style="color: #66cc66;">=</span> webRequest.<span style="color: #006600;">getAttributes</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getTemplateURI</span><span style="color: #66cc66;">&#40;</span>controller, templateName<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span></pre></div></div>

<p>The value of that <code>templateUri</code> is what you want to set in your <code>views</code> map.</p>
<h3>redirectArgs.params</h3>
<p>grails 1.3.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #ff0000;">&quot;bar&quot;</span> <span style="color: #66cc66;">==</span> redirectArgs.<span style="color: #006600;">params</span>.<span style="color: #006600;">foo</span></pre></div></div>

<p>Grails 2.0.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// strip off everything up till the ? that marks the start of the query string</span>
<span style="color: #000000; font-weight: bold;">def</span> params <span style="color: #66cc66;">=</span> WebUtils.<span style="color: #006600;">fromQueryString</span><span style="color: #66cc66;">&#40;</span>response.<span style="color: #006600;">redirectUrl</span>.<span style="color: #663399;">find</span><span style="color: #66cc66;">&#40;</span>/<span style="color: #66cc66;">&#91;</span>^<span style="color: #66cc66;">?</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">*</span>$/<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #ff0000;">&quot;bar&quot;</span> <span style="color: #66cc66;">==</span> params.<span style="color: #006600;">foo</span></pre></div></div>

<p>gross.  This really should be better and usability has regressed on this.</p>
<h3>mockResponse</h3>
<p>grails 1.3.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">assertEquals <span style="color: #cc66cc;">404</span>, mockResponse.<span style="color: #006600;">status</span>
assertEquals <span style="color: #ff0000;">&quot;foo bar baz&quot;</span>, mockResponse.<span style="color: #006600;">contentAsString</span></pre></div></div>

<p>Grails 2.0.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #cc66cc;">404</span> <span style="color: #66cc66;">==</span> response.<span style="color: #006600;">status</span>
assertEquals <span style="color: #ff0000;">&quot;foo bar baz&quot;</span>, response.<span style="color: #006600;">contentAsString</span></pre></div></div>

<p>So you should be able to find/replace <code>mockResponse</code> with just <code>response</code></p>
<h3>mockLogging</h3>
<p>grails 1.3.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">mockLogging<span style="color: #66cc66;">&#40;</span>Foo<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>grails 2.0.x:</p>
<p>In many cases, this can simply be deleted.  If it&#8217;s either the class under test (the class you are doing a <code>@TestFor</code> on) or something you&#8217;ve already got in a <code>@Mock</code> or a <code>@Build</code> class level annotation, it already has logging.</p>
<p>For those classes that need logging, but are not already mocked, you can use the <code>grails.test.MockUtils.mockLogging</code> static method.  Just add this static import to your class to get the same behavior:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #a1a100;">static</span> grails.<span style="color: #006600;">test</span>.<span style="color: #006600;">MockUtils</span>.<span style="color: #66cc66;">*</span>
    ...
    <span style="color: #006600;">mockLogging</span><span style="color: #66cc66;">&#40;</span>MyService<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>If it&#8217;s a collaborator that isn&#8217;t already mocked out, you can use the <code>mockFor</code> method as described in the <a href="http://grails.org/doc/latest/guide/testing.html#mockingCollaborators" onclick="pageTracker._trackPageview('/outgoing/grails.org/doc/latest/guide/testing.html_mockingCollaborators?referer=');">mocking collaborators</a> section of the grails user docs.</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">def</span> foo <span style="color: #66cc66;">=</span> mockFor<span style="color: #66cc66;">&#40;</span>Foo, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">// true here is &quot;loose&quot; mocking, otherwise you need to `demand` method calls for them to not fail.</span></pre></div></div>

<p>Overall, I&#8217;m much happier with grails 2.0.  It has a number of great features and speed improvements that make it a worthwhile upgrade.  I complain above about a few places where I think testing is not as easy as it was, but most of these could be remedied with a few patches to grails-core.  </p>
]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2012/05/01/upgrading-to-grails-2-unit-testing/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Quick Shell Function to Bootstrap a Gradle Groovy Project</title>
		<link>http://naleid.com/blog/2012/02/29/quick-shell-function-to-bootstrap-a-gradle-groovy-project/</link>
		<comments>http://naleid.com/blog/2012/02/29/quick-shell-function-to-bootstrap-a-gradle-groovy-project/#comments</comments>
		<pubDate>Thu, 01 Mar 2012 03:57:39 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
				<category><![CDATA[command line]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[gradle]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[osx]]></category>
		<category><![CDATA[shortcut]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=571</guid>
		<description><![CDATA[Gradle is a great build tool. It&#8217;s easy to download and install. If you&#8217;re on a mac and have homebrew, it&#8217;s as easy as: brew install gradle It&#8217;s very easy to use with a little experience, but I find having a good starting place to base your work from can help. Here&#8217;s a quick function [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://gradle.org/" onclick="pageTracker._trackPageview('/outgoing/gradle.org/?referer=');">Gradle</a> is a great build tool.  It&#8217;s easy to <a href="http://gradle.org/downloads" onclick="pageTracker._trackPageview('/outgoing/gradle.org/downloads?referer=');">download</a> and install.  If you&#8217;re on a mac and have <a href="http://mxcl.github.com/homebrew/" onclick="pageTracker._trackPageview('/outgoing/mxcl.github.com/homebrew/?referer=');">homebrew</a>, it&#8217;s as easy as:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">brew <span style="color: #c20cb9; font-weight: bold;">install</span> gradle</pre></div></div>

<p>It&#8217;s very easy to use with a little experience, but I find having a good starting place to base your work from can help.</p>
<p>Here&#8217;s a quick function that I&#8217;ve got in my <code>.zshrc</code> to bootstrap a new groovy gradle project in the current directory (it should also work in in a .profile/.bash_profile/.bashrc).</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> newgradle<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Creating files for new gradle project&quot;</span>
&nbsp;
    <span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #000000; font-weight: bold;">&lt;&lt;</span>EOF<span style="color: #000000; font-weight: bold;">&gt;</span>.gitignore
<span style="color: #000000; font-weight: bold;">*</span>.un~
<span style="color: #000000; font-weight: bold;">*</span>.iml
<span style="color: #000000; font-weight: bold;">*</span>.ipr
<span style="color: #000000; font-weight: bold;">*</span>.iws
build
.gradle
EOF
&nbsp;
    <span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #000000; font-weight: bold;">&lt;&lt;</span>EOF<span style="color: #000000; font-weight: bold;">&gt;</span>build.gradle
apply plugin: <span style="color: #ff0000;">'groovy'</span>
apply plugin: <span style="color: #ff0000;">'idea'</span>
&nbsp;
repositories <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    mavenCentral<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
dependencies <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    groovy <span style="color: #ff0000;">'org.codehaus.groovy:groovy-all:1.8.6'</span>
    groovy <span style="color: #ff0000;">'org.apache.ivy:ivy:2.2.0'</span>
    testCompile <span style="color: #ff0000;">'junit:junit:4.10'</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
task createSourceDirs<span style="color: #7a0874; font-weight: bold;">&#40;</span>description : <span style="color: #ff0000;">'Create empty source directories for all defined sourceSets'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">&lt;&lt;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    sourceSets<span style="color: #000000; font-weight: bold;">*</span>.allSource.srcDirs.flatten<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>.each <span style="color: #7a0874; font-weight: bold;">&#123;</span> File sourceDirectory -<span style="color: #000000; font-weight: bold;">&gt;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">!</span>sourceDirectory.exists<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
            println <span style="color: #ff0000;">&quot;Making <span style="color: #000099; font-weight: bold;">\$</span>sourceDirectory&quot;</span>
            sourceDirectory.mkdirs<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
        <span style="color: #7a0874; font-weight: bold;">&#125;</span>
    <span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
idea <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    project <span style="color: #7a0874; font-weight: bold;">&#123;</span>
        jdkName = <span style="color: #ff0000;">'1.6'</span>
    <span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
EOF
&nbsp;
    gradle createSourceDirs
&nbsp;
    <span style="color: #c20cb9; font-weight: bold;">git</span> init
    <span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #660033;">-a1</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #c20cb9; font-weight: bold;">find</span> src    <span style="color: #666666; font-style: italic;"># list all created assets</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<p>It creates a <code>build.gradle</code> file ready to work with java and groovy projects, including IDEA integration (just execute <code>gradle idea</code>).</p>
<p>This gives you all of the tasks necessary to compile, jar, test, and distribute your code.  For more information, check out the gradle docs on the <a href="http://gradle.org/docs/current/userguide/java_plugin.html" onclick="pageTracker._trackPageview('/outgoing/gradle.org/docs/current/userguide/java_plugin.html?referer=');">java</a>, <a href="http://gradle.org/docs/current/userguide/groovy_plugin.html" onclick="pageTracker._trackPageview('/outgoing/gradle.org/docs/current/userguide/groovy_plugin.html?referer=');">groovy</a>, and <a href="http://gradle.org/docs/current/userguide/idea_plugin.html" onclick="pageTracker._trackPageview('/outgoing/gradle.org/docs/current/userguide/idea_plugin.html?referer=');">idea</a> tasks.</p>
<p>It also creates all the necessary source directories for you and initializes a new git repository (with starting <code>.gitignore</code> file) for you to save your work.</p>
<p>You can easily tweak the <code>build.gradle</code> or <code>.gitignore</code> files to fit your needs.  If you don&#8217;t use git, you can either delete those lines, or subsitute the equivalent lines for the source control tool you use. These are just a good starting place for me.</p>
<p>Here&#8217;s the sample output from the script above:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">%</span> <span style="color: #c20cb9; font-weight: bold;">mkdir</span> testapp
<span style="color: #000000; font-weight: bold;">%</span> <span style="color: #7a0874; font-weight: bold;">cd</span> testapp
<span style="color: #000000; font-weight: bold;">%</span> newgradle                                                                   
Creating files <span style="color: #000000; font-weight: bold;">for</span> new gradle project
:createSourceDirs
Making <span style="color: #000000; font-weight: bold;">/</span>Users<span style="color: #000000; font-weight: bold;">/</span>tnaleid<span style="color: #000000; font-weight: bold;">/</span>Documents<span style="color: #000000; font-weight: bold;">/</span>workspace<span style="color: #000000; font-weight: bold;">/</span>testapp<span style="color: #000000; font-weight: bold;">/</span>src<span style="color: #000000; font-weight: bold;">/</span>main<span style="color: #000000; font-weight: bold;">/</span>resources
Making <span style="color: #000000; font-weight: bold;">/</span>Users<span style="color: #000000; font-weight: bold;">/</span>tnaleid<span style="color: #000000; font-weight: bold;">/</span>Documents<span style="color: #000000; font-weight: bold;">/</span>workspace<span style="color: #000000; font-weight: bold;">/</span>testapp<span style="color: #000000; font-weight: bold;">/</span>src<span style="color: #000000; font-weight: bold;">/</span>main<span style="color: #000000; font-weight: bold;">/</span>java
Making <span style="color: #000000; font-weight: bold;">/</span>Users<span style="color: #000000; font-weight: bold;">/</span>tnaleid<span style="color: #000000; font-weight: bold;">/</span>Documents<span style="color: #000000; font-weight: bold;">/</span>workspace<span style="color: #000000; font-weight: bold;">/</span>testapp<span style="color: #000000; font-weight: bold;">/</span>src<span style="color: #000000; font-weight: bold;">/</span>main<span style="color: #000000; font-weight: bold;">/</span>groovy
Making <span style="color: #000000; font-weight: bold;">/</span>Users<span style="color: #000000; font-weight: bold;">/</span>tnaleid<span style="color: #000000; font-weight: bold;">/</span>Documents<span style="color: #000000; font-weight: bold;">/</span>workspace<span style="color: #000000; font-weight: bold;">/</span>testapp<span style="color: #000000; font-weight: bold;">/</span>src<span style="color: #000000; font-weight: bold;">/</span>test<span style="color: #000000; font-weight: bold;">/</span>resources
Making <span style="color: #000000; font-weight: bold;">/</span>Users<span style="color: #000000; font-weight: bold;">/</span>tnaleid<span style="color: #000000; font-weight: bold;">/</span>Documents<span style="color: #000000; font-weight: bold;">/</span>workspace<span style="color: #000000; font-weight: bold;">/</span>testapp<span style="color: #000000; font-weight: bold;">/</span>src<span style="color: #000000; font-weight: bold;">/</span>test<span style="color: #000000; font-weight: bold;">/</span>java
Making <span style="color: #000000; font-weight: bold;">/</span>Users<span style="color: #000000; font-weight: bold;">/</span>tnaleid<span style="color: #000000; font-weight: bold;">/</span>Documents<span style="color: #000000; font-weight: bold;">/</span>workspace<span style="color: #000000; font-weight: bold;">/</span>testapp<span style="color: #000000; font-weight: bold;">/</span>src<span style="color: #000000; font-weight: bold;">/</span>test<span style="color: #000000; font-weight: bold;">/</span>groovy
&nbsp;
BUILD SUCCESSFUL
&nbsp;
Total <span style="color: #000000; font-weight: bold;">time</span>: <span style="color: #000000;">2.344</span> secs
Initialized empty Git repository <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">/</span>Users<span style="color: #000000; font-weight: bold;">/</span>tnaleid<span style="color: #000000; font-weight: bold;">/</span>Documents<span style="color: #000000; font-weight: bold;">/</span>workspace<span style="color: #000000; font-weight: bold;">/</span>testapp<span style="color: #000000; font-weight: bold;">/</span>.git<span style="color: #000000; font-weight: bold;">/</span>
.
..
.git
.gitignore
.gradle
build.gradle
src
src
src<span style="color: #000000; font-weight: bold;">/</span>main
src<span style="color: #000000; font-weight: bold;">/</span>main<span style="color: #000000; font-weight: bold;">/</span>groovy
src<span style="color: #000000; font-weight: bold;">/</span>main<span style="color: #000000; font-weight: bold;">/</span>java
src<span style="color: #000000; font-weight: bold;">/</span>main<span style="color: #000000; font-weight: bold;">/</span>resources
src<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">test</span>
src<span style="color: #000000; font-weight: bold;">/</span>test<span style="color: #000000; font-weight: bold;">/</span>groovy
src<span style="color: #000000; font-weight: bold;">/</span>test<span style="color: #000000; font-weight: bold;">/</span>java
src<span style="color: #000000; font-weight: bold;">/</span>test<span style="color: #000000; font-weight: bold;">/</span>resources</pre></div></div>

<p>Then you&#8217;ve got all this gradle functionality ready to use:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">%</span> gradle tasks                                                           
:tasks
&nbsp;
<span style="color: #660033;">------------------------------------------------------------</span>
All tasks runnable from root project
<span style="color: #660033;">------------------------------------------------------------</span>
&nbsp;
Build tasks
<span style="color: #660033;">-----------</span>
assemble - Assembles all Jar, War, Zip, and Tar archives.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
classes - Assembles the main classes.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the main classes.
testClasses - Assembles the <span style="color: #7a0874; font-weight: bold;">test</span> classes.
&nbsp;
Documentation tasks
<span style="color: #660033;">-------------------</span>
groovydoc - Generates Groovydoc API documentation <span style="color: #000000; font-weight: bold;">for</span> the main <span style="color: #7a0874; font-weight: bold;">source</span> code.
javadoc - Generates Javadoc API documentation <span style="color: #000000; font-weight: bold;">for</span> the main <span style="color: #7a0874; font-weight: bold;">source</span> code.
&nbsp;
Help tasks
<span style="color: #660033;">----------</span>
dependencies - Displays the dependencies of root project <span style="color: #ff0000;">'test'</span>.
<span style="color: #7a0874; font-weight: bold;">help</span> - Displays a <span style="color: #7a0874; font-weight: bold;">help</span> message
projects - Displays the sub-projects of root project <span style="color: #ff0000;">'test'</span>.
properties - Displays the properties of root project <span style="color: #ff0000;">'test'</span>.
tasks - Displays the tasks runnable from root project <span style="color: #ff0000;">'test'</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>some of the displayed tasks may belong to subprojects<span style="color: #7a0874; font-weight: bold;">&#41;</span>.
&nbsp;
IDE tasks
<span style="color: #660033;">---------</span>
cleanIdea - Cleans IDEA project files <span style="color: #7a0874; font-weight: bold;">&#40;</span>IML, IPR<span style="color: #7a0874; font-weight: bold;">&#41;</span>
idea - Generates IDEA project files <span style="color: #7a0874; font-weight: bold;">&#40;</span>IML, IPR, IWS<span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
Verification tasks
<span style="color: #660033;">------------------</span>
check - Runs all checks.
<span style="color: #7a0874; font-weight: bold;">test</span> - Runs the unit tests.
&nbsp;
Other tasks
<span style="color: #660033;">-----------</span>
cleanIdeaWorkspace
createSourceDirs - Create empty <span style="color: #7a0874; font-weight: bold;">source</span> directories <span style="color: #000000; font-weight: bold;">for</span> all defined sourceSets
&nbsp;
Rules
<span style="color: #660033;">-----</span>
Pattern: build<span style="color: #000000; font-weight: bold;">&lt;</span>ConfigurationName<span style="color: #000000; font-weight: bold;">&gt;</span>: Assembles the artifacts of a configuration.
Pattern: upload<span style="color: #000000; font-weight: bold;">&lt;</span>ConfigurationName<span style="color: #000000; font-weight: bold;">&gt;</span>: Assembles and uploads the artifacts belonging to a configuration.
Pattern: clean<span style="color: #000000; font-weight: bold;">&lt;</span>TaskName<span style="color: #000000; font-weight: bold;">&gt;</span>: Cleans the output files of a task.
&nbsp;
To see all tasks and <span style="color: #c20cb9; font-weight: bold;">more</span> detail, run with --all.
&nbsp;
BUILD SUCCESSFUL
&nbsp;
Total <span style="color: #000000; font-weight: bold;">time</span>: <span style="color: #000000;">4.871</span> secs</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2012/02/29/quick-shell-function-to-bootstrap-a-gradle-groovy-project/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Finding and Purging Big Files From Git History</title>
		<link>http://naleid.com/blog/2012/01/17/finding-and-purging-big-files-from-git-history/</link>
		<comments>http://naleid.com/blog/2012/01/17/finding-and-purging-big-files-from-git-history/#comments</comments>
		<pubDate>Tue, 17 Jan 2012 06:58:13 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
				<category><![CDATA[command line]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[osx]]></category>
		<category><![CDATA[shortcut]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=566</guid>
		<description><![CDATA[On a recent grails project, we&#8217;re using a git repo that was originally converted from a SVN repo with a ton of large binary objects in it (lots of jar files that really should come from an ivy/maven repo). The .git directory was over a gigabyte in size and this made it very cumbersome to [...]]]></description>
			<content:encoded><![CDATA[<p>On a recent grails project, we&#8217;re using a git repo that was originally converted from a SVN repo with a ton of large binary objects in it (lots of jar files that really should come from an ivy/maven repo).  The <code>.git</code> directory was over a gigabyte in size and this made it very cumbersome to clone and manipulate.</p>
<p>We decided to leverage git&#8217;s history rewriting capabilities to make a much smaller repository (and kept our previous repo as a backup just in case).</p>
<p>Here are a few questions/answers that I figured out how to answer with git and some shell commands:</p>
<h4>What object SHA is associated with each file in the Repo?</h4>
<p>Git has a unique SHA that it associates with each object (such as files which it calls blobs) throughout it&#8217;s history.</p>
<p>This helps us find that object and decide whether it&#8217;s worth deleting later on:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">git</span> rev-list <span style="color: #660033;">--objects</span> <span style="color: #660033;">--all</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sort</span> <span style="color: #660033;">-k</span> <span style="color: #000000;">2</span> <span style="color: #000000; font-weight: bold;">&gt;</span> allfileshas.txt</pre></div></div>

<p>Take a look at the resulting <code>allfileshas.txt</code> file for the full list.  </p>
<h4>What Unique Files Exist Throughout The History of My Git Repo?</h4>
<p>If you want to see the unique files throughout the history of your git repo (such as to grep for .jar files that you might have committed a while ago):</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">    <span style="color: #c20cb9; font-weight: bold;">git</span> rev-list <span style="color: #660033;">--objects</span> <span style="color: #660033;">--all</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sort</span> <span style="color: #660033;">-k</span> <span style="color: #000000;">2</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cut</span> <span style="color: #660033;">-f</span> <span style="color: #000000;">2</span> -d\  <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">uniq</span></pre></div></div>

<h4>How Big Are The Files In My Repo?</h4>
<p>We can find the big files in our repo by doing a <code>git gc</code> which makes git compact the archive and stores an index file that we can analyse.</p>
<p>Get the last object SHA for all committed files and sort them in biggest to smallest order:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">git</span> <span style="color: #c20cb9; font-weight: bold;">gc</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #c20cb9; font-weight: bold;">git</span> verify-pack <span style="color: #660033;">-v</span> .git<span style="color: #000000; font-weight: bold;">/</span>objects<span style="color: #000000; font-weight: bold;">/</span>pack<span style="color: #000000; font-weight: bold;">/</span>pack-<span style="color: #000000; font-weight: bold;">*</span>.idx <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">egrep</span> <span style="color: #ff0000;">&quot;^\w+ blob\W+[0-9]+ [0-9]+ [0-9]+$&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sort</span> <span style="color: #660033;">-k</span> <span style="color: #000000;">3</span> <span style="color: #660033;">-n</span> <span style="color: #660033;">-r</span> <span style="color: #000000; font-weight: bold;">&gt;</span> bigobjects.txt</pre></div></div>

<p>Take that result and iterate through each line of it to find the SHA, file size in bytes, and real file name (you also need the allfileshas.txt output file from above):</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">for</span> SHA <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">cut</span> <span style="color: #660033;">-f</span> <span style="color: #000000;">1</span> -d\  <span style="color: #000000; font-weight: bold;">&lt;</span> bigobjects.txt<span style="color: #000000; font-weight: bold;">`</span>; <span style="color: #000000; font-weight: bold;">do</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #007800;">$SHA</span> bigobjects.txt<span style="color: #7a0874; font-weight: bold;">&#41;</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #007800;">$SHA</span> allfileshas.txt<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{print $1,$3,$7}'</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> bigtosmall.txt
<span style="color: #000000; font-weight: bold;">done</span>;</pre></div></div>

<p>(there&#8217;s probably a more efficient way to do this, but this was fast enough for my purposes with ~50k files in our repo)</p>
<p>Then, just take a look at the bigtosmall.txt file to see your biggest file culprits.</p>
<h4>Purging a file or directory from history</h4>
<p>Use filter-branch to remove the file/directory (replace <code>MY-BIG-DIRECTORY-OR-FILE</code> with the path that you&#8217;d like to delete relative to the root of the git repo:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">git</span> filter-branch <span style="color: #660033;">--prune-empty</span> <span style="color: #660033;">--index-filter</span> <span style="color: #ff0000;">'git rm -rf --cached --ignore-unmatch MY-BIG-DIRECTORY-OR-FILE'</span> <span style="color: #660033;">--tag-name-filter</span> <span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #660033;">--</span> <span style="color: #660033;">--all</span></pre></div></div>

<p>Then clone the repo and make sure to not leave any hard links with:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">git</span> clone <span style="color: #660033;">--no-hardlinks</span> <span style="color: #c20cb9; font-weight: bold;">file</span>:<span style="color: #000000; font-weight: bold;">///</span>Users<span style="color: #000000; font-weight: bold;">/</span>yourUser<span style="color: #000000; font-weight: bold;">/</span>your<span style="color: #000000; font-weight: bold;">/</span>full<span style="color: #000000; font-weight: bold;">/</span>repo<span style="color: #000000; font-weight: bold;">/</span>path repo-clone-name</pre></div></div>

<p>You can use this command from the parent directory that contains your git repository and it&#8217;s clone to see how much space each of them take, and how much you&#8217;ve shrunk the repo in size:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">du</span> <span style="color: #660033;">-s</span> <span style="color: #000000; font-weight: bold;">*</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>     <span style="color: #666666; font-style: italic;"># add the -h flag to see the output in human readable size formats, just like ls -lah vs ls -la</span></pre></div></div>

<p>With these commands, I was able to reduce the file size of our repo with a few thousand commits down below the size of the checked out repository (more than an order of magnitude smaller).  I only removed old binary files, we still have full history for all code files.</p>
]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2012/01/17/finding-and-purging-big-files-from-git-history/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>How to use kdiff3 as a 3-way merge tool with mercurial, git, and Tower.app</title>
		<link>http://naleid.com/blog/2012/01/12/how-to-use-kdiff3-as-a-3-way-merge-tool-with-mercurial-git-and-tower-app/</link>
		<comments>http://naleid.com/blog/2012/01/12/how-to-use-kdiff3-as-a-3-way-merge-tool-with-mercurial-git-and-tower-app/#comments</comments>
		<pubDate>Thu, 12 Jan 2012 06:29:08 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
				<category><![CDATA[command line]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[mercurial]]></category>
		<category><![CDATA[osx]]></category>
		<category><![CDATA[shortcut]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=523</guid>
		<description><![CDATA[There are a few very nice looking, mac-like diff tools for OSX (Kaleidoscope and Changes come to mind), but none for doing &#8220;real&#8221; merges. By this, I mean real, 3-way merges with all of the information you need in front of you. There are no good-looking, &#8220;mac-like&#8221; merge tools, but if you swallow your pride [...]]]></description>
			<content:encoded><![CDATA[<p>There are a few very nice looking, mac-like diff tools for OSX (<a href="http://www.kaleidoscopeapp.com/" onclick="pageTracker._trackPageview('/outgoing/www.kaleidoscopeapp.com/?referer=');">Kaleidoscope</a> and <a href="http://connectedflow.com/changes/" onclick="pageTracker._trackPageview('/outgoing/connectedflow.com/changes/?referer=');">Changes</a> come to mind), but none for doing &#8220;real&#8221; merges.  By this, I mean real, <a href="http://en.wikipedia.org/wiki/3-way_merge#Three-way_merge" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/3-way_merge_Three-way_merge?referer=');">3-way merges</a> with all of the information you need in front of you.</p>
<p>There are no good-looking, &#8220;mac-like&#8221; merge tools, but if you swallow your pride there are a few different options for 3-way merges, including <a href="http://www.araxis.com/merge_mac/" onclick="pageTracker._trackPageview('/outgoing/www.araxis.com/merge_mac/?referer=');">Araxis Merge ($$$!)</a>, <a href="http://www.sourcegear.com/diffmerge/downloads.php" onclick="pageTracker._trackPageview('/outgoing/www.sourcegear.com/diffmerge/downloads.php?referer=');">DiffMerge</a>, <a href="http://www.deltopia.com/compare-merge-sync/macosx/" onclick="pageTracker._trackPageview('/outgoing/www.deltopia.com/compare-merge-sync/macosx/?referer=');">DeltaWalker</a>, and <a href="http://en.wikipedia.org/wiki/Apple_Developer_Tools#FileMerge" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Apple_Developer_Tools_FileMerge?referer=');">FileMerge</a> which comes free with XCode.</p>
<p>I&#8217;ve tried them all, and find them all confusing.  They all tend use a 3-pane display to do the merging with your file in the left pane, the file you&#8217;re merging in the right pane, and the messy half-merged file in the middle.</p>
<p>That&#8217;s not enough information.</p>
<p>A 3-way merge actually has four important sources of information:<span id="more-523"></span></p>
<ul>
<li><code>LOCAL</code> &#8211; your file with the changes you&#8217;ve made to it</li>
<li><code>REMOTE</code> &#8211; the file you&#8217;re merging in, possibly authored by someone else</li>
<li><code>BASE</code> &#8211; the common ancestor file that <code>LOCAL</code> and <code>REMOTE</code> came from</li>
<li><code>MERGE_RESULT</code> &#8211; the file resulting from the merge where you resolve conflicts</li>
</ul>
<p>You often need to see all four of these pieces of information to make intelligent choices.  Where you came from (<code>LOCAL</code>), where the other person&#8217;s changes came from (<code>REMOTE</code>), where you both started (<code>BASE</code>) and where you are now (<code>MERGE_RESULT</code>).</p>
<p>Most other 3-way merge tools either conflate or omit the <code>BASE</code> and that can make it harder to see what the right thing to do is.</p>
<p><a href="http://kdiff3.sourceforge.net/" onclick="pageTracker._trackPageview('/outgoing/kdiff3.sourceforge.net/?referer=');">Kdiff3</a> is my merge tool of choice.  It&#8217;s not pretty; it&#8217;s cross-platform <a href="http://en.wikipedia.org/wiki/Qt_(framework)" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Qt_framework?referer=');">Qt</a> based so it has a very old-school linux GUI feel to it.  But like linux, it&#8217;s functional and can help you quickly be productive once you get over the learning curve.</p>
<p>If you&#8217;ve got 2 heads that you need to merge in your current repository:<br />
<a href="http://naleid.com/blog/wp-content/uploads/2012/01/2_heads_need_to_merge.png"><img src="http://naleid.com/blog/wp-content/uploads/2012/01/2_heads_need_to_merge.png" alt="" title="2_heads_need_to_merge" width="403" height="107" class="aligncenter size-full wp-image-526" /></a><br />
We made this modification to <code>file.txt</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">diff</span> <span style="color: #660033;">--git</span> a<span style="color: #000000; font-weight: bold;">/</span>file.txt b<span style="color: #000000; font-weight: bold;">/</span>file.txt
<span style="color: #660033;">---</span> a<span style="color: #000000; font-weight: bold;">/</span>file.txt
+++ b<span style="color: #000000; font-weight: bold;">/</span>file.txt
<span style="color: #000000; font-weight: bold;">@@</span> -<span style="color: #000000;">1</span>,<span style="color: #000000;">1</span> +<span style="color: #000000;">1</span>,<span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">@@</span>
-BASE: common_ancestor
+LOCAL: <span style="color: #c20cb9; font-weight: bold;">file</span> <span style="color: #000000; font-weight: bold;">in</span> current working branch</pre></div></div>

<p>and they made this change to the same file and line:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">diff</span> <span style="color: #660033;">--git</span> a<span style="color: #000000; font-weight: bold;">/</span>file.txt b<span style="color: #000000; font-weight: bold;">/</span>file.txt
<span style="color: #660033;">---</span> a<span style="color: #000000; font-weight: bold;">/</span>file.txt
+++ b<span style="color: #000000; font-weight: bold;">/</span>file.txt
<span style="color: #000000; font-weight: bold;">@@</span> -<span style="color: #000000;">1</span>,<span style="color: #000000;">1</span> +<span style="color: #000000;">1</span>,<span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">@@</span>
-BASE: common_ancestor
+REMOTE: other branch we<span style="color: #ff0000;">'re merging in</span></pre></div></div>

<p>If you run your SCM&#8217;s merge command (here, in mercurial: <code>hg merge -r 2</code>), and have kdiff3 configured as your merge tool, you&#8217;ll get a pop-up window like this:<br />
<a href="http://naleid.com/blog/wp-content/uploads/2012/01/kdiff3_merge_window_fixed.png"><img src="http://naleid.com/blog/wp-content/uploads/2012/01/kdiff3_merge_window_fixed.png" alt="" title="kdiff3_merge_window_fixed" width="1241" height="512" class="alignleft size-full wp-image-555" /></a><br />
As you can see, it shows you all 4 pieces of information, <code>BASE</code>, <code>LOCAL</code>, and <code>REMOTE</code> on top, and the <code>MERGE_RESULT</code> file on the bottom.  It currently has a <code>Merge Conflict</code> that you need to fix.</p>
<p>You can move from one unresolved conflict to the next using the triple up and triple-down colored arrows in the middle of the tool bar.  When a conflict is highlighted, you can press any combination of the A, B, and C buttons in the toolbar.  Pressing one of those buttons will resolve the conflict with the code from pane A, B, or C on top.  So if the <code>LOCAL</code> file (your file) had the right changes in it, you&#8217;d press B.</p>
<p>It&#8217;s possible to press more than one button if code from multiple panes is valid.  You can also directly edit the file in the <code>MERGE_RESULT</code> pane to make manual changes if the correct merge is not the exact text in A/B/C.</p>
<p>Another option, if you want to take all of the changes from one file and discard any changes from the others, is to go to the &#8220;Merge&#8221; menu and pick one of &#8220;Choose A Everywhere&#8221;, &#8220;Choose B Everywhere&#8221;, or &#8220;Choose C Everywhere&#8221;.</p>
<p>Once you&#8217;ve resolved your file, simply save it (cmd-S) and quit out of kdiff3.  Your SCM should see the <code>MERGE_RESULT</code> no longer has any merge conflicts and will mark it as resolved, ready for you to commit it.  If there are other files with merge conflicts, you can repeat the process with those files.</p>
<h2>Installing kdiff3</h2>
<p>Installing kdiff3 is as easy as <a href="http://sourceforge.net/projects/kdiff3/files/kdiff3/" onclick="pageTracker._trackPageview('/outgoing/sourceforge.net/projects/kdiff3/files/kdiff3/?referer=');">downloading the latest version from sourceforge</a> and copying it to your <code>/Applications</code> directory.</p>
<p>The app is a simple wrapper around a Qt-based application.  It can be run from the command line at <code>/Applications/kdiff3.app/Contents/MacOS/kdiff3</code>.  You can make a symlink of that into your path, but I assume in the instructions below only that you&#8217;ve got kdiff3 in your <code>/Applications</code> folder.</p>
<h2>Mercurial Integration</h2>
<p>Mercurial command line integration is pretty easy.  Just open up your <code>~/.hgrc</code> file (or create one if you don&#8217;t have it already), and add this to it:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>extdiff<span style="color: #7a0874; font-weight: bold;">&#93;</span>
cmd.kdiff3 = <span style="color: #000000; font-weight: bold;">/</span>Applications<span style="color: #000000; font-weight: bold;">/</span>kdiff3.app<span style="color: #000000; font-weight: bold;">/</span>Contents<span style="color: #000000; font-weight: bold;">/</span>MacOS<span style="color: #000000; font-weight: bold;">/</span>kdiff3
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>merge-tools<span style="color: #7a0874; font-weight: bold;">&#93;</span>
kdiff3.args = <span style="color: #007800;">$base</span> <span style="color: #007800;">$local</span> <span style="color: #007800;">$other</span> <span style="color: #660033;">-o</span> <span style="color: #007800;">$output</span></pre></div></div>

<p>That configures kdiff3 as your merge tool of choice, so it should pop up automatically when you hit a merge conflict.  You can also use it as a diff tool:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">hg kdiff3 <span style="color: #660033;">-r</span> revision_to_compare_to</pre></div></div>

<h2>Git Command-Line Integration</h2>
<p>To configure the git command line to use kdiff3 as a diff and merge tool, add this to your ~/.gitconfig:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>difftool <span style="color: #ff0000;">&quot;kdiff3&quot;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
    path = <span style="color: #000000; font-weight: bold;">/</span>Applications<span style="color: #000000; font-weight: bold;">/</span>kdiff3.app<span style="color: #000000; font-weight: bold;">/</span>Contents<span style="color: #000000; font-weight: bold;">/</span>MacOS<span style="color: #000000; font-weight: bold;">/</span>kdiff3
    trustExitCode = <span style="color: #c20cb9; font-weight: bold;">false</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>difftool<span style="color: #7a0874; font-weight: bold;">&#93;</span>
    prompt = <span style="color: #c20cb9; font-weight: bold;">false</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #c20cb9; font-weight: bold;">diff</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
    tool = kdiff3
<span style="color: #7a0874; font-weight: bold;">&#91;</span>mergetool <span style="color: #ff0000;">&quot;kdiff3&quot;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
    path = <span style="color: #000000; font-weight: bold;">/</span>Applications<span style="color: #000000; font-weight: bold;">/</span>kdiff3.app<span style="color: #000000; font-weight: bold;">/</span>Contents<span style="color: #000000; font-weight: bold;">/</span>MacOS<span style="color: #000000; font-weight: bold;">/</span>kdiff3
    trustExitCode = <span style="color: #c20cb9; font-weight: bold;">false</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>mergetool<span style="color: #7a0874; font-weight: bold;">&#93;</span>
    keepBackup = <span style="color: #c20cb9; font-weight: bold;">false</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>merge<span style="color: #7a0874; font-weight: bold;">&#93;</span>
    tool = kdiff3</pre></div></div>

<p>Now you can use the external tool commands to look at diffs:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">git</span> difftool <span style="color: #7a0874; font-weight: bold;">&#91;</span>revision_sha<span style="color: #7a0874; font-weight: bold;">&#93;</span></pre></div></div>

<p>and fix unresolved merge conflicts:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">git</span> mergetool</pre></div></div>

<h2>Git Tower Integration</h2>
<p>I&#8217;m normally a pretty hard-core command line user, but I still sometimes find the places I get myself in git confusing enough that I&#8217;m willing to use a GUI to get myself out.  I think that <a href="http://www.git-tower.com/" onclick="pageTracker._trackPageview('/outgoing/www.git-tower.com/?referer=');">git-tower</a> is the best git GUI available right now. </p>
<p>It can show you diff files and lets you browse through history in it&#8217;s UI. It also lets you do a number of basic and mid-level commands (adding, committing, rebasing, squashing commits, cherry-picking, etc), but it doesn&#8217;t have a built-in diff or merge tool.  </p>
<p>It does come with the ability to integrate with a few different merge tools out of the box, but not kdiff3.  </p>
<p>After a little digging around, I found a generic <a href="http://www.git-tower.com/files/applicationHelp/pgs/Integration_DiffTools.html" onclick="pageTracker._trackPageview('/outgoing/www.git-tower.com/files/applicationHelp/pgs/Integration_DiffTools.html?referer=');">post showing how configure Tower with a plist file and a shim shell script</a>, but nothing specific to kdiff3.</p>
<p>I figured out how to get Tower and kdiff3 to play nice with each other, and have the instructions and files in a <a href="https://github.com/tednaleid/git-tower-kdiff3-shim" onclick="pageTracker._trackPageview('/outgoing/github.com/tednaleid/git-tower-kdiff3-shim?referer=');">GitHub repository</a>.  To get it working, just clone the repository to a temporary directory, and run the install.sh script:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>tmp
<span style="color: #c20cb9; font-weight: bold;">git</span> clone <span style="color: #c20cb9; font-weight: bold;">git</span><span style="color: #000000; font-weight: bold;">@</span>github.com:tednaleid<span style="color: #000000; font-weight: bold;">/</span>git-tower-kdiff3-shim.git 
<span style="color: #7a0874; font-weight: bold;">cd</span> git-tower-kdiff3-shim
.<span style="color: #000000; font-weight: bold;">/</span>install.sh</pre></div></div>

<p>It simply copies the plist file and the shim script into the appropriate directories (and Tower is <i>very</i> picky about those locations).</p>
<p>Then you need to go into Tower&#8217;s Preferences->Git Config control panel, and choose kdiff3 as the diff and merge tool:<br />
<a href="http://naleid.com/blog/wp-content/uploads/2012/01/tower_preferences.png"><img src="http://naleid.com/blog/wp-content/uploads/2012/01/tower_preferences.png" alt="" title="tower_preferences" width="764" height="386" class="alignleft size-full wp-image-542" /></a><br />
For a quick rundown of how Tower works with kdiff3 as a merge tool, here&#8217;s an example where we&#8217;ve got one commit where we made changes to locally:<br />
<a href="http://naleid.com/blog/wp-content/uploads/2012/01/my_master.png"><img src="http://naleid.com/blog/wp-content/uploads/2012/01/my_master.png" alt="" title="my_master" width="1416" height="822" class="alignleft size-full wp-image-534" /></a><br />
And another commit on <code>origin/master</code> where someone else made changes to the same piece of code:<br />
<a href="http://naleid.com/blog/wp-content/uploads/2012/01/their_master.png"><img src="http://naleid.com/blog/wp-content/uploads/2012/01/their_master.png" alt="" title="their_master" width="1416" height="822" class="alignleft size-full wp-image-536" /></a><br />
We&#8217;ve pulled it down and now have 2 heads within our repository. To fix this, we need to merge and resolve the conflicts, hit the merge button and we&#8217;ll get an error message because there are conflicts that git can&#8217;t resolve automatically:<br />
<a href="http://naleid.com/blog/wp-content/uploads/2012/01/tower-conflict.png"><img src="http://naleid.com/blog/wp-content/uploads/2012/01/tower-conflict.png" alt="" title="tower-conflict" width="1416" height="822" class="alignleft size-full wp-image-540" /></a><br />
If you highlight a file with merge conflicts, and then hit the &#8220;Merge Tool&#8221; button, it will bring up kdiff3 and let us use it to resolve the issue:<br />
<a href="http://naleid.com/blog/wp-content/uploads/2012/01/kdiff3-merge.png"><img src="http://naleid.com/blog/wp-content/uploads/2012/01/kdiff3-merge.png" alt="" title="kdiff3-merge" width="1257" height="512" class="alignleft size-full wp-image-545" /></a><br />
Fix the merge conflicts in kdiff3 (ex: press &#8220;C&#8221; to change it to &#8220;Goodbye Cruel World&#8221;), save it, and quit out of kdiff3. Tower should see that the file has had it&#8217;s conflicts resolved and lets you commit the merge and carry on.</p>
<p>You can also use Kdiff3 as a regular diff tool if you don&#8217;t like looking at diff files.  Just choose a file that you&#8217;ve modified to diff:<br />
<a href="http://naleid.com/blog/wp-content/uploads/2012/01/tower-diff.png"><img src="http://naleid.com/blog/wp-content/uploads/2012/01/tower-diff.png" alt="" title="tower-diff" width="1416" height="822" class="alignleft size-full wp-image-547" /></a><br />
Press the &#8220;Diff Tool&#8221; button, and it&#8217;ll pop up in kdiff3:<br />
<a href="http://naleid.com/blog/wp-content/uploads/2012/01/tower-kdiff3.png"><img src="http://naleid.com/blog/wp-content/uploads/2012/01/tower-kdiff3.png" alt="" title="tower-kdiff3" width="1257" height="512" class="alignleft size-full wp-image-548" /></a><br />
Kdiff3 might be homely, but it&#8217;s easy to use once you understand how it works.</p>
]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2012/01/12/how-to-use-kdiff3-as-a-3-way-merge-tool-with-mercurial-git-and-tower-app/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Getting a Clojure REPL in Vim with VimClojure, Nailgun, and Leiningen</title>
		<link>http://naleid.com/blog/2011/12/19/getting-a-clojure-repl-in-vim-with-vimclojure-nailgun-and-leiningen/</link>
		<comments>http://naleid.com/blog/2011/12/19/getting-a-clojure-repl-in-vim-with-vimclojure-nailgun-and-leiningen/#comments</comments>
		<pubDate>Mon, 19 Dec 2011 07:13:32 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
				<category><![CDATA[clojure]]></category>
		<category><![CDATA[command line]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=505</guid>
		<description><![CDATA[Having a Clojure REPL (Read Eval Print Loop) right inside Vim makes it easier to test ideas, get documentation, and explore your code. There are a few hoops that you need to jump through to enable it, but the payoff is worth it. Install Leiningen Leiningen isn&#8217;t required, but it makes managing the classpath for [...]]]></description>
			<content:encoded><![CDATA[<p>Having a Clojure REPL (Read Eval Print Loop) right inside Vim makes it easier to test ideas, get documentation, and explore your code.  There are a few hoops that you need to jump through to enable it, but the payoff is worth it.</p>
<p><span id="more-505"></span><br />
<h2>Install Leiningen</h2>
<p>Leiningen isn&#8217;t required, but it makes managing the classpath for your REPL quite a bit easier and automates the inclusion of the files in your project so that they can be used in the REPL.  </p>
<p>If you&#8217;ve already got clojure installed and are managing your classpath some other way, you&#8217;ll want to modify the nailgun server script below to remove the call to <code>lein</code> and ensure you&#8217;re getting clojure and other classes in the ng-server&#8217;s classpath.</p>
<p>Install <a href="https://github.com/technomancy/leiningen" onclick="pageTracker._trackPageview('/outgoing/github.com/technomancy/leiningen?referer=');">Leiningen</a> if you don&#8217;t have it already.</p>
<p>Download the <a href="https://raw.github.com/technomancy/leiningen/stable/bin/lein" onclick="pageTracker._trackPageview('/outgoing/raw.github.com/technomancy/leiningen/stable/bin/lein?referer=');"><code>lein</code> script</a>.</p>
<p>Put it somewhere on your <code>PATH</code> (such as <code>/usr/local/bin</code> or <code>~/bin</code>).</p>
<p>Set it to be executable:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">chmod</span> +x lein</pre></div></div>

<p>Then, tell it to self-install with this command:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">lein self-install</pre></div></div>

<p>Now you should have a working <code>lein</code> installation:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">lein <span style="color: #7a0874; font-weight: bold;">help</span></pre></div></div>

<p>And you can create a new clojure project skeleton, with clojure 1.3.0 all ready to go with:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">lein new myproj</pre></div></div>

<p>It creates a skeleton project rectory and a <code>project.clj</code> file that you can use to hold lib dependencies.</p>
<p>Go into that project and you can use lein to go into a repl, get the project&#8217;s classpath, run tests, etc.  Use <code>lein help</code> to see the full list of commands.</p>
<h2>Install VimClojure in Vim</h2>
<p>Install the <a href="https://github.com/vim-scripts/VimClojure" onclick="pageTracker._trackPageview('/outgoing/github.com/vim-scripts/VimClojure?referer=');">VimClojure plugin</a> in vim.</p>
<p>Hopefully, you&#8217;re already using Tim Pope&#8217;s vim script management script <a href="https://github.com/tpope/vim-pathogen " onclick="pageTracker._trackPageview('/outgoing/github.com/tpope/vim-pathogen?referer=');">Pathogen</a>.</p>
<p>If so, you should be able to just clone VimClojure into your bundle ectory:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> .vim<span style="color: #000000; font-weight: bold;">/</span>bundle
<span style="color: #c20cb9; font-weight: bold;">git</span> clone <span style="color: #c20cb9; font-weight: bold;">git</span>:<span style="color: #000000; font-weight: bold;">//</span>github.com<span style="color: #000000; font-weight: bold;">/</span>vim-scripts<span style="color: #000000; font-weight: bold;">/</span>VimClojure.git</pre></div></div>

<p>If you&#8217;re not using Pathogen, install the plugin with your normal process.</p>
<p>Edit your <code>.vimrc</code>, make sure you have these settings if they&#8217;re not already there (they need to be after the method that calls pathogen to install bundles):</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">filetype off
call pathogen<span style="color: #666666; font-style: italic;">#runtime_append_all_bundles()</span>
...
filetype plugin indent on
syntax on</pre></div></div>

<p>and add these items to your <code>.vimrc</code></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">let</span> g:vimclojure<span style="color: #666666; font-style: italic;">#HighlightBuiltins = 1</span>
<span style="color: #7a0874; font-weight: bold;">let</span> g:vimclojure<span style="color: #666666; font-style: italic;">#ParenRainbow = 1</span></pre></div></div>

<p>To test that VimClojure is installed OK, edit a clojure file (like this sample one modified from <code>(source +)</code>):</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defn</span> myplus
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>x<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>cast Number x<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>x y<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">.</span> clojure<span style="color: #66cc66;">.</span>lang<span style="color: #66cc66;">.</span>Numbers <span style="color: #66cc66;">&#40;</span>add x y<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>x y <span style="color: #66cc66;">&amp;</span> more<span style="color: #66cc66;">&#93;</span>
     <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">reduce</span> myplus <span style="color: #66cc66;">&#40;</span>myplus x y<span style="color: #66cc66;">&#41;</span> more<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>If the parenthesis are differently colored in matched pairs, that means that &#8220;rainbow&#8221; parenthesis are enabled and the plugin is working.  It should look something like this:</p>
<p><a href="http://naleid.com/blog/wp-content/uploads/2011/12/rainbow_parens.png"><img src="http://naleid.com/blog/wp-content/uploads/2011/12/rainbow_parens.png" alt="" title="rainbow_parens" width="512" height="187" class="aligncenter size-full wp-image-515" /></a></p>
<p>You&#8217;ll also want to update the <a href="https://bitbucket.org/kotarak/vimclojure/src/tip/vim/doc/clojure.txt" onclick="pageTracker._trackPageview('/outgoing/bitbucket.org/kotarak/vimclojure/src/tip/vim/doc/clojure.txt?referer=');">helptags documentation</a> for VimClojure:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">:helptags ~<span style="color: #000000; font-weight: bold;">/</span>.vim<span style="color: #000000; font-weight: bold;">/</span>bundle<span style="color: #000000; font-weight: bold;">/</span>VimClojure<span style="color: #000000; font-weight: bold;">/</span>doc<span style="color: #000000; font-weight: bold;">/</span></pre></div></div>

<p>Now you can lookup help for VimClojure:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">:<span style="color: #7a0874; font-weight: bold;">help</span> VimClojure</pre></div></div>

<p>You&#8217;ve now got a functioning VimClojure installation that can already be pretty helpful.  If you want to get an integrated REPL installed, there&#8217;s a bit more to do.</p>
<h2>Setting up Nailgun to work with VimClojure</h2>
<p>This part is the trickiest part (in the github docs, they say &#8220;Here be Dragons&#8221;).  I think the biggest difficulty is that they don&#8217;t show you how to configure/launch the nailgun server, we&#8217;ll go through those steps.</p>
<p><a href="http://www.martiansoftware.com/nailgun/background.html" onclick="pageTracker._trackPageview('/outgoing/www.martiansoftware.com/nailgun/background.html?referer=');">Nailgun</a> is a server that runs an instance of the JVM.  On startup, you tell it what classpath this JVM should run with.  Then, with a nailgun client, you can send it additional classes to execute within that JVM and it can show you the output.</p>
<p>It makes things much faster as the Nailgun server is already running hot when you send it things to execute.  You don&#8217;t pay any JVM startup penalty.</p>
<p><strong>Warning: If you&#8217;ve previously tried to install nailgun through homebrew, it will not work with clojure.  The server jar that homebrew uses is half the size of the server jar that we&#8217;re going to be using for clojure.  Uninstall it or put the things below earlier in the classpath.</strong></p>
<h4>Compile the VimClojure nailgun client</h4>
<p>We need to compile the VimClojure nailgun client.  You can either download the <a href="http://kotka.de/projects/vimclojure/vimclojure-nailgun-client-2.3.0.zip" onclick="pageTracker._trackPageview('/outgoing/kotka.de/projects/vimclojure/vimclojure-nailgun-client-2.3.0.zip?referer=');">sources</a> directly or else <a href="https://bitbucket.org/kotarak/vimclojure" onclick="pageTracker._trackPageview('/outgoing/bitbucket.org/kotarak/vimclojure?referer=');">clone the bitbucket repo</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">hg clone https:<span style="color: #000000; font-weight: bold;">//</span>bitbucket.org<span style="color: #000000; font-weight: bold;">/</span>kotarak<span style="color: #000000; font-weight: bold;">/</span>vimclojure</pre></div></div>

<p>Go into the client directory and <code>make</code> the ng client executable:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> vimclojure<span style="color: #000000; font-weight: bold;">/</span>client  <span style="color: #666666; font-style: italic;"># or cd vimclojure-nailgun-client</span>
<span style="color: #c20cb9; font-weight: bold;">make</span></pre></div></div>

<p>This will create an executable called <code>ng</code> in the current directory.  Copy that somewhere in your path (likely the same place you put <code>lein</code> above).</p>
<p>You should now be able to type <code>ng</code> and have it spit out help text.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">%</span> ng
Usage: ng class <span style="color: #7a0874; font-weight: bold;">&#91;</span>--nailgun-options<span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>args<span style="color: #7a0874; font-weight: bold;">&#93;</span>
          <span style="color: #7a0874; font-weight: bold;">&#40;</span>to execute a class<span style="color: #7a0874; font-weight: bold;">&#41;</span>
   or: ng <span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>--nailgun-options<span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>args<span style="color: #7a0874; font-weight: bold;">&#93;</span>
          <span style="color: #7a0874; font-weight: bold;">&#40;</span>to execute an aliased class<span style="color: #7a0874; font-weight: bold;">&#41;</span>
   or: <span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>--nailgun-options<span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>args<span style="color: #7a0874; font-weight: bold;">&#93;</span>
          <span style="color: #7a0874; font-weight: bold;">&#40;</span>to execute an aliased class, where <span style="color: #ff0000;">&quot;alias&quot;</span>
           is both the <span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #000000; font-weight: bold;">for</span> the class and a symbolic
           <span style="color: #c20cb9; font-weight: bold;">link</span> to the ng client<span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
where options include:
   --nailgun-D<span style="color: #000000; font-weight: bold;">&lt;</span>name<span style="color: #000000; font-weight: bold;">&gt;</span>=<span style="color: #000000; font-weight: bold;">&lt;</span>value<span style="color: #000000; font-weight: bold;">&gt;</span>   set<span style="color: #000000; font-weight: bold;">/</span>override a client environment variable
   <span style="color: #660033;">--nailgun-version</span>           print product version and <span style="color: #7a0874; font-weight: bold;">exit</span>
   <span style="color: #660033;">--nailgun-showversion</span>       print product version and <span style="color: #7a0874; font-weight: bold;">continue</span>
   <span style="color: #660033;">--nailgun-server</span>            to specify the address of the nailgun server
                               <span style="color: #7a0874; font-weight: bold;">&#40;</span>default is localhost<span style="color: #7a0874; font-weight: bold;">&#41;</span>
   <span style="color: #660033;">--nailgun-port</span>              to specify the port of the nailgun server
                               <span style="color: #7a0874; font-weight: bold;">&#40;</span>default is <span style="color: #000000;">2113</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
   <span style="color: #660033;">--nailgun-help</span>              print this message and <span style="color: #7a0874; font-weight: bold;">exit</span></pre></div></div>

<h4>Setting up Nailgun Server</h4>
<p>Download latest <a href="http://clojars.org/repo/vimclojure/server/" onclick="pageTracker._trackPageview('/outgoing/clojars.org/repo/vimclojure/server/?referer=');">server jar from clojars</a> (currently <a href="http://clojars.org/repo/vimclojure/server/2.3.0/server-2.3.0.jar" onclick="pageTracker._trackPageview('/outgoing/clojars.org/repo/vimclojure/server/2.3.0/server-2.3.0.jar?referer=');">2.3.0</a>).</p>
<p>Put that jar somewhere that you won&#8217;t delete it, and then add an export for that location in your .bashrc/.vimrc, ex:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">VIMCLOJURE_SERVER_JAR</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$HOME</span>/lib/vimclojure/server-2.3.0.jar&quot;</span></pre></div></div>

<p>Now save this script as <code>ng-server</code> and put it somewhere in your path and make it executable:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#! /bin/bash</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-z</span> <span style="color: #007800;">$VIMCLOJURE_SERVER_JAR</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Error! Need to define location of VimClojure nailgun server jar with:&quot;</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;export VIMCLOJURE_SERVER_JAR=&lt;location&gt;&quot;</span>
    <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #660033;">-f</span> <span style="color: #007800;">$VIMCLOJURE_SERVER_JAR</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Error! Unable to find VimClojure nailgun server jar at '<span style="color: #007800;">$VIMCLOJURE_SERVER_JAR</span>'&quot;</span>
    <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #007800;">LEIN_CLASSPATH</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span>lein classpath<span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #007800;">$LEIN_CLASSPATH</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Warning! Unable to get classpath from lein, just using existing classpath, expecting clojure jars to be available&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #007800;">NG_CLASSPATH</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$VIMCLOJURE_SERVER_JAR</span>:<span style="color: #007800;">$LEIN_CLASSPATH</span>:<span style="color: #007800;">$CLASSPATH</span>&quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> java <span style="color: #660033;">-server</span> <span style="color: #660033;">-cp</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$NG_CLASSPATH</span>&quot;</span> vimclojure.nailgun.NGServer
java <span style="color: #660033;">-server</span> <span style="color: #660033;">-cp</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$NG_CLASSPATH</span>&quot;</span> vimclojure.nailgun.NGServer</pre></div></div>

<p>Then add this to your .vimrc setup:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #ff0000;">&quot; this should only be necessary if you don't have the ng client in your PATH
let vimclojure#NailgunClient = &quot;</span><span style="color: #000000; font-weight: bold;">/</span>path<span style="color: #000000; font-weight: bold;">/</span>to<span style="color: #000000; font-weight: bold;">/</span>your<span style="color: #000000; font-weight: bold;">/</span>ng<span style="color: #ff0000;">&quot;
let vimclojure#WantNailgun = 1</span></pre></div></div>

<p>Now, from the root of a lein project, make sure you&#8217;re dependencies are updated:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">lein deps   <span style="color: #000000; font-weight: bold;">//</span> puts things defined <span style="color: #000000; font-weight: bold;">in</span> project.clj into .<span style="color: #000000; font-weight: bold;">/</span>lib by default</pre></div></div>

<p>Without additional configuration, you&#8217;ll likely just have the clojure-1.3.0.jar in there.</p>
<p>The script above leverages lein to create a classpath that the nailgun server can use.  If you run <code>lein classpath</code>, you&#8217;ll see what it will use:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">%</span> lein classpath
<span style="color: #000000; font-weight: bold;">/</span>Users<span style="color: #000000; font-weight: bold;">/</span>tnaleid<span style="color: #000000; font-weight: bold;">/</span>Documents<span style="color: #000000; font-weight: bold;">/</span>workspace<span style="color: #000000; font-weight: bold;">/</span>clojure-test<span style="color: #000000; font-weight: bold;">/</span>myproj<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">test</span>:<span style="color: #000000; font-weight: bold;">/</span>Users<span style="color: #000000; font-weight: bold;">/</span>tnaleid<span style="color: #000000; font-weight: bold;">/</span>Documents<span style="color: #000000; font-weight: bold;">/</span>workspace<span style="color: #000000; font-weight: bold;">/</span>clojure-test<span style="color: #000000; font-weight: bold;">/</span>myproj<span style="color: #000000; font-weight: bold;">/</span>test-resources:<span style="color: #000000; font-weight: bold;">/</span>Users<span style="color: #000000; font-weight: bold;">/</span>tnaleid<span style="color: #000000; font-weight: bold;">/</span>Documents<span style="color: #000000; font-weight: bold;">/</span>workspace<span style="color: #000000; font-weight: bold;">/</span>clojure-test<span style="color: #000000; font-weight: bold;">/</span>myproj<span style="color: #000000; font-weight: bold;">/</span>src:<span style="color: #000000; font-weight: bold;">/</span>Users<span style="color: #000000; font-weight: bold;">/</span>tnaleid<span style="color: #000000; font-weight: bold;">/</span>Documents<span style="color: #000000; font-weight: bold;">/</span>workspace<span style="color: #000000; font-weight: bold;">/</span>clojure-test<span style="color: #000000; font-weight: bold;">/</span>myproj<span style="color: #000000; font-weight: bold;">/</span>classes:<span style="color: #000000; font-weight: bold;">/</span>Users<span style="color: #000000; font-weight: bold;">/</span>tnaleid<span style="color: #000000; font-weight: bold;">/</span>Documents<span style="color: #000000; font-weight: bold;">/</span>workspace<span style="color: #000000; font-weight: bold;">/</span>clojure-test<span style="color: #000000; font-weight: bold;">/</span>myproj<span style="color: #000000; font-weight: bold;">/</span>resources:<span style="color: #000000; font-weight: bold;">/</span>Users<span style="color: #000000; font-weight: bold;">/</span>tnaleid<span style="color: #000000; font-weight: bold;">/</span>Documents<span style="color: #000000; font-weight: bold;">/</span>workspace<span style="color: #000000; font-weight: bold;">/</span>clojure-test<span style="color: #000000; font-weight: bold;">/</span>myproj<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>clojure-1.3.0.jar</pre></div></div>

<p>Now, you should be able to start up a nailgun server from the root of a lein project and have it automatically pull in all dependencies, ready to recieve client requests.</p>
<p>Open up the src/leinprojectname/core.clj file in vim.  Then press</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">\sr</pre></div></div>

<p>to get a REPL window that you can interact with.  (if you&#8217;ve remapped localleader to something else, replace <code>\</code> with your custom key)</p>
<p>There are quite a few other things you can do with the REPL, check out <code>:help VimClojure</code> and go down to <code>Keybindings</code> for more details.</p>
<p>Here are some good starter ones:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">\el  - <span style="color: #7a0874; font-weight: bold;">eval</span> current line
\eb  - <span style="color: #7a0874; font-weight: bold;">eval</span> current visual block selected
\ef  - <span style="color: #7a0874; font-weight: bold;">eval</span> current <span style="color: #c20cb9; font-weight: bold;">file</span>
&nbsp;
\sr  - start interactive REPL
\sR  - start interactive REPL initialized to have same namespace <span style="color: #c20cb9; font-weight: bold;">as</span> current buffer
&nbsp;
\si  - prompt <span style="color: #000000; font-weight: bold;">for</span> input and lookup with <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">source</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
\fd  - prompt <span style="color: #000000; font-weight: bold;">for</span> input and lookup with <span style="color: #7a0874; font-weight: bold;">&#40;</span>find-doc<span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<h2>The Pain is Over, Now Reap the Rewards</h2>
<p>Once all this configuration is done, it should be as easy as running the server in a lein project:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">ng-server</pre></div></div>

<p>And then editing your files in vim accessing the embedded REPL whenever you need it.</p>
]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2011/12/19/getting-a-clojure-repl-in-vim-with-vimclojure-nailgun-and-leiningen/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Speed up your Grails / Spring Security Development with an Auto Login Bookmarklet</title>
		<link>http://naleid.com/blog/2011/11/29/speed-up-your-grails-spring-security-development-with-autologin-bookmarklet/</link>
		<comments>http://naleid.com/blog/2011/11/29/speed-up-your-grails-spring-security-development-with-autologin-bookmarklet/#comments</comments>
		<pubDate>Tue, 29 Nov 2011 05:35:21 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
				<category><![CDATA[grails]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[shortcut]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=479</guid>
		<description><![CDATA[When you&#8217;re doing dev on your website, how often do you log in with the same username and password? I bet it&#8217;s 20+ of times a day when you&#8217;re actively developing. Having to log in manually impedes development speed. If you watch what your browser is doing when it&#8217;s interacting with a Spring Security application, [...]]]></description>
			<content:encoded><![CDATA[<p>When you&#8217;re doing dev on your website, how often do you log in with the same username and password?  I bet it&#8217;s 20+ of times a day when you&#8217;re actively developing.</p>
<p>Having to log in manually impedes development speed.  </p>
<p>If you watch what your browser is doing when it&#8217;s interacting with a Spring Security application, you&#8217;ll see that (by default) it&#8217;s POSTing 2 parameters (<code>j_username</code> and <code>j_password</code>) to <code>http://localhost:8080/YOURAPP/j_spring_security_check</code>.</p>
<p>It&#8217;s easy to automate the login process with a little bit of vanilla javascript. Edit this javascript url to replace <code>YOURAPP</code>, <code>YOURUSERNAME</code>, and <code>YOURPASSWORD</code>, then make a bookmark out of it in your browser:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">javascript<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>var<span style="color: #339933;">%</span>20path<span style="color: #339933;">=</span><span style="color: #3366CC;">'http://localhost:8080/YOURAPP/j_spring_security_check'</span><span style="color: #339933;">;</span>var<span style="color: #339933;">%</span>20params<span style="color: #339933;">=</span><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'j_username'</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'YOURUSERNAME'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'j_password'</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'YOURPASSWORD'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>var<span style="color: #339933;">%</span>20form<span style="color: #339933;">=</span>document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;form&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>form.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;method&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;POST&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>form.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;action&quot;</span><span style="color: #339933;">,</span>path<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>for<span style="color: #009900;">&#40;</span>var<span style="color: #339933;">%</span>20key<span style="color: #339933;">%</span>20in<span style="color: #339933;">%</span>20params<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>var<span style="color: #339933;">%</span>20hiddenField<span style="color: #339933;">=</span>document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;input&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>hiddenField.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;type&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;hidden&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>hiddenField.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;name&quot;</span><span style="color: #339933;">,</span>key<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>hiddenField.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;value&quot;</span><span style="color: #339933;">,</span>params<span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>form.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>hiddenField<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>document.<span style="color: #660066;">body</span>.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>form<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>form.<span style="color: #660066;">submit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Any time you want to log in, just click that bookmark.  You&#8217;re now fully authenticated and in the app without having to interact with the login page. </p>
<p>Alternatively, if you&#8217;re using Google Chrome (or Firefox), you can create a <a href="http://www.google.com/support/chrome/bin/answer.py?answer=95653" onclick="pageTracker._trackPageview('/outgoing/www.google.com/support/chrome/bin/answer.py?answer=95653&amp;referer=');">&#8220;search engine&#8221;</a> associated with a user-defined keyword.  Type the keyword in the address bar to launch it. </p>
<p>You can even parameterize it to log in as a variety of users.</p>
<p>Say that you&#8217;ve got a number of different test users in your app: &#8220;admin&#8221;, &#8220;joeuser&#8221;, &#8220;sales&#8221;, &#8220;finance&#8221;, etc.  All of the test users have the same password, but different usernames with different roles.  If you make the username in the javascript url a &#8220;%s&#8221;, Chrome will replace that &#8220;%s&#8221; with your &#8220;search term&#8221;.</p>
<p>So if your app is &#8220;superapp&#8221; and all passwords are &#8220;password&#8221;, you can use this to create a Chrome search engine that lets you login with whatever test user you want</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">javascript<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>var<span style="color: #339933;">%</span>20path<span style="color: #339933;">=</span><span style="color: #3366CC;">'http://localhost:8080/superapp/j_spring_security_check'</span><span style="color: #339933;">;</span>var<span style="color: #339933;">%</span>20params<span style="color: #339933;">=</span><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'j_username'</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'%s'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'j_password'</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'password'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>var<span style="color: #339933;">%</span>20form<span style="color: #339933;">=</span>document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;form&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>form.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;method&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;POST&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>form.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;action&quot;</span><span style="color: #339933;">,</span>path<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>for<span style="color: #009900;">&#40;</span>var<span style="color: #339933;">%</span>20key<span style="color: #339933;">%</span>20in<span style="color: #339933;">%</span>20params<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>var<span style="color: #339933;">%</span>20hiddenField<span style="color: #339933;">=</span>document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;input&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>hiddenField.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;type&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;hidden&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>hiddenField.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;name&quot;</span><span style="color: #339933;">,</span>key<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>hiddenField.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;value&quot;</span><span style="color: #339933;">,</span>params<span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>form.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>hiddenField<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>document.<span style="color: #660066;">body</span>.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>form<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>form.<span style="color: #660066;">submit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>To set it up, go into your preferences (cmd-,) and press the &#8220;Manage Search Engines&#8221; button.</p>
<p><a href="http://naleid.com/blog/wp-content/uploads/2011/11/chrome_manage_search_engines.png"><img src="http://naleid.com/blog/wp-content/uploads/2011/11/chrome_manage_search_engines.png" alt="" title="chrome_manage_search_engines" width="875" height="132" class="aligncenter size-full wp-image-480" /></a></p>
<p>Then under &#8220;Other Search Engines&#8221; click in the box to &#8220;Add a new search engine&#8221;</p>
<p><a href="http://naleid.com/blog/wp-content/uploads/2011/11/chrome_add_new_search_engine.png"><img src="http://naleid.com/blog/wp-content/uploads/2011/11/chrome_add_new_search_engine.png" alt="" title="chrome_add_new_search_engine"  class="aligncenter wp-image-481" /></a></p>
<p>Name it with your app&#8217;s name (&#8220;superapp login&#8221;), set the keyword to an abbreviation of your app&#8217;s name (&#8220;sa&#8221;), and set the url to the edited javascript command to log in with your app&#8217;s url/username/password (potentially with the username as &#8220;%s&#8221; to parameterize it).</p>
<p><a href="http://naleid.com/blog/wp-content/uploads/2011/11/chrome_add_new_after.png"><img src="http://naleid.com/blog/wp-content/uploads/2011/11/chrome_add_new_after.png" alt="" title="chrome_add_new_after"  class="aligncenter wp-image-482" /></a></p>
<p>Once you save it, you can then go to your browser&#8217;s address bar (cmd-L) and type your abbreviation (&#8220;sa&#8221;) to get a new &#8220;search engine&#8221;.  Then enter the username you want to log in as.</p>
<p><a href="http://naleid.com/blog/wp-content/uploads/2011/11/chrome_search_engine_in_action.png"><img src="http://naleid.com/blog/wp-content/uploads/2011/11/chrome_search_engine_in_action.png" alt="" title="chrome_search_engine_in_action" class="aligncenter  wp-image-483" /></a></p>
<p>Hit enter and you&#8217;ll automatically be logged in to your app, without having to interact with your normal login page.</p>
<p>Automating this can help to keep you in the zone, especially if you&#8217;re using a security framework that allows deep linking.  </p>
<p>If deep linking is enabled, the quickest way to get back to the page you&#8217;re iterating on after your session has expired (or you&#8217;ve bounced the app) is to reload the page.  As it&#8217;s redirecting you to the login page, go to your address bar (cmd-L), type your keyword (ex: &#8220;sa&#8221;) and any associated username (ex: &#8220;admin&#8221;) and hit enter.  You&#8217;ll be logged in before the login page displays and Spring Security will redirect you back to the page you originally requested.</p>
]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2011/11/29/speed-up-your-grails-spring-security-development-with-autologin-bookmarklet/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Better Grails Batch Import Performance with Redis and Jesque</title>
		<link>http://naleid.com/blog/2011/10/13/better-grails-batch-import-performance-with-redis-and-jesque/</link>
		<comments>http://naleid.com/blog/2011/10/13/better-grails-batch-import-performance-with-redis-and-jesque/#comments</comments>
		<pubDate>Fri, 14 Oct 2011 04:25:25 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
				<category><![CDATA[grails]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[jesque]]></category>
		<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[redis]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=451</guid>
		<description><![CDATA[A couple of years ago, I put up a well-received blog post on tuning Batch Import Performance with Grails an MySQL. I&#8217;ve recently needed to revisit some batch importing procedures and have acquired a few extra tools in my Grails utility belt since writing that post: Grails Redis and Grails Jesque. Redis is a very [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of years ago, I put up a well-received blog post on tuning <a href="http://naleid.com/blog/2009/10/01/batch-import-performance-with-grails-and-mysql/">Batch Import Performance with Grails an MySQL</a>.</p>
<p>I&#8217;ve recently needed to revisit some batch importing procedures and have acquired a few extra tools in my Grails utility belt since writing that post: <a href="http://grails.org/plugin/redis" onclick="pageTracker._trackPageview('/outgoing/grails.org/plugin/redis?referer=');">Grails Redis</a> and <a href="http://grails.org/plugin/jesque" onclick="pageTracker._trackPageview('/outgoing/grails.org/plugin/jesque?referer=');">Grails Jesque</a>.</p>
<p><a href="http://redis.io" onclick="pageTracker._trackPageview('/outgoing/redis.io?referer=');">Redis</a> is a very fast key/value store, where the values are not just strings, but are data structures like lists, sets, and hash maps.  I&#8217;m the main author of the grails redis plugin, and it&#8217;s my favorite pragmatic technology of the past few years.  If you&#8217;re new to Redis, check out the <a href="http://naleid.com/blog/2011/06/27/redis-groovy-and-grails-presentation-at-gr8conf-2011-and-gum/">presentation slides I gave at this year&#8217;s gr8conf</a>.</p>
<p><a href="https://github.com/gresrun/jesque" onclick="pageTracker._trackPageview('/outgoing/github.com/gresrun/jesque?referer=');">Jesque</a> is a Java implementation of <a href="https://github.com/defunkt/resque" onclick="pageTracker._trackPageview('/outgoing/github.com/defunkt/resque?referer=');">Resque</a>.  A Redis-backed message queueing system for creating background jobs.  The Jesque plugin is fully integrated with Grails and allows you to create worker jobs that are spring injected and have an active hibernate session.  Resque was written in Ruby by the folks at GitHub.</p>
<p>This combination makes parallelizing work very easy, as most of the pain of trying to spin off threads in grails is handled for you by Jesque.  Yes, there&#8217;s GPars, but the threads that it creates aren&#8217;t spring injected and don&#8217;t have hibernate sessions.</p>
<p>Using Jesque is as simple as:</p>
<ol>
<li>create a <a href="https://bitbucket.org/tednaleid/example-jesque-batch/src/tip/grails-app/jobs/com/naleid/example/BookConsumerJob.groovy" onclick="pageTracker._trackPageview('/outgoing/bitbucket.org/tednaleid/example-jesque-batch/src/tip/grails-app/jobs/com/naleid/example/BookConsumerJob.groovy?referer=');"><code>Job</code></a> class that implements a <code>perform</code> method.</li>
<li>tell Jesque to start up 1..n worker threads that monitor a queue and use your <code>Job</code> to process work</li>
<li><code>enqueue</code> work on the queue so workers can pick it up</li>
</ol>
<p>I&#8217;ve created a <a href="https://bitbucket.org/tednaleid/example-jesque-batch/src" onclick="pageTracker._trackPageview('/outgoing/bitbucket.org/tednaleid/example-jesque-batch/src?referer=');">bitbucket repository with all of the source code</a> from the original Batch Import post, as well as with the enhancements below.</p>
<p>The example problem is that there is a <a href="https://bitbucket.org/tednaleid/example-jesque-batch/src/tip/src/groovy/com/naleid/example/Library.groovy" onclick="pageTracker._trackPageview('/outgoing/bitbucket.org/tednaleid/example-jesque-batch/src/tip/src/groovy/com/naleid/example/Library.groovy?referer=');">Library</a> class that produces metadata for 100,000 books that we want to persist in the database as <a href="https://bitbucket.org/tednaleid/example-jesque-batch/src/tip/grails-app/domain/com/naleid/example/Book.groovy" onclick="pageTracker._trackPageview('/outgoing/bitbucket.org/tednaleid/example-jesque-batch/src/tip/grails-app/domain/com/naleid/example/Book.groovy?referer=');">Book</a> domain objects.</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #a1a100;">com.naleid.example</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #aaaadd; font-weight: bold;">Book</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #aaaadd; font-weight: bold;">String</span> title
    <span style="color: #aaaadd; font-weight: bold;">String</span> isbn
    <span style="color: #aaaadd; font-weight: bold;">Integer</span> edition
&nbsp;
    <span style="color: #000000; font-weight: bold;">static</span> constraints <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">static</span> mapping <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span>
        isbn column:<span style="color: #ff0000;">'isbn'</span>, index:<span style="color: #ff0000;">'book_isbn_idx'</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>The naive way of doing this takes Grails ~3 hours to do the inserts.  The <a href="http://naleid.com/blog/2009/10/01/batch-import-performance-with-grails-and-mysql/">original batch performance post</a> showed how to improve this time from 3 hours to 3 minutes with a few Grails and MySQL tweaks.  </p>
<p>Using Redis + Jesque to parallelize the task, I&#8217;m able to cut that time in half again to a little over 90 seconds on my MacBook Air.</p>
<p>On real-world imports, where there is quite a bit more data and potentially other linked domain objects that can be memoized with the redis-plugin, I&#8217;ve seen a >100x speed improvement over the original serial import, even with the tuning tips from my original post.</p>
<p>Install <a href="http://redis.io/download" onclick="pageTracker._trackPageview('/outgoing/redis.io/download?referer=');">redis</a> and <a href="https://bitbucket.org/tednaleid/example-jesque-batch/overview" onclick="pageTracker._trackPageview('/outgoing/bitbucket.org/tednaleid/example-jesque-batch/overview?referer=');">clone the test project from bitbucket</a> to try it yourself.  Just <code>grails-run app</code>, go to <a href="http://localhost:8080/example-jesque-batch/" onclick="pageTracker._trackPageview('/outgoing/localhost_8080/example-jesque-batch/?referer=');">the running app on localhost</a> and click on the link to the <a href="http://localhost:8080/example-jesque-batch/serialBook/index" onclick="pageTracker._trackPageview('/outgoing/localhost_8080/example-jesque-batch/serialBook/index?referer=');">SerialBookController</a> to see the original version, or the <a href="http://localhost:8080/example-jesque-batch/parallelBook/index" onclick="pageTracker._trackPageview('/outgoing/localhost_8080/example-jesque-batch/parallelBook/index?referer=');">ParallelBookController</a> to see the faster Redis+Jesque version.  Each will display the length of time they took to do the insert after they&#8217;re done.</p>
<p>The ParallelBookController calls <code><a href="https://bitbucket.org/tednaleid/example-jesque-batch/src/tip/grails-app/services/com/naleid/example/BookService.groovy" onclick="pageTracker._trackPageview('/outgoing/bitbucket.org/tednaleid/example-jesque-batch/src/tip/grails-app/services/com/naleid/example/BookService.groovy?referer=');">bookService</a>.parallelImportBooksInLibrary()</code>.  That method spins up a number of worker threads, iterates through the books in the <code>Library</code> and enqueues each one on a Jesque queue.  When it&#8217;s done iterating through the <code>Library</code>, it tells all the threads to end when they&#8217;re done processing all the work:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">def</span> parallelImportBooksInLibrary<span style="color: #66cc66;">&#40;</span>library<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #aaaadd; font-weight: bold;">Integer</span> workerCount <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">10</span>
        <span style="color: #aaaadd; font-weight: bold;">String</span> queueName <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;import:book&quot;</span>
        withWorkers<span style="color: #66cc66;">&#40;</span>queueName, BookConsumerJob, workerCount<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            library.<span style="color: #663399;">each</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #aaaadd; font-weight: bold;">Map</span> bookValueMap <span style="color: #66cc66;">-&gt;</span>
                <span style="color: #aaaadd; font-weight: bold;">String</span> bookValueMapJson <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span>bookValueMap <span style="color: #000000; font-weight: bold;">as</span> JSON<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
                jesqueService.<span style="color: #006600;">enqueue</span><span style="color: #66cc66;">&#40;</span>queueName, BookConsumerJob.<span style="color: #006600;">simpleName</span>, bookValueMapJson<span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #993333;">void</span> withWorkers<span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">String</span> queueName, <span style="color: #000000; font-weight: bold;">Class</span> jobClass, <span style="color: #aaaadd; font-weight: bold;">Integer</span> workerCount <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">5</span>, Closure closure<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">def</span> workers <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>
        <span style="color: #000000; font-weight: bold;">def</span> fullQueueName <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;resque:queue:$queueName&quot;</span>
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #66cc66;">&#123;</span>
            workers <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>..<span style="color: #006600;">workerCount</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #663399;">collect</span> <span style="color: #66cc66;">&#123;</span> jesqueService.<span style="color: #006600;">startWorker</span><span style="color: #66cc66;">&#40;</span>queueName, jobClass.<span style="color: #006600;">simpleName</span>, jobClass<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#125;</span>
            closure<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #808080; font-style: italic;">// wait for all the work we've generated to be pulled off the queue</span>
            <span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>redisService.<span style="color: #006600;">exists</span><span style="color: #66cc66;">&#40;</span>fullQueueName<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> sleep<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">500</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #808080; font-style: italic;">// all work is off the queue, tell each worker to kill themselves when they're finished</span>
            workers<span style="color: #66cc66;">*</span>.<span style="color: #006600;">end</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>The work queue that persist the <code>Book</code> domain objects in the database are very simple Jesque Job artefacts that are spring injected and have an active hibernate session.  They can be of any class type.  The only requirement is that they have a method named <code>perform</code> that is called and passed an item of work from the queue.</p>
<p>Here&#8217;s the example <a href="https://bitbucket.org/tednaleid/example-jesque-batch/src/tip/grails-app/jobs/com/naleid/example/BookConsumerJob.groovy" onclick="pageTracker._trackPageview('/outgoing/bitbucket.org/tednaleid/example-jesque-batch/src/tip/grails-app/jobs/com/naleid/example/BookConsumerJob.groovy?referer=');"><code>BookConsumerJob</code></a> class that persists a <code>Book</code> to the database:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #a1a100;">com.naleid.example</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #a1a100;">grails.converters.JSON</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> BookConsumerJob <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">def</span> bookService
&nbsp;
    <span style="color: #993333;">void</span> perform<span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">String</span> bookJson<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        bookService.<span style="color: #006600;">updateOrInsertBook</span><span style="color: #66cc66;">&#40;</span>JSON.<span style="color: #006600;">parse</span><span style="color: #66cc66;">&#40;</span>bookJson<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>You can see how simple the <code>BookConsumerJob</code> class is.  It also calls out to the same <a href="https://bitbucket.org/tednaleid/example-jesque-batch/src/tip/grails-app/services/com/naleid/example/BookService.groovy" onclick="pageTracker._trackPageview('/outgoing/bitbucket.org/tednaleid/example-jesque-batch/src/tip/grails-app/services/com/naleid/example/BookService.groovy?referer=');">bookService</a> method that the serial batch import calls to import a <code>Book</code>.</p>
<p>One other neat thing about using Jesque is that it adheres to the Resque conventions for what gets stored in Redis.  This means that you can <code>gem install resque-web</code> and then launch <code>resque-web</code> to get a nice monitoring platform for your Jobs and to see errors, or how much work is left in the queue.</p>
]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2011/10/13/better-grails-batch-import-performance-with-redis-and-jesque/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Using Dropbox to Share (most of) Your Home Directory Across Multiple Computers</title>
		<link>http://naleid.com/blog/2011/10/03/using-dropbox-to-share-your-home-directory-across-multiple-computers/</link>
		<comments>http://naleid.com/blog/2011/10/03/using-dropbox-to-share-your-home-directory-across-multiple-computers/#comments</comments>
		<pubDate>Tue, 04 Oct 2011 02:44:11 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
				<category><![CDATA[command line]]></category>
		<category><![CDATA[osx]]></category>
		<category><![CDATA[shortcut]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=433</guid>
		<description><![CDATA[I&#8217;m a very happy customer of Dropbox. It allows painless syncing of files across multiple computers without extra features to complicate it. The top rated answer on Quora to the question &#8220;Why is Dropbox more popular than other programs with similar functionality?&#8221; sums things up perfectly. One of my favorite uses of Dropbox is to [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m a very happy customer of <a href="http://dropbox.com" onclick="pageTracker._trackPageview('/outgoing/dropbox.com?referer=');">Dropbox</a>.   It allows painless syncing of files across multiple computers without extra features to complicate it.  The <a href="http://www.quora.com/Dropbox/Why-is-Dropbox-more-popular-than-other-programs-with-similar-functionality/answer/Michael-Wolfe" onclick="pageTracker._trackPageview('/outgoing/www.quora.com/Dropbox/Why-is-Dropbox-more-popular-than-other-programs-with-similar-functionality/answer/Michael-Wolfe?referer=');">top rated answer on Quora</a> to the question &#8220;Why is Dropbox more popular than other programs with similar functionality?&#8221; sums things up perfectly.</p>
<p>One of my favorite uses of Dropbox is to sync almost all of the non-machine specific configuration files and directories in my home directory across all my OSX computers (currently my iMac, MacBook Air, and my work laptop).  </p>
<p>Doing this lets me make a configuration change to one computer and have it almost instantly available on any other computer without any manual steps.</p>
<p>This is especially important for my zshell and Vim configurations as I&#8217;m always tweaking those, but it&#8217;s also helpful to have my Documents, Downloads and Pictures shared.</p>
<p>I have a folder in my <code>Dropbox</code> directory called <code>home</code>, I use a script called <code>link.sh</code> to automatically create symlinks in my home directory to the things I&#8217;ve got stored in Dropbox.</p>
<p><code>Dropbox/home</code> currently has these files and directories in it:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.ackrc
.dbvis
.groovy
.gvimrc
.hg
.hgignore_global
.ssh
.subversion
.vim
.viminfo
.vimrc
.zshenv
Desktop-starling.local<span style="color: #000000; font-weight: bold;">/</span>   <span style="color: #666666; font-style: italic;"># unique Desktop for my MacBook Air</span>
Desktop-kestrel.local<span style="color: #000000; font-weight: bold;">/</span>    <span style="color: #666666; font-style: italic;"># unique Desktop for my iMac</span>
Desktop-thrush.local<span style="color: #000000; font-weight: bold;">/</span>     <span style="color: #666666; font-style: italic;"># unique Desktop for my work MacBook Pro</span>
Documents<span style="color: #000000; font-weight: bold;">/</span>
Downloads<span style="color: #000000; font-weight: bold;">/</span>
Pictures<span style="color: #000000; font-weight: bold;">/</span>
bin<span style="color: #000000; font-weight: bold;">/</span></pre></div></div>

<p>My <code>Dropbox/home</code> directory also has a shell script in it called <code>link.sh</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#! /usr/bin/env bash</span>
<span style="color: #7a0874; font-weight: bold;">cd</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">dirname</span> <span style="color: #007800;">$0</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> linkFile<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    <span style="color: #007800;">LINK_TO_NAME</span>=<span style="color: #007800;">$2</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-z</span> <span style="color: #007800;">$LINK_TO_NAME</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
        <span style="color: #007800;">LINK_TO_NAME</span>=<span style="color: #007800;">$1</span>
    <span style="color: #000000; font-weight: bold;">fi</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-a</span> <span style="color: #007800;">$HOME</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$LINK_TO_NAME</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;**** Found existing <span style="color: #007800;">$LINK_TO_NAME</span>, skipping...&quot;</span>
    <span style="color: #000000; font-weight: bold;">elif</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-h</span> <span style="color: #007800;">$HOME</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$LINK_TO_NAME</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Already symlinked <span style="color: #007800;">$LINK_TO_NAME</span>, skipping...&quot;</span>
    <span style="color: #000000; font-weight: bold;">else</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Linking $1 to <span style="color: #007800;">$LINK_TO_NAME</span>&quot;</span>
        <span style="color: #c20cb9; font-weight: bold;">ln</span> <span style="color: #660033;">-s</span> <span style="color: #007800;">$PWD</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$1</span> <span style="color: #007800;">$HOME</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$LINK_TO_NAME</span> 
    <span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">for</span> F <span style="color: #000000; font-weight: bold;">in</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #660033;">-a1</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-v</span> link.sh <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-v</span> Desktop <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">egrep</span> <span style="color: #660033;">-v</span> <span style="color: #ff0000;">&quot;^..?$&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">egrep</span> <span style="color: #660033;">-v</span> <span style="color: #ff0000;">&quot;^.*un~$&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-v</span> .DS_Store<span style="color: #7a0874; font-weight: bold;">&#41;</span>; <span style="color: #000000; font-weight: bold;">do</span>
    linkFile <span style="color: #007800;">$F</span>
<span style="color: #000000; font-weight: bold;">done</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">HOSTNAME</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">hostname</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-d</span> <span style="color: #ff0000;">&quot;Desktop-<span style="color: #007800;">$HOSTNAME</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    linkFile <span style="color: #ff0000;">&quot;Desktop-<span style="color: #007800;">$HOSTNAME</span>&quot;</span> <span style="color: #ff0000;">&quot;Desktop&quot;</span>
<span style="color: #000000; font-weight: bold;">else</span> 
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Unable to find Desktop-<span style="color: #007800;">$HOSTNAME</span> to link to Desktop&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span></pre></div></div>

<p>What the script does is:</p>
<ol>
<li><code>cd</code> into the directory that the script is located in (it only symlinks files in the same directory)
<li>list out all of the files and directories in the same directory as the script</li>
<li>filter out the things we don&#8217;t want to link (like <code>.</code>, <code>..</code>, the <code>link.sh</code> script itself, etc)</li>
<li>For all of the files/directories that pass the filter, call <code>linkFile</code> to create a symlink in the current user&#8217;s home directory as long as there isn&#8217;t already a file or a symlink there</li>
<li>Then look for a file called <code>Desktop-$HOSTNAME</code> where <code>$HOSTNAME</code> is the name of the current machine and create a <code>~/Dropbox</code> symlink to it if it&#8217;s found.
</ol>
<p>It should be safe and non-destructive and only create symlinks when there isn&#8217;t anything else there with the same name.</p>
<p>I didn&#8217;t have my <code>Pictures</code>, <code>Documents</code>, and <code>Downloads</code> in my Dropbox for quite a while and was able to get away with the free 2GB plan. I recently upgraded to a paid Dropbox plan as I wanted those directories shared as well (though I exclude a couple of them from my work MacBook Pro).</p>
<p>For &#8220;special&#8221; directories like <code>Desktop</code>, <code>Pictures</code>, <code>Documents</code>, and <code>Downloads</code>, I needed to use <code>sudo rm -r [dirname]</code> to remove it before I could create the symlink (BACKUP THE DIRECTORY FIRST).   </p>
<p>I&#8217;ve been using this for over a year, and haven&#8217;t noticed any apps that care that those directories are symlinks.</p>
<p>Also?  I have used this shell script many times on my systems, and I think it&#8217;s safe, but <strong>PLEASE</strong> backup before using it, or deleting any directories.  An adult crying is not a pretty sight :).</p>
]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2011/10/03/using-dropbox-to-share-your-home-directory-across-multiple-computers/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Smart Bash/Zsh Aliases to Run Appropriate Grails Version</title>
		<link>http://naleid.com/blog/2011/09/26/smart-bash-zsh-aliases-to-run-appropriate-grails-version/</link>
		<comments>http://naleid.com/blog/2011/09/26/smart-bash-zsh-aliases-to-run-appropriate-grails-version/#comments</comments>
		<pubDate>Tue, 27 Sep 2011 03:46:39 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
				<category><![CDATA[command line]]></category>
		<category><![CDATA[grails]]></category>
		<category><![CDATA[groovy]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=423</guid>
		<description><![CDATA[I&#8217;m currently on a project that has a couple of different apps that are using different versions of grails that need to be run concurrently. Switching a symlink no longer fit the way I needed to work so I came up with a couple bash/zsh aliases that are smart about the version of grails for [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m currently on a project that has a couple of different apps that are using different versions of grails that need to be run concurrently.  <a href="http://naleid.com/blog/2009/03/08/bashzsh-aliases-to-switch-groovy-and-grails-version/">Switching a symlink</a> no longer fit the way I needed to work so I came up with a couple bash/zsh aliases that are smart about the version of grails for the current directory.</p>
<p>These aliases work for both the <code>grails</code> as well as the <code>grails-debug</code> commands (for attaching a remote debugger).</p>
<p>If there is an <code>application.properties</code> file in the current directory, we can find the current version of grails for the app.</p>
<p>If there isn&#8217;t an <code>application.properties</code> file in the current directory, the script just defaults to whatever version of grails you&#8217;ve already set up as your default through the standard <code>$GRAILS_HOME</code> environment variable.  You can use the <a href="http://naleid.com/blog/2009/03/08/bashzsh-aliases-to-switch-groovy-and-grails-version/">grails symlink switching aliases</a> that I created previously to easily move this between versions.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">grails</span>=<span style="color: #ff0000;">&quot;execute_grails_version grails&quot;</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> grails-debug=<span style="color: #ff0000;">&quot;execute_grails_version grails-debug&quot;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> execute_grails_version<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    <span style="color: #007800;">GRAILS_CMD</span>=<span style="color: #007800;">$1</span>
    <span style="color: #7a0874; font-weight: bold;">shift</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-f</span> application.properties <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
        <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">GRAILS_VERSION</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">grep</span> app.grails.version application.properties <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-E</span> <span style="color: #ff0000;">'s/.*=(.*)/\1/'</span><span style="color: #000000; font-weight: bold;">`</span>
        <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">GRAILS_HOME</span>=<span style="color: #ff0000;">&quot;/usr/local/grails-<span style="color: #007800;">$GRAILS_VERSION</span>&quot;</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;application.properties found, using <span style="color: #000099; font-weight: bold;">\$</span>GRAILS_HOME of <span style="color: #007800;">$GRAILS_HOME</span>&quot;</span>
    <span style="color: #000000; font-weight: bold;">else</span> 
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;application.properties NOT found, leaving <span style="color: #000099; font-weight: bold;">\$</span>GRAILS_HOME as <span style="color: #007800;">$GRAILS_HOME</span>&quot;</span>
    <span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #660033;">-d</span> <span style="color: #007800;">$GRAILS_HOME</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;ERROR: Unable to find <span style="color: #000099; font-weight: bold;">\$</span>GRAILS_HOME directory at <span style="color: #007800;">$GRAILS_HOME</span>&quot;</span>
        <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
    <span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$GRAILS_HOME</span><span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$GRAILS_CMD</span> <span style="color: #007800;">$*</span>
    <span style="color: #007800;">$GRAILS_HOME</span><span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$GRAILS_CMD</span> <span style="color: #007800;">$*</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<p>UPDATE: There are a few situations where aliases aren&#8217;t available (or are a pain to get available) such as when code is being executed as part of another application rather than from the command line.  To get around this, these scripts (created by a co-worker of mine, <a href="http://twitter.com/#!/sjurgemeyer" onclick="pageTracker._trackPageview('/outgoing/twitter.com/_/sjurgemeyer?referer=');">@sjurgemeyer</a>) could be put in your <code>PATH</code>, ahead of your <code>$GRAILS_HOME/bin</code> and used instead of the aliases above:</p>
<p>grails:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">grails-version grails <span style="color: #007800;">$*</span></pre></div></div>

<p>grails-debug:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">grails-version grails-debug <span style="color: #007800;">$*</span></pre></div></div>

<p>grails-version:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">GRAILS_CMD</span>=<span style="color: #007800;">$1</span>
<span style="color: #7a0874; font-weight: bold;">shift</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-f</span> application.properties <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">GRAILS_VERSION</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">grep</span> app.grails.version application.properties <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-E</span> <span style="color: #ff0000;">'s/.*=(.*)/\1/'</span><span style="color: #000000; font-weight: bold;">`</span>
    <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">GRAILS_HOME</span>=<span style="color: #ff0000;">&quot;/usr/local/grails-<span style="color: #007800;">$GRAILS_VERSION</span>&quot;</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;application.properties found, using <span style="color: #000099; font-weight: bold;">\$</span>GRAILS_HOME of <span style="color: #007800;">$GRAILS_HOME</span>&quot;</span>
<span style="color: #000000; font-weight: bold;">else</span> 
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;application.properties NOT found, leaving <span style="color: #000099; font-weight: bold;">\$</span>GRAILS_HOME as <span style="color: #007800;">$GRAILS_HOME</span>&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #660033;">-d</span> <span style="color: #007800;">$GRAILS_HOME</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;ERROR: Unable to find <span style="color: #000099; font-weight: bold;">\$</span>GRAILS_HOME directory at <span style="color: #007800;">$GRAILS_HOME</span>&quot;</span>
    <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$GRAILS_HOME</span><span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$GRAILS_CMD</span> <span style="color: #007800;">$*</span>
<span style="color: #007800;">$GRAILS_HOME</span><span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$GRAILS_CMD</span> <span style="color: #007800;">$*</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2011/09/26/smart-bash-zsh-aliases-to-run-appropriate-grails-version/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Dynamically setting Grails Log4J levels with the Console Plugin</title>
		<link>http://naleid.com/blog/2011/09/23/dynamically-setting-grails-log4j-levels-with-the-console-plugin/</link>
		<comments>http://naleid.com/blog/2011/09/23/dynamically-setting-grails-log4j-levels-with-the-console-plugin/#comments</comments>
		<pubDate>Fri, 23 Sep 2011 18:41:59 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
				<category><![CDATA[grails]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[metaprogramming]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=414</guid>
		<description><![CDATA[If you&#8217;ve got Burt Beckwith&#8217;s great Grails Console Plugin installed, it&#8217;s easy to tweak the logging levels dynamically in your grails application. The quick and dirty way to switch your logging level dynamically, if you know the name of the logger is just to do this in your console window: import org.apache.log4j.* Logger.getLogger&#40;&#34;org.springframework&#34;&#41;.level = Level.DEBUG [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve got Burt Beckwith&#8217;s great <a href="http://grails.org/plugin/console" onclick="pageTracker._trackPageview('/outgoing/grails.org/plugin/console?referer=');">Grails Console Plugin</a> installed, it&#8217;s easy to tweak the logging levels dynamically in your grails application.</p>
<p>The quick and dirty way to switch your logging level dynamically, if you know the name of the logger is just to do this in your console window:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #a1a100;">org.apache.log4j.*</span>
Logger.<span style="color: #006600;">getLogger</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;org.springframework&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">level</span> <span style="color: #66cc66;">=</span> Level.<span style="color: #006600;">DEBUG</span></pre></div></div>

<p>Sometimes, a few helper methods can help you see what the current config is (especially if you&#8217;ve changed some things), as well as figure out what the right loggers are to tweak.  This sample script can be used in a grails console to make it easy to view and change the logging level to whatever you want, just cut and paste it into your application&#8217;s console window (in dev it defaults to: http://localhost:8080/yourAppName/console):</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #a1a100;">org.apache.log4j.Logger</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #a1a100;">org.apache.log4j.Level</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #a1a100;">static</span> org.<span style="color: #006600;">apache</span>.<span style="color: #006600;">log4j</span>.<span style="color: #006600;">Level</span>.<span style="color: #66cc66;">*</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">def</span> getRootLogger<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> Logger.<span style="color: #006600;">rootLogger</span> <span style="color: #66cc66;">&#125;</span>
<span style="color: #000000; font-weight: bold;">def</span> getAllLoggers<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> rootLogger.<span style="color: #006600;">loggerRepository</span>.<span style="color: #006600;">currentLoggers</span>.<span style="color: #663399;">toList</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #663399;">sort</span> <span style="color: #66cc66;">&#123;</span> it.<span style="color: #006600;">name</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#125;</span>
<span style="color: #000000; font-weight: bold;">def</span> getActiveLoggers<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> allLoggers.<span style="color: #663399;">findAll</span> <span style="color: #66cc66;">&#123;</span> it.<span style="color: #006600;">level</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#125;</span>
<span style="color: #000000; font-weight: bold;">def</span> getLogger<span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">String</span> logName<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> rootLogger.<span style="color: #006600;">getLogger</span><span style="color: #66cc66;">&#40;</span>logName<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#125;</span>
<span style="color: #000000; font-weight: bold;">def</span> setLevel<span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">String</span> logName, Level level<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> rootLogger.<span style="color: #006600;">getLogger</span><span style="color: #66cc66;">&#40;</span>logName<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">level</span> <span style="color: #66cc66;">=</span> level <span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">def</span> printLogger<span style="color: #66cc66;">&#40;</span>logger<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #993399;">println</span> <span style="color: #ff0000;">&quot;${logger.name} -&gt; ${logger.level}&quot;</span> <span style="color: #66cc66;">&#125;</span>
<span style="color: #000000; font-weight: bold;">def</span> printAllLoggers<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> allLoggers.<span style="color: #663399;">each</span> <span style="color: #66cc66;">&#123;</span> printLogger<span style="color: #66cc66;">&#40;</span>it<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#125;</span>
<span style="color: #000000; font-weight: bold;">def</span> printActiveLoggers<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> activeLoggers.<span style="color: #663399;">each</span> <span style="color: #66cc66;">&#123;</span> printLogger<span style="color: #66cc66;">&#40;</span>it<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>This makes it easy to see what logs are currently active (those with a log level set):</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">printActiveLoggers<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>prints something like:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">grails.<span style="color: #006600;">app</span>.<span style="color: #006600;">filters</span>.<span style="color: #006600;">LoggingFilters</span> <span style="color: #66cc66;">-&gt;</span> DEBUG
grails.<span style="color: #006600;">app</span>.<span style="color: #006600;">filters</span>.<span style="color: #006600;">SecurityFilters</span> <span style="color: #66cc66;">-&gt;</span> DEBUG
grails.<span style="color: #006600;">app</span>.<span style="color: #006600;">service</span>.<span style="color: #006600;">grails</span>.<span style="color: #006600;">plugin</span>.<span style="color: #006600;">redis</span>.<span style="color: #006600;">RedisService</span> <span style="color: #66cc66;">-&gt;</span> WARN
grails.<span style="color: #006600;">app</span>.<span style="color: #006600;">task</span> <span style="color: #66cc66;">-&gt;</span> DEBUG
org.<span style="color: #006600;">apache</span>.<span style="color: #006600;">cxf</span> <span style="color: #66cc66;">-&gt;</span> DEBUG
...</pre></div></div>

<p>You can also list all loggers, which also adds in those loggers who&#8217;s log level is currently `null`:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">printAllLoggers<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>prints:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">grails.<span style="color: #006600;">app</span> <span style="color: #66cc66;">-&gt;</span> DEBUG
grails.<span style="color: #006600;">app</span>.<span style="color: #006600;">bootstrap</span>.<span style="color: #006600;">BootStrap</span> <span style="color: #66cc66;">-&gt;</span> <span style="color: #000000; font-weight: bold;">null</span>
grails.<span style="color: #006600;">app</span>.<span style="color: #006600;">bootstrap</span>.<span style="color: #006600;">QuartzBootStrap</span> <span style="color: #66cc66;">-&gt;</span> <span style="color: #000000; font-weight: bold;">null</span>
grails.<span style="color: #006600;">app</span>.<span style="color: #006600;">codec</span>.<span style="color: #006600;">org</span>.<span style="color: #006600;">codehaus</span>.<span style="color: #006600;">groovy</span>.<span style="color: #006600;">grails</span>.<span style="color: #006600;">plugins</span>.<span style="color: #006600;">codecs</span>.<span style="color: #006600;">Base64Codec</span> <span style="color: #66cc66;">-&gt;</span> <span style="color: #000000; font-weight: bold;">null</span>
grails.<span style="color: #006600;">app</span>.<span style="color: #006600;">codec</span>.<span style="color: #006600;">org</span>.<span style="color: #006600;">codehaus</span>.<span style="color: #006600;">groovy</span>.<span style="color: #006600;">grails</span>.<span style="color: #006600;">plugins</span>.<span style="color: #006600;">codecs</span>.<span style="color: #006600;">HTMLCodec</span> <span style="color: #66cc66;">-&gt;</span> <span style="color: #000000; font-weight: bold;">null</span>
grails.<span style="color: #006600;">app</span>.<span style="color: #006600;">codec</span>.<span style="color: #006600;">org</span>.<span style="color: #006600;">codehaus</span>.<span style="color: #006600;">groovy</span>.<span style="color: #006600;">grails</span>.<span style="color: #006600;">plugins</span>.<span style="color: #006600;">codecs</span>.<span style="color: #006600;">HexCodec</span> <span style="color: #66cc66;">-&gt;</span> <span style="color: #000000; font-weight: bold;">null</span>
...</pre></div></div>

<p>You can also dynamically grab/create a logger and set it&#8217;s logging level to something more or less verbose than it&#8217;s current value:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">def</span> logger <span style="color: #66cc66;">=</span> getLogger<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;grails.app.service.grails.plugin.redis.RedisService&quot;</span><span style="color: #66cc66;">&#41;</span>
printLogger<span style="color: #66cc66;">&#40;</span>logger<span style="color: #66cc66;">&#41;</span>   <span style="color: #808080; font-style: italic;">// initially WARN</span>
logger.<span style="color: #006600;">level</span> <span style="color: #66cc66;">=</span> INFO  
printLogger<span style="color: #66cc66;">&#40;</span>logger<span style="color: #66cc66;">&#41;</span>   <span style="color: #808080; font-style: italic;">// prints INFO</span></pre></div></div>

<p>prints:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">grails.<span style="color: #006600;">app</span>.<span style="color: #006600;">service</span>.<span style="color: #006600;">grails</span>.<span style="color: #006600;">plugin</span>.<span style="color: #006600;">redis</span>.<span style="color: #006600;">RedisService</span> <span style="color: #66cc66;">-&gt;</span> WARN
grails.<span style="color: #006600;">app</span>.<span style="color: #006600;">service</span>.<span style="color: #006600;">grails</span>.<span style="color: #006600;">plugin</span>.<span style="color: #006600;">redis</span>.<span style="color: #006600;">RedisService</span> <span style="color: #66cc66;">-&gt;</span> INFO</pre></div></div>

<p>It&#8217;d be easy to turn this into a simple gsp/controller that accepts changes and can list things out.  There are also a number of other plugins out there that let you view/change logging levels (including another one of Burt&#8217;s plugins, <a href="http://grails.org/plugin/app-info" onclick="pageTracker._trackPageview('/outgoing/grails.org/plugin/app-info?referer=');">app info</a>), but if you don&#8217;t have those installed, this is a quick way to see what&#8217;s going on with your application.</p>
]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2011/09/23/dynamically-setting-grails-log4j-levels-with-the-console-plugin/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

