<?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>Nicholas Hagen</title>
	<atom:link href="http://www.znetdevelopment.com/blogs/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.znetdevelopment.com/blogs</link>
	<description>Tidbits and blogs from Nicholas Hagen related to software, web design, and usability</description>
	<lastBuildDate>Fri, 17 May 2013 14:29:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Faster Synchronization Through Hash Maps</title>
		<link>http://www.znetdevelopment.com/blogs/2013/05/17/faster-synchronization-through-hash-maps/</link>
		<comments>http://www.znetdevelopment.com/blogs/2013/05/17/faster-synchronization-through-hash-maps/#comments</comments>
		<pubDate>Fri, 17 May 2013 14:29:15 +0000</pubDate>
		<dc:creator>Nicholas Hagen</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[ArrayList]]></category>
		<category><![CDATA[HashMap]]></category>
		<category><![CDATA[List]]></category>
		<category><![CDATA[Map]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[Speed]]></category>
		<category><![CDATA[Synchronization]]></category>
		<category><![CDATA[Synchronized]]></category>

		<guid isPermaLink="false">http://www.znetdevelopment.com/blogs/?p=714</guid>
		<description><![CDATA[As part of a load testing project, I needed to collect and store data as fast as possible in a multi-threaded environment. This meant synchronizing on collections to avoid collisions. Synchronization is always a bad idea in any performance critical application. In my simple testing, adding to a synchronized ArrayList can be almost 4x slower [...]]]></description>
				<content:encoded><![CDATA[<p>As part of a load testing project, I needed to collect and store data as fast as possible in a multi-threaded environment.  This meant synchronizing on collections to avoid collisions.  Synchronization is always a bad idea in any performance critical application.  In my simple testing, adding to a synchronized ArrayList can be almost 4x slower than a non-synchronized ArrayList.  Then, I had an interesting idea of what would be faster:  a synchronized list or a hash map keyed off the local thread id?  In my testing, the map was actually faster as it required no synchronization&#8230;almost 2x faster.  It is still slower than non-synchronized list, understandably so, but it at least gets to within 2x worse.  The hash map example works since upon retrieval, you are guaranteed that the list being added to cannot collide except with itself as the list is tied to a specific thread.  This is similar to a ThreadLocal, except ThreadLocal is even slower performance. The downfall of this method is that it assumes you are primarily concerned with &#8216;add&#8217; and that reads only occur at the end at which point you have to merge.  This also assumes that order does not matter.  In my load testing project, all of that holds true.  All I care about is storing metrics as fast as possible without colliding.</p>
<p><script src="https://gist.github.com/nicholashagen/5599362.js"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.znetdevelopment.com/blogs/2013/05/17/faster-synchronization-through-hash-maps/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript as the Future? My Personal Opinions</title>
		<link>http://www.znetdevelopment.com/blogs/2013/03/04/javascript-as-the-future-my-personal-opinions/</link>
		<comments>http://www.znetdevelopment.com/blogs/2013/03/04/javascript-as-the-future-my-personal-opinions/#comments</comments>
		<pubDate>Mon, 04 Mar 2013 14:04:43 +0000</pubDate>
		<dc:creator>Nicholas Hagen</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[browsers]]></category>
		<category><![CDATA[Concurrency]]></category>
		<category><![CDATA[Node]]></category>
		<category><![CDATA[Node.js]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[Scalability]]></category>
		<category><![CDATA[Threads]]></category>
		<category><![CDATA[V8]]></category>

		<guid isPermaLink="false">http://www.znetdevelopment.com/blogs/?p=690</guid>
		<description><![CDATA[I have long had mixed feelings on the future of JavaScript. Many people believe it is the future. Others believe it is the new VM of development (similar to Java). Others believe it as the sole language across server and client-side development (one language to rule them all). So, where do I stand? I stand [...]]]></description>
				<content:encoded><![CDATA[<p>I have long had mixed feelings on the future of JavaScript.  Many people believe it is the future.  Others believe it is the new VM of development (similar to Java).  Others believe it as the sole language across server and client-side development (one language to rule them all).  So, where do I stand?  I stand somewhere between I guess.  I believe the concept of JavaScript as a VM supporting new languages like CoffeeScript, Dart, TypeScript, etc.  In fact, I applaud those efforts.  JavaScript, as a language, moves at such a snail pace due to lack of quick browser integration.  As a result, the language, syntactally, is out-dated, whereas the newer languages such as CoffeeScript, Dart, and TypeScript compile into JavaScript but provide several syntactic sugar syntaxes. My biggest issue is that these languages still have to compile into JavaScript and be at the mercy of server side providers (V8/Node.js) and browser vendors.  I&#8217;m also not a believer in JavaScript as a scaling language, performance and development-wise, at least compared to other languages (Java, Ruby, Python, etc).  JavaScript is definitely a great language, but it has several pitfalls that I believe the &#8220;cool-factor&#8221; simply ignores in order to tout its resurgence.  JavaScript definitely has a place, but often times there are better choices that developers fail to utilize because they want to use JavaScript as the cool kid on the block.  The right tool needs to be used for the right job to be successful.</p>
<p><span id="more-690"></span></p>
<p>I&#8217;ve been using JavaScript for years in various varieties.  Every time, I end up having too many issues or end up in complexity to believe JavaScript is the answer.  In contrast to many JavaScript advocates, I&#8217;m primarily a JVM-based developer and have seen the power of modern day languages on the JVM, notably Groovy.  People often blame my JavaScript ignorance on lack of knowledge and understanding.  There may be some truth there (I&#8217;m a backend engineer on the JVM, so JavaScript is not my day job), but it&#8217;s not the entire truth.  JavaScript has pitfalls regardless if people want to admit it or not.  Personally I think that die hard JavaScript developers just &#8220;deal&#8221; with it, ignore it, or have not seen the advancements of other modern day languages to realize the pitfalls.</p>
<p>My biggest issue with JavaScript as of late is the lack of threading.  Many developers, including myself, develop libraries on both Node.js for the server and the browser as the client.  This means you develop for the lesser of the two: browsers.  This means you are bound by a single thread in the browser.  Why is that an issue you say?  Isn&#8217;t event driven programming far superior?  Aren&#8217;t threading and multi-threading concurrent applications problematic and complex?  The answer to all those questions is yes, as a developer (key part is &#8220;as a developer&#8221;).  The browser and Node.js are not developers; they are libraries and systems that abstract away the complexity of development from the developer.  This is especially true in request/response systems such as the web. Think about Java servlets or Apache&#8217;s http server or even Node.js/Vert.x clustering.  As a developer you design code in a single thread of work, but the underlying system uses multiple threads (or processes in the case of Node.js).  The system enforces, rightfully so, that single thread of work.  Behind the scenes they concurrently process hundreds of requests.  That means, as a developer, you have simplicity in your code and any pauses (ie: remote calls, database invocations, etc) do not block the world.  In JavaScript, this is very much not true.  If you block anything, you block everything, including the browser application, leading to poor application performance.  The answer in JavaScript is event driven programming or callbacks.  Fundamentally, there is nothing wrong with that.  They are often times very strong design patterns for performance and simplicity in design.  In fact, almost every modern day language (even Java), use them where they make sense.  For example, many of the utility classes in Groovy heavily use the concept of closures or listeners.  The problem is that JavaScript enforces it so heavily (because any minor pause is a stop the world pause), that you quickly end up in callback hell.  I realize that there are improved ways of dealing with that such as deferrals and promises.  Realistically, though, those are not solutions.  Instead, they are bandaids to a broken problem in the first place: lack of threading. Callback hell in JavaScript only exists because every pause blocks everything.  If that were not true and the browser still processed while the JavaScript application thread was paused, you would make far less callbacks.  The truth is that whether you end up in callback hell or utilize some library such as promises, the result is always the same: far more complexity than is needed.  Remember the notion of KISS?  Any real world JavaScript application on the browser fails to meet that criteria due to having to ensure every operation is non-blocking.</p>
<p>I heavily realized this recently.  I needed to write a generic iteration method that could accept a callback for writing data:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">Utils.<span style="color: #660066;">forEach</span><span style="color: #009900;">&#40;</span>array<span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>item<span style="color: #339933;">,</span> writer<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">/* write stuff */</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>If you think about this, the solution is fairly straightforward:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">Utils.<span style="color: #660066;">forEach</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>array<span style="color: #339933;">,</span> callback<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">var</span> i<span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> array.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        callback<span style="color: #009900;">&#40;</span>array<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> Context.<span style="color: #660066;">writer</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>What&#8217;s the problem, you ask?  Well, what if I told you that the callback was allowed to do anything, including make remote invocations:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> callback <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>item<span style="color: #339933;">,</span> writer<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    $.<span style="color: #000066; font-weight: bold;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/api/'</span> <span style="color: #339933;">+</span> item.<span style="color: #660066;">id</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>json<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        writer.<span style="color: #660066;">write</span><span style="color: #009900;">&#40;</span>json.<span style="color: #660066;">result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>If you use this callback to the <code>Utils.forEach</code> method, it will fail to work as expected because the AJAX invocations will happen at some point in the future.  The solution to this and keeping to simplicity, is to use a synchronous AJAX request.  However, that means blocking everything, which is obviously not an option.  That is where JavaScript fails.  It requires you to take simplicity and over-complicate it.  This is not just for AJAX, it goes for anything that could block, including I/O.  For example, to fix our 6-line <code>Utils.forEach</code> implementation to support asynchronous callbacks leads to the following hard to read code:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">Utils.<span style="color: #660066;">forEach</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>array<span style="color: #339933;">,</span> callback<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">var</span> i<span style="color: #339933;">,</span> iterator<span style="color: #339933;">;</span>
    i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
    iterator <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">&lt;</span> array.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            i<span style="color: #339933;">++;</span>
            callback<span style="color: #009900;">&#40;</span>array<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> Context.<span style="color: #660066;">writer</span><span style="color: #339933;">,</span> iterator<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    iterator<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: #339933;">;</span></pre></td></tr></table></div>

<p>That&#8217;s now an 11-line hard to follow function doing weird workarounds like creating a temporary callback iterator and recursively invoking itself.  If you did not catch it yet, this is not complete.  We have to then modify our user callback to accept that third callback parameter and make sure it invokes the iterator callback.  This requires a dependence on both sides, which is another no-no in computer science (loose coupling anyone?)</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> callback <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>item<span style="color: #339933;">,</span> writer<span style="color: #339933;">,</span> iterator<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    $.<span style="color: #000066; font-weight: bold;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/api/'</span> <span style="color: #339933;">+</span> item.<span style="color: #660066;">id</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>json<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        writer.<span style="color: #660066;">write</span><span style="color: #009900;">&#40;</span>json.<span style="color: #660066;">result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        iterator<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;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>In any other language that has multi-threads and application pauses do not block everything, the original method works just fine.  This is very unfortunate in JavaScript.  Although, I have plenty of other issues for the language, but that&#8217;s for another time and many of those are being solved by languages such as CoffeeScript, Dart and TypeScript or even by the upcoming EcmaScript 6 standard.  Unfortunately, the latter won&#8217;t happen in browsers for a few years out.  The threading issue will continue to be a hinderance for application simplicity and scalability (development-wise). </p>
<p>Will JavaScript be the next VM or the future of development?  Time will certainly tell.  Until JavaScript allows some form of true behind the scenes threading where the application does not entirely block, I do not buy into it even though I&#8217;m sure I&#8217;ll continue working in it.  By the way, yes I know web workers exist, but again that&#8217;s another bandaid to the original problem and web workers have too many other issues and inherent complexity. Personally, I know other systems such as Groovy and Grails offer so many more advantages and I choose the language that offers the best advantages for a given use case.  </p>
<p>As a last aside, one of the things I love about Grails (or Spring on Java) is AOP (aspect oriented programming).  AOP allows loose coupling in a high degree by moving cross cutting concerns (logging, debugging, performance metrics, etc) away from the application.  This is easy in Grails because you wrap the method in a proxy and detect the start and end of the function.  With JavaScript, the end of the function is often times not the end of the function, because it has to invoke a callback.  Take the AJAX example callback above.  If I wanted to inject timing statements around that, it would be very fast because the AJAX call happens in the background.  It would have to magically know that a callback was invoked and wrap that as well.  That is near impossible which is why AOP libraries do not effectively work in JavaScript.  This is yet another reason why I choose Grails over JavaScript.  As applications become more complex, these options do not exist in JavaScript, because of the nature of the language.  Hopefully this changes over time so JavaScript can become a better framework for real world applications while still maintaining their simplicity.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.znetdevelopment.com/blogs/2013/03/04/javascript-as-the-future-my-personal-opinions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Budget Analyzer Application: Client Side via jQuery Mobile</title>
		<link>http://www.znetdevelopment.com/blogs/2013/01/24/budget-analyzer-application-client-side-via-jquery-mobile/</link>
		<comments>http://www.znetdevelopment.com/blogs/2013/01/24/budget-analyzer-application-client-side-via-jquery-mobile/#comments</comments>
		<pubDate>Fri, 25 Jan 2013 03:25:58 +0000</pubDate>
		<dc:creator>Nicholas Hagen</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[Animation]]></category>
		<category><![CDATA[Budget Analyzer]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[Data Attributes]]></category>
		<category><![CDATA[Grails]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[jQuery Mobile]]></category>
		<category><![CDATA[Page Transitions]]></category>
		<category><![CDATA[Responsive Design]]></category>

		<guid isPermaLink="false">http://www.znetdevelopment.com/blogs/?p=681</guid>
		<description><![CDATA[As mentioned in my previous post, this article series is around my Budget Analyzer application that I built using Grails and jQuery Mobile frameworks. I hope to present this as a more real world example of using Grails along with using jQuery Mobile for the client side responsible design that can scale to any sized [...]]]></description>
				<content:encoded><![CDATA[<p>As mentioned in my <a href="http://www.znetdevelopment.com/blogs/2013/01/23/budget-analyzer-application-server-side-via-grails/" title="Budget Analyzer Application: Server Side via Grails">previous post</a>, this article series is around my Budget Analyzer application that I built using Grails and jQuery Mobile frameworks. I hope to present this as a more real world example of using Grails along with using jQuery Mobile for the client side responsible design that can scale to any sized client (phone, tablet, desktop, etc). The jQuery Mobile framework is an excellent library that simplifies that approach dramatically.<br />
<span id="more-681"></span><br />
The application I am presenting here is a Budget Analyzer application. The problem with most of the financial applications and budget aware applications such as Mint.com is that they do not provide a forward looking statement. One of the things I like to know when dealing with accounts is how much money would be in my account in two weeks based on upcoming bills and budgets. This is very handy when making a payment and knowing whether you have a certain amount in your account so you do not hit any thresholds. Although forward looking balances are mere estimates, it gives you an easy way to see what bills are approaching and their approximate impact to the bottom line.</p>
<p>This particular post will focus on the client side rendering via <a href="http://jquerymobile.com/">jQuery Mobile</a>.  jQuery Mobile is a great framework that allows the application to focus on semantics and clean HTML design while the actual rendering is performed client side via CSS and JS.  Further, the framework supports responsive design that automatically scales to the desired form factor including phones, tablets, and desktops.  All this combined allows the application developer to define the semantic HTML that scales to all platforms rather than having to create separate interfaces and designs for each form factor.</p>
<p>To utilize jQuery Mobile you need to include the relevant CSS and JS files which also includes the jQuery core library.  The simplest way is using the jQuery CDN via:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="html" style="font-family:monospace;">&lt;link rel=&quot;stylesheet&quot; href=&quot;http://code.jquery.com/mobile/1.3.0-beta.1/jquery.mobile-1.3.0-beta.1.min.css&quot; /&gt;
&lt;script src=&quot;http://code.jquery.com/jquery-1.8.3.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;http://code.jquery.com/mobile/1.3.0-beta.1/jquery.mobile-1.3.0-beta.1.min.js&quot;&gt;&lt;/script&gt;</pre></td></tr></table></div>

<p>jQuery Mobile uses the HTML5 data attributes to identify the options for various components. The main component in jQuery Mobile is the page.  A page identifies a particular unique page or screen in the application.  A single HTML page may include multiple pages for optimizations, but jQuery Mobile will only show a single page at a time.  Per my <a href="https://github.com/nicholashagen/budget-analyzer/blob/master/grails-app/views/layouts/main.gsp">main layout</a>, an examlpe page layout is (notice the data attributes):</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="html" style="font-family:monospace;">&lt;div id=&quot;page&quot; data-role=&quot;page&quot;&gt;
    &lt;div data-role=&quot;header&quot; data-id=&quot;header&quot; data-position=&quot;fixed&quot;&gt;
        &lt;h1&gt;Title&lt;/h1&gt;
    &lt;/div&gt;
    &lt;div data-role=&quot;content&quot;&gt;
        page content goes here
    &lt;/div&gt;
    &lt;div data-role=&quot;footer&quot; data-id=&quot;nav&quot; data-position=&quot;fixed&quot;&gt;
        &lt;div data-role=&quot;navbar&quot;&gt;
            &lt;ul&gt;
                &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Overview&lt;/a&gt;&lt;/li&gt;
                &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Navigation&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;</pre></td></tr></table></div>

<p>The page section contains three main div sections: the header, the content, and the footer.  jQuery Mobile supports fixed positioning across form factors for headers and footers.  The fixed positioning allows the elements to remain in their respective locations regardless of user scrolling or page transitions.  This symbolizes a typical native mobile application (ie: iOS).  The footer may also include a navigation bar that also resembles a navigation bar in a native iOS application, especially if icons are included (they are not in my case).  The biggest takeaway from this simple example is that the HTML is simple and straightforward, yet it works in almost every form factor imaginable.</p>
<p>The next great part about jQuery Mobile is its automatic navigation and linking support.  By default, all links in your application are standard links that would work regardless of JavaScript being enabled.  This also means they are easily traversal by search engines for SEO purposes.  jQuery Mobile automatically translates links into AJAX links and does automatic page transitions.  It also manages the history through HTML5 history linking as well as through old school methods such as hashtag references.  The server-side developer purely defines standard HTTP request modeling of pages while jQuery Mobile automatically downloads content through AJAX behind the scenes.  The best part of the navigation is the automatic transitions.  Using data attributes within links, you can customize the transition without any JavaScript code such as slideup, slidedown, flip, etc.  For example, the <a href="https://github.com/nicholashagen/budget-analyzer/blob/master/grails-app/views/transaction/list.gsp">transaction page</a> in my application defines a button that loads the create transaction view through a slideup transition via <code>data-transition="slideup"</code>.  This makes the form look like it comes from the bottom.  Transitions, especially in client-side applications, are great at informing the user an action is taking place and a new view is being displayed.</p>
<p>Next up is button support.  jQuery Mobile supports automatic button styling for standard links as well as custom form inputs and buttons.  An application selects the proper semantics (links for traversing, buttons for form handling, etc), but they all appear the same.  Buttons may also include icons in addition to text.  For example, within Grails I define a custom link using Grails linking support.  </p>
<p>The <a href="https://github.com/nicholashagen/budget-analyzer/blob/master/grails-app/views/transaction/list.gsp">first example</a> right aligns a button in a header section.  Note that the data role is not required in headers, but the CSS class is needed to properly align the button on the right or left.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="html" style="font-family:monospace;">&lt;g:link action=&quot;create&quot; params=&quot;[type:type]&quot; class=&quot;ui-btn-right&quot;
        data-icon=&quot;plus&quot; data-transition=&quot;slideup&quot;&gt;
    Add
&lt;/g:link&gt;</pre></td></tr></table></div>

<p>The <a href="https://github.com/nicholashagen/budget-analyzer/blob/master/grails-app/views/transaction/_form.gsp">second example</a> is used within a form.  One button is a standard link while the other is a form submit button.  jQuery Mobile properly handles both links and frames through AJAX navigation.  </p>
<p>Note that the <code>data-role="button"</code> causes the to display as an actual button. Also, the <code>data-theme</code> may be used to customize the styling of the button.  Finally, when using transitions, you can reverse the animation with the <code>data-direction="reverse"</code> attribute.  In this application, the create form slides up to display.  Upon submission or cancel, the form should slide back down in reverse.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="html" style="font-family:monospace;">&lt;g:link action=&quot;list&quot; params=&quot;[type:type]&quot; 
           data-role=&quot;button&quot; data-icon=&quot;delete&quot; data-theme=&quot;a&quot;
           data-transition=&quot;slideup&quot; data-direction=&quot;reverse&quot;&gt;
    Cancel
&lt;/g:link&gt;
&lt;button type=&quot;submit&quot; data-theme=&quot;b&quot; data-icon=&quot;check&quot;
           data-transition=&quot;slideup&quot; data-direction=&quot;reverse&quot;&gt;
    Submit
&lt;/button&gt;</pre></td></tr></table></div>

<p>The whole form aspect is also fully supported by jQuery Mobile.  Certain form elements are custom styled and laid out to support various form factors.  The basic form element is defined as the following.  The key is using the fieldcontain role along with a label and associated input field.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="html" style="font-family:monospace;">&lt;div id=&quot;name-container&quot; data-role=&quot;fieldcontain&quot;&gt;
    &lt;label for=&quot;name&quot;&gt;Name:&lt;/label&gt;
    &lt;g:textField name=&quot;name&quot; value=&quot;${fieldValue(bean: transaction, field: 'name')}&quot; /&gt;
&lt;/div&gt;</pre></td></tr></table></div>

<p>The final thing to note in the application is the custom listview support.  jQuery Mobile, inline with native client applications, supports a variety of listviews to show lists and tables of data.  The listview support is amazing and provides all sorts of options through data attributes.  It also allows customization which is what I used to gain the proper look and feel.  The end result is a great looking interface without having to design or create crazy CSS rules.  The <a href="https://github.com/nicholashagen/budget-analyzer/blob/master/grails-app/views/overview/list.gsp">HTML</a> is purely semantic as list items.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="html" style="font-family:monospace;">&lt;ul id=&quot;overview-list&quot; data-role=&quot;listview&quot; data-dividertheme=&quot;b&quot;&gt;
    &lt;li data-theme=&quot;e&quot;&gt;
        &lt;h4 class=&quot;listview-content&quot;&gt;Today's Balance&lt;/h4&gt;
        &lt;h4 class=&quot;listview-note ui-li-aside&quot;&gt;
	       $100.00
	&lt;/h4&gt;
    &lt;/li&gt;
    &lt;li data-role=&quot;list-divider&quot;&gt;January 2013&lt;/li&gt;
    &lt;li&gt;
        &lt;a href=&quot;#&quot; class=&quot;ui-btn-right&quot; data-transition=&quot;slide&quot;&gt;
            &lt;h4 class=&quot;listview-info bill&quot;&gt;25&lt;/h4&gt;
		  &lt;h4 class=&quot;listview-content&quot;&gt;My Bill&lt;/h4&gt;
		  &lt;h4 class=&quot;listview-note ui-li-aside amount&quot;&gt;$23.00&lt;/h4&gt;
	   &lt;/a&gt;
    &lt;/li&gt;
&lt;/ul&gt;</pre></td></tr></table></div>

<p>The interesting portions are the dividerthemes that provide different styling for the dividing sections.  I use one style for the main divider at the top for the overview balance and then a different style for each month dividing the items.  The actual content is controlled by the <code>h4</code> references and associated style classes that align the content to the left, right, or as general textual content.  Finally, I use a custom class (ie: bill, budget, etc) to customize the left-aligned date to a particular color to identify each transaction type.</p>
<p>There is so much more to jQuery Mobile and the documentation is pretty good.  For example, you can use jQuery syntax to add further hooks into the framework such as handling a long tap on a given list view item:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// process once the page has actually been initialized</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#page'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">live</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'pageinit'</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// process long holds</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#overview-list li'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'taphold'</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">/* do something */</span> <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>If you have yet to try jQuery Mobile I strongly encourage it.  It fits perfectly with Grails as well.  If you use Grails, you may also want to consider using the Grails resource plugin with the Grails jQuery Mobile plugin.  The two automate the inclusion of the necessary dependencies and libraries.  However, the jQuery Mobile plugin in Grails tends to be behind a few releases in the fast evolving framework, which is why I opted to just use the CDN directly.</p>
<p>Read the previous article on <a href="http://www.znetdevelopment.com/blogs/2013/01/23/budget-analyzer-application-server-side-via-grails/" title="Budget Analyzer Application: Server Side via Grails">Server Side via Grails</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.znetdevelopment.com/blogs/2013/01/24/budget-analyzer-application-client-side-via-jquery-mobile/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Budget Analyzer Application: Server Side via Grails</title>
		<link>http://www.znetdevelopment.com/blogs/2013/01/23/budget-analyzer-application-server-side-via-grails/</link>
		<comments>http://www.znetdevelopment.com/blogs/2013/01/23/budget-analyzer-application-server-side-via-grails/#comments</comments>
		<pubDate>Thu, 24 Jan 2013 04:28:47 +0000</pubDate>
		<dc:creator>Nicholas Hagen</dc:creator>
				<category><![CDATA[Grails]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Budget]]></category>
		<category><![CDATA[cloud]]></category>
		<category><![CDATA[Cloud Foundry]]></category>
		<category><![CDATA[Convention over Configuration]]></category>
		<category><![CDATA[GORM]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[GVM]]></category>
		<category><![CDATA[H2]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Jobs]]></category>
		<category><![CDATA[Layouts]]></category>
		<category><![CDATA[Mail]]></category>
		<category><![CDATA[Plugin]]></category>
		<category><![CDATA[Quartz]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[URL Mappings]]></category>

		<guid isPermaLink="false">http://www.znetdevelopment.com/blogs/?p=679</guid>
		<description><![CDATA[In this post, I will present one of my newest applications that I built using Grails and jQuery Mobile frameworks. I hope to present this as a more real world example of using Grails along with using jQuery Mobile for the client side responsible design that can scale to any sized client (phone, tablet, desktop, [...]]]></description>
				<content:encoded><![CDATA[<p>In this post, I will present one of my newest applications that I built using Grails and jQuery Mobile frameworks.  I hope to present this as a more real world example of using Grails along with using jQuery Mobile for the client side responsible design that can scale to any sized client (phone, tablet, desktop, etc).  The jQuery Mobile framework is an excellent library that simplifies that approach dramatically.<br />
<span id="more-679"></span></p>
<p>The application I am presenting here is a Budget Analyzer application.  The problem with most of the financial applications and budget aware applications such as Mint.com is that they do not provide a forward looking statement.  One of the things I like to know when dealing with accounts is how much money would be in my account in two weeks based on upcoming bills and budgets.  This is very handy when making a payment and knowing whether you have a certain amount in your account so you do not hit any thresholds.  Although forward looking balances are mere estimates, it gives you an easy way to see what bills are approaching and their approximate impact to the bottom line.</p>
<p>The first part of this application is the server-side stack composing of Grails.  I used the latest Grails 2.2 build for this application.  If you do not have Grails installed, the easiest way to install and manage the various instances of Grails on your system is the excellent <a href="http://gvmtool.net/">GVM (Groovy enVironment Manager) Tool</a>.  I would show how to install it, but it is so simple that just visiting the site and following the couple steps is all you need.</p>
<p>The backend of the application is built using standard Domain objects in Grails.  Domain objects in Grails are so simple its ridiculous.  If you have ever built a persistence layer in Java using something like JPA or Hibernate, you know how much crud there is in defining properties, annotations (or XML files), getters/setters, proper equals/hashCode implementations, etc, not to mention trying to recall the countless annotations you need for mapping the tables, columns, joins, cascading, caching, etc.  Grails provides suitable defaults in most cases following its convention over configuration principle.  Combined with Groovy&#8217;s automatic getter/setter provisioning, you can create a simple Transaction class in a few lines.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Transaction <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">String</span> name
    <span style="color: #000066; font-weight: bold;">int</span> recurrence
    <span style="color: #000066; font-weight: bold;">double</span> amount
    <span style="color: #003399;">Date</span> startDate
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>That class is a valid Grails domain object that you could easily invoke <code>Transaction.list()</code> to fetch all saved transactions.  As I said, ridiculously easy.</p>
<p>Domain objects may also contain helper methods to add domain-specific business logic.  Note that the business logic should be independent of any other beans or objects and should be self contained of any dependencies.  One thing to keep in mind is that Grails may confuse helper methods as domain properties so you may have to add the fields to the transient declarations (ie: <code>static transients = ['expired']</code>.  See my <a href="https://github.com/nicholashagen/budget-analyzer/tree/master/grails-app/domain/budget/analyzer">domain classes</a> for an example of how I use domain objects.</p>
<p>Since I was using domain objects, I needed a place to persist them.  Grails provides various persistence capabilities on its GORM platform including Hibernate and NoSQL approaches.  My domain model is very simple, so a traditional Hibernate SQL database worked fine.  For development I used the in-memory H2 database.  This works great especially when domain models are iterated upon so you don&#8217;t have to worry about merge conflicts, database destruction, etc.  I used the <a href="https://github.com/nicholashagen/budget-analyzer/blob/master/grails-app/conf/BootStrap.groovy">BootStrap.groovy</a> startup file in Grails to initialize the database with a set of transactions.  For production, I used a standard MySQL installation.  See <a href="https://github.com/nicholashagen/budget-analyzer/blob/master/grails-app/conf/DataSource.groovy">DataSource.groovy</a> for more information.</p>
<p>The next layer of the server-side architecture is the Grails service layer.  Grails services are simplified Spring managed bean built on autowiring.  They provide the business logic of the platform above the UI layer and controllers as well as shared code between controllers.  Services may exist per domain class to provide functions to query the domain class or they may contain more generalized business logic that utilizes several domain classes.  The organization of the services depends on your particular use cases.  I prefer services to contain the queries and persists rather than within controllers so that the services act as the in-between layer.  Further, services may be easily injected into any bean (controllers, other services, jobs, etc).  As such, they are great at abstracting the queries from each of those components.  The <a href="https://github.com/nicholashagen/budget-analyzer/blob/master/grails-app/services/budget/analyzer/TransactionService.groovy">TransactionService.groovy</a> is an excellent example of this.  It provides business logic to rollup transactions into specific transaction items.  It also provides a few different queries.  However, in order to simplify the query cache and persistence, I use <code>Transaction.list()</code> in both queries and then perform the query in Groovy code.  Transactions in my system are only composed of a 10-30 instances, so performing a list each time is no problem.</p>
<p>Above the service tier is the standard controller tier used to interact with the views.  Controllers provide automatic routing by a convention over configuration rule.  In this application, there are multiple types of transactions: bills, budgets, deposits, etc.  They are each transactions with their own impact to the underlying balance.  As they are each transactions, they all share the same semantics for the views.  The easiest way to do this with controllers is by passing a <code>type</code> query parameter as part of the path.  However, in order to be more RESTful, I put the <code>type</code> parameter as part of the path and injected it as a query param into the controller via <a href="https://github.com/nicholashagen/budget-analyzer/blob/master/grails-app/conf/UrlMappings.groovy">UrlMappings.groovy</a>.  The rule <code>/transaction/$type?/$action?/$id?</code> accomplishes this by associating the type as a query path parameter along with the id.  URL mappings in Grails are simple and effective at creating generic controllers with RESTful linking.  Further, all link creation in Grails (<code>g:link</code>, <code>createLink</code>, etc) properly recreate the links based on the given controller, action, and parameters.  This allows the URL mapping to be changed in a single location without having to update all other links in the application.  Other than that, the <a href="https://github.com/nicholashagen/budget-analyzer/tree/master/grails-app/controllers/budget/analyzer">controllers</a> are fairly straightforward and perform UI logic to setup the UI model that gets passed into the views.  One other minor note is the usage of the static <code>allowedMethods</code> fields to ensure the actions are handled in only certain HTTP transactions.  This is especially helpful when dealing with the RESTful style linking.</p>
<p>The controllers utilize the <a href="https://github.com/nicholashagen/budget-analyzer/tree/master/grails-app/views">views</a> for actual rendering.  The views are fairly straightforward.  In this application, there are two layouts that provide the frame around the page content: <a href="https://github.com/nicholashagen/budget-analyzer/blob/master/grails-app/views/layouts/main.gsp">main</a> and <a href="https://github.com/nicholashagen/budget-analyzer/blob/master/grails-app/views/layouts/dialog.gsp">dialog</a>.  The main layout is the application frame around the pages.  The dialog layout is used to provide a jQuery Mobile dialog frame.  Both layouts provide the associated header sections that provide the meta data, including iOS headers for supporting a home screen web application, icon, and splash screen.  They also provide the assets for javascript and CSS.  Note that I could have used the new Grails resource plugin to inject the CSS automatically.  However, as I only have a few assets most of which are on a CDN via jQuery, it was simpler to just inject in the page directly.  I highly recommend the resources plugin for anything more complex.</p>
<p>The main layout uses two features to allow the page to pass data to the layout.  The first is content sections.  The main layout contains two distinct content sections that page can implement: buttons and content.  The buttons provide the buttons that appear in the page header (think iOS).  The content is the actual page content between the header and footer.  This is achieved in the layout via the <code>&lt;g:pageProperty name="page.content" /&gt;</code> tag.  In the actual page, such as the <a href="https://github.com/nicholashagen/budget-analyzer/blob/master/grails-app/views/overview/list.gsp">main overview page</a>, you simply include a <code>&lt;content tag="content"&gt;</code> tag.  The layout can then choose where to place each content section within its frame.</p>
<p>The second feature is page properties.  For example, the main layout defines the common footer, but in some templates, the footer is not desired.  The page can use standard HTML5 data attributes to notify the layout accordingly. For example, the <a href="https://github.com/nicholashagen/budget-analyzer/blob/master/grails-app/views/transaction/create.gsp">create transaction template</a> disables the footer by specifying <code>&lt;body data-nofooter="true"&gt;</code> on the main body tag.  The layout can retrieve that property as an expression such as <code>&lt;g:if test="${pageProperty(name:'body.data-nofooter') != 'true'}"&gt;</code>. Note that the values are expressed as strings.  Both of these features make layouts much more powerful as well as more effective at being generic.</p>
<p>The only other interesting aspect of the views is using sub-templates to share code between page templates.  The create and edit transaction templates do this as they each provide the same form elements.</p>
<p>The next part of the stack is jobs.  Jobs are Grails components that run periodically (either through a timeout or cron definition) that allow the application to perform background processing.  Jobs are provided by the Grails <a href="http://grails.org/plugin/quartz">Quartz plugin</a>. Note that while 1.0-RC5 is currently out as of this post, I had issues in getting it properly running.  As such, I am using <code>compile "org.grails.plugins:quartz:0.4.2"</code> as my dependency.  The Quartz plugin provides a Grails job component that allows the full Spring managed bean autowiring injection (similar to controllers and services).  To create a new job, invoke the <code>grails create-job</code> command.  The jobs appear in the <a href="https://github.com/nicholashagen/budget-analyzer/tree/master/grails-app/jobs">jobs</a> folder.  The easiest sample is a simple timeout and execution block:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> MyJob <span style="color: #009900;">&#123;</span>
    def timeout <span style="color: #339933;">=</span> 60000L<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 1 minute</span>
&nbsp;
    def execute<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> println <span style="color: #0000ff;">&quot;Invoke me every minute!&quot;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>In this application, the <a href="https://github.com/nicholashagen/budget-analyzer/blob/master/grails-app/jobs/budget/analyzer/BalanceRetrieverJob.groovy">BalanceRetrieveJob</a> is used to retrieve the latest balance every hour.  The job injects a <a href="https://github.com/nicholashagen/budget-analyzer/blob/master/src/groovy/budget/analyzer/BalanceLoader.groovy">balance loader</a> automatically.  The loader is just an interface that a specific implementation can be provided as defined in the Spring <a href="https://github.com/nicholashagen/budget-analyzer/blob/master/grails-app/conf/spring/resources.groovy">resources.groovy</a> file.</p>
<p>Another aspect of the job is sending emails whenever a threshold is exceeded.  The <a href="http://grails.org/plugin/mail">mail plugin</a> (<code>compile "org.grails.plugins:mail:1.0.1"</code> along with a custom repository <code>mavenRepo "http://download.java.net/maven/2/"</code>) provides a very simple method for sending emails using a custom Groovy DSL.  Just setup the appropriate configuration in <a href="https://github.com/nicholashagen/budget-analyzer/blob/master/grails-app/conf/Config.groovy">Config.groovy</a> to get started.</p>
<p>The server side aspect was extremely simple to build and the fact that it only takes a few days to construct is a testimony of the power you can build in an application leveraging Grails.</p>
<p>But one more thing&#8230;</p>
<p>I needed a place to host my newly created application so that I could load it on my iPhone and iPad as a home screen web application.  After first trying to use Google AppEngine and its annoying restrictions followed by my own LAMP domain which did not have sufficient resources to run Java, I decided to use VMWare&#8217;s <a href="http://cloudfoundry.com/">Cloud Foundry</a>.  I already had an account and since it is still in beta it was free to use.  If you have not tried Cloud Foundry yet and are writing Grails applications, I highly recommend it!  You are probably thinking I had to spend a bunch of time setting things up, changing around code, changing the domain model, etc.  If so, you are heavily mistaken.  All I needed to do was include the <a href="http://grails.org/plugin/cloud-foundry">Cloud Foundry plugin</a> (<code>compile "org.grails.plugins:cloud-foundry:1.2.3"</code> and associated Maven repositories) and then run <code>grails prod cf-push</code>.  The plugin took care of the rest including configuring a MySQL instance in the cloud and re-configuring the data source configuration accordingly.  Grails took care of auto-creating the schema, the bootstrap took care of initializing the transactions, and the job took care of downloading the initial balance on startup.  Once I was ready to re-deploy, I simply had to run <code>grails prod cf-update</code>.  It was so extremely easy I am actually annoyed I started with my other options first. For more information including other minor setup steps, see the <a href="http://blog.springsource.com/2011/04/12/one-step-deployment-with-grails-and-cloud-foundry/">One-Step Deployment Guide</a>.</p>
<p>The biggest aspect that impressed me with Cloud Foundry was speed and performance.  When I ran on App Engine (which required all sorts of other annoying changes and setup and was no where near as simple), it took forever to load at times.  When I ran in my domain, it was horrific.  When I run in Cloud Foundry, it renders pages in milliseconds making the experience awesome.</p>
<p>Stay tuned for the next series on using jQuery Mobile and Phantom JS.</p>
<p>Read the next article on <a href="http://www.znetdevelopment.com/blogs/2013/01/24/budget-analyzer-application-client-side-via-jquery-mobile/" title="Budget Analyzer Application: Client Side via jQuery Mobile">Client Side via jQuery Mobile</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.znetdevelopment.com/blogs/2013/01/23/budget-analyzer-application-server-side-via-grails/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>RFC: Add &#8216;between&#8217; and &#8216;in&#8217; to modern day languages</title>
		<link>http://www.znetdevelopment.com/blogs/2012/10/01/rfc-add-between-and-in-to-modern-day-languages/</link>
		<comments>http://www.znetdevelopment.com/blogs/2012/10/01/rfc-add-between-and-in-to-modern-day-languages/#comments</comments>
		<pubDate>Mon, 01 Oct 2012 15:33:46 +0000</pubDate>
		<dc:creator>Nicholas Hagen</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[operators]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[RFC]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.znetdevelopment.com/blogs/?p=646</guid>
		<description><![CDATA[One of the recent additions to TeaTrove I am making is adding the between and in operator. This may seem trivial, but they are quite powerful as well as generally better performing, not to mention easier to read. The SQL standard has had these operators for decades because they just make sense. I&#8217;m rather surprised [...]]]></description>
				<content:encoded><![CDATA[<p>One of the recent additions to TeaTrove I am making is adding the <code>between</code> and <code>in</code> operator.  This may seem trivial, but they are quite powerful as well as generally better performing, not to mention easier to read.  The SQL standard has had these operators for decades because they just make sense.  I&#8217;m rather surprised by how many modern day languages do not have it.  Many of the languages do have support for the <code>in</code> operator, which is a good addition, but it&#8217;s not a suitable replacement as the <code>between</code> operator.<br />
<span id="more-646"></span><br />
For example, consider the following code:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>getX<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">5</span> and getX<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">9</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Although that makes sense, the following is much more readable:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>getX<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> between <span style="color: #cc66cc;">5</span> and <span style="color: #cc66cc;">9</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>It is also faster because <code>getX()</code> is only invoked once rather than twice as in the previous example.  You could avoid that in the previous example, but using a local variable, but often times that is overkill for a simple check.</p>
<p>The <code>in</code> operator is even more convenient:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>getX<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">5</span> <span style="color: #339933;">||</span> getX<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">7</span> <span style="color: #339933;">||</span> getX<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">9</span> <span style="color: #339933;">||</span> getX<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">11</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">// vs</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>getX<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> in <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">5</span>, <span style="color: #cc66cc;">7</span>, <span style="color: #cc66cc;">9</span>, <span style="color: #cc66cc;">11</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>With the range operator in Groovy, this is possible to simulate the <code>between</code> operator but it does not perform as fast as it has to do a list creation and then a list lookup rather than two conditional checks which is heavily optimized in VMs.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>getX<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">in</span> <span style="color: #cc66cc;">5</span>..<span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Hopefully future languages (Groovy, Javascript, Python, Ruby, etc) can all add the <code>between</code> and <code>in</code> operators in future releases to further empower developers.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.znetdevelopment.com/blogs/2012/10/01/rfc-add-between-and-in-to-modern-day-languages/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Good Use Case for Server-Sent Events</title>
		<link>http://www.znetdevelopment.com/blogs/2012/09/02/good-use-case-for-server-sent-events/</link>
		<comments>http://www.znetdevelopment.com/blogs/2012/09/02/good-use-case-for-server-sent-events/#comments</comments>
		<pubDate>Sun, 02 Sep 2012 20:18:36 +0000</pubDate>
		<dc:creator>Nicholas Hagen</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[Animations]]></category>
		<category><![CDATA[CSS3]]></category>
		<category><![CDATA[EventSource]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Progress Bar]]></category>
		<category><![CDATA[Server Sent Events]]></category>
		<category><![CDATA[servlets]]></category>
		<category><![CDATA[SSE]]></category>
		<category><![CDATA[WebSockets]]></category>

		<guid isPermaLink="false">http://www.znetdevelopment.com/blogs/?p=632</guid>
		<description><![CDATA[Although some would say there are two competing standards for handling live events on the web, websockets and server-sent events (event source), they are really very different from each other with their own use cases. I have always been a proponent of websockets as it is bi-directional and as a result more powerful. Although in [...]]]></description>
				<content:encoded><![CDATA[<p>Although some would say there are two competing standards for handling live events on the web, websockets and server-sent events (event source), they are really very different from each other with their own use cases.  I have always been a proponent of websockets as it is bi-directional and as a result more powerful.  Although in reality, there are few cases where you truly need bi-directional support.  Nonetheless, I&#8217;ve always been of the opinion of &#8220;what if&#8221; one day you need bi-directional support.  It&#8217;s better to be on a platform today that supports it tomorrow.  As a result, I&#8217;ve never had a good use case for server-sent events.  Today, I came across one.</p>
<p><span id="more-632"></span><br />
Server sent events are actually quite easy to implement, much more so than websockets (talking pure protocol of course as frameworks exist for both that make it easy).  In fact, event source at the server side is basically a standard HTTP request/response with a specific format over a long-held request.  On the client-side, the browser has a built-in JavaScript API that knows how to handle that format to receive asynchronous callbacks.  This allows applications to easily support server-sent events for certain use cases.  My specific use case I built into a standard Servlet on Tomcat with the native JavaScript API.</p>
<p>So what is my use case?  My use case is basically providing progress for long running tasks.  Imagine a user clicks on a submit button or a link that results in a long running request.  How do you notify the user of that progress?  The old way was just putting a spinner on the page to notify the user that &#8220;something&#8221; was happening.  However, what if the &#8220;something&#8221; actually failed or is stuck?  The user would never know.  The next method was the application would spin off a second AJAX request to constantly ping the server for progress.  I have never been a big support of this method as it results in two connections per user as well as constantly pinging the server wasting bandwidth.  Long polling came around to help alleviate the bandwidth issues, but you would still need 2 connections not to mention the expense of tearing down and bringing up the long polling connections. My answer to this is server-sent events.  This could also be done with web-sockets, but it&#8217;d be much more complex and have no added benefit.  Remember the principle rule of development: KISS (keep it simple).  In other words, do not use complicated technology unless you get a better bang for your buck.  In this scenario we already have to invoke an HTTP request to handle the submission and since SSE is essentially an HTTP request, it works nicely.</p>
<p>Let&#8217;s get to the code.  The first portion is the server side.  This example is a pure Java Servlet but you could use any technology.  You could also use other servlet-based frameworks rather than dealing specifically with the servlet.  This is only meant to demonstrate what you could do.</p>
<p><script src="https://gist.github.com/3604120.js?file=LongTaskServlet.java"></script></p>
<p>Basically, once the <code>doGet</code> method is called, it sets the content type and encoding so that the client knows that a EventSource connection is being established.  Failure to set these settings will cause the client to error.  Next, it creates a long running task to do whatever it needs to do which also has a progress state.  The task is used to send another SSE whenever the progress updates.  This allows the task to immediately notify the client whenever its progress updates.  The actual data is sent down as JSON so that the client can parse it for information.</p>
<p>The client code is just as simple.</p>
<p><script src="https://gist.github.com/3604120.js?file=index.html"></script></p>
<p>Basically, whenever the link is invoked, it sends an EventSource connection to the above servlet.  Every time a message is sent it updates the progress bar accordingly.  Once the complete message is received it properly closes the connection so that it is not re-established by the browser.  At this point, the script would update the page notifying the user of completion.</p>
<p>Note that the CSS3 progress bar in this example is based off the great work at <a href="http://www.catswhocode.com/blog/how-to-create-a-kick-ass-css3-progress-bar">http://www.catswhocode.com/blog/how-to-create-a-kick-ass-css3-progress-bar</a>.</p>
<p>The full example is at this Gist: https://gist.github.com/3604120</p>
]]></content:encoded>
			<wfw:commentRss>http://www.znetdevelopment.com/blogs/2012/09/02/good-use-case-for-server-sent-events/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Public Request for Feedback from AT&amp;T</title>
		<link>http://www.znetdevelopment.com/blogs/2012/08/23/public-request-for-feedback-from-att/</link>
		<comments>http://www.znetdevelopment.com/blogs/2012/08/23/public-request-for-feedback-from-att/#comments</comments>
		<pubDate>Thu, 23 Aug 2012 12:12:51 +0000</pubDate>
		<dc:creator>Nicholas Hagen</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[3G]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[AT&T]]></category>
		<category><![CDATA[Bandwidth]]></category>
		<category><![CDATA[Facetime]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[Skype]]></category>

		<guid isPermaLink="false">http://www.znetdevelopment.com/blogs/?p=622</guid>
		<description><![CDATA[To Whom It May Concern at AT&#038;T: From the rest of the community who is fully confused by the latest announcement from AT&#038;T concerning Facetime over 3G and the requirement for the new shared data plans, we would like to have some questions answered that would help to further solidify your position and rationale for [...]]]></description>
				<content:encoded><![CDATA[<p>To Whom It May Concern at AT&#038;T:</p>
<p>From the rest of the community who is fully confused by the <a href="http://attpublicpolicy.com/fcc/enabling-facetime-over-our-mobile-broadband-network/">latest announcement</a> from AT&#038;T concerning Facetime over 3G and the requirement for the new shared data plans, we would like to have some questions answered that would help to further solidify your position and rationale for this announcement.<br />
<span id="more-622"></span><br />
1.  <strong><em>Why is it a requirement to only support the new shared data plans rather than the tiered data plans?</em></strong>  I know of several people currently with an unlimited plan who would willingly move to a tiered data plan, including myself, in order to gain Facetime over 3G.  However, we do not want to be forced to pay an additional $20-40 on average from our current plans.  For example, as a user who does not talk often and who never uses texting, why would I need to have unlimited quantities?  In that scenario, I only need 400-500 minutes and no texting, which averages around $50-60 a month.  Add 4 GB of data and that is only $80-90 a month versus the $70 + $40 for shared data plans.  If the biggest issue is truly data usage, why does it matter if I am on a tiered data plan or a shared data plan (data is data and caps are caps).  The use=age of texting and voice should not impact the usage of data and applications.  The end result is the same for both the consumer and your bottom line.</p>
<p>2. <strong><em>Why is this restriction only in place for Facetime?</em></strong>  There are several other popular video communication applications for smartphones, the most popular being Skype.  Skype currently works over 3G without issue and without restriction.  One could argue that Skype has more market penetration than Facetime and is potentially more threatening to your network.  For one, Skype has a much larger market share and supports many more devices due to its support for Android and iOS (not to mention tablets, computers, and other smartphones).  From the latest <a href="https://play.google.com/store/apps/details?id=com.skype.raider&#038;hl=en">Google Play market statistics</a>, the Android-based Skype application has been downloaded 50M-100M times.  Compare that to the fact that only the iPhone 4 and iPhone 4S support Facetime and only iOS to iOS calls are supported.  I&#8217;m willing to guess the market share is drastically in favor of Skype.  For example, last quarter, only <a href="http://www.thestreet.com/story/11395189/1/atts-iphone-customer-base-grows.html">7M iPhones were activated</a> on the AT&#038;T network.  The nationwide split is around <a href="http://www.bgr.com/2012/07/02/android-market-share-us-smartphone-iphone/">50% Android and 32% iOS</a>.  That being said, why should Facetime be disallowed but Skype allowed.  That seems to give an unfair competitive advantage to Skype without justifiable means.  This is further compounded when you add other video-chat applications such as Qik, Fring, Tango, Google+, etc.  If the answer is that Facetime is &#8220;theoretically&#8221; easier and more penetratable as market share, can you please share that data with us?  However, why should one companies&#8217; success force an unfair advantage?  Either all video applications should be disallowed or none should be.</p>
<p>3. <strong><em>If the selling point is you need to have a specific shared data plan, then why should the data used by Facetime count against that data usage?</em></strong>  If you are requiring a specific data plan, there has to be an incentive to selecting that plan versus another plan such as the tiered data plans.  Otherwise, this just seems like a ruthless attempt to coerce users into unwanted plans and extra cost.</p>
<p>4. <strong><em>Why not make Facetime only available over certain network capabilities (HSPA+ or LTE)?</em></strong>.  In theory LTE, and even HSPA+ to a lesser extent, should provide better bandwidth than today&#8217;s older generation 3G networks.  It would not be unreasonable to only support Facetime over these high speed networks for both bandwidth conservation and sheer performance and quality.  The community, although still disappointed, would be able to better understand these types of restrictions (other than the fact that Skype and other applications run over 3G still).  This would also allow a smaller rollout over time in order to better gauge the true network impact.</p>
<p>These answers would help the community better understand where you are coming from as an organization.  We are all fully aware of the data bandwidth issues plaguing wireless carriers today, but in the end the decisions being made in this issue do not make sense:  competitor networks are not making such concessions, other applications work without restriction, market share analysis (at least what the public has access to) provides no known concrete evidence that Facetime over 3G will substantially impact wireless networks adversely.  These types of issues only worsen the credibility of AT&#038;T, decrease customer satisfication, and ultimately confuse your user base.</p>
<p>We hope to hear back from you regarding these questions,</p>
<p>Sincerely,</p>
<p>The AT&#038;T iPhone Community<br />
on behalf of Nicholas Hagen</p>
]]></content:encoded>
			<wfw:commentRss>http://www.znetdevelopment.com/blogs/2012/08/23/public-request-for-feedback-from-att/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Handling Environments in Java EE Projects</title>
		<link>http://www.znetdevelopment.com/blogs/2012/08/07/handling-environments-in-java-ee-projects/</link>
		<comments>http://www.znetdevelopment.com/blogs/2012/08/07/handling-environments-in-java-ee-projects/#comments</comments>
		<pubDate>Tue, 07 Aug 2012 12:42:08 +0000</pubDate>
		<dc:creator>Nicholas Hagen</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Application Server]]></category>
		<category><![CDATA[EJB]]></category>
		<category><![CDATA[Environment]]></category>
		<category><![CDATA[Glassfish]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[Java EE]]></category>
		<category><![CDATA[Java SE]]></category>
		<category><![CDATA[JPA]]></category>
		<category><![CDATA[Listener]]></category>
		<category><![CDATA[MDB]]></category>
		<category><![CDATA[Persistence]]></category>
		<category><![CDATA[Servlet]]></category>
		<category><![CDATA[Web Application]]></category>

		<guid isPermaLink="false">http://www.znetdevelopment.com/blogs/?p=612</guid>
		<description><![CDATA[Web applications in Java EE (and even SE) have many components from MDB configuration to JMS configuration to Servlet configuration to JPA/database configuration. Each of these components has their own methodology of configuration. Further, some are easier to manage than others. The reason this is important is when it comes to managing your environments from [...]]]></description>
				<content:encoded><![CDATA[<p>Web applications in Java EE  (and even SE) have many components from MDB configuration to JMS configuration to Servlet configuration to JPA/database configuration.  Each of these components has their own methodology of configuration.  Further, some are easier to manage than others.  The reason this is important is when it comes to managing your environments from development to test to production.  The most ideal situation is a single build and archive (WAR, EAR, etc) that you can deploy to any environment and change the settings at the container level.  This blog article hopes to explain some methods of managing those configurations.<br />
<span id="more-612"></span></p>
<p><strong>Servlets</strong></p>
<p>The first piece of configuration we will talk about is servlets.  Typically, you will not change servlet paths from one environment to another, but there are certain occasions where you will need to.  The problem is that there is no way to do substitution parameters in <code>web.xml</code>.  One way I found to do this is by using the new servlet-based methods to dynamically add servlets.  The Servlet 3.0 specification allows you to add servlets dynamically during startup, typically as part of a Servlet Context Listener.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;">@WebServletContextListener 
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyServletContextListener <span style="color: #000000; font-weight: bold;">implements</span> ServletContextListener <span style="color: #009900;">&#123;</span> 
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> contextInitialized<span style="color: #009900;">&#40;</span>ServletContextEvent event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
        <span style="color: #666666; font-style: italic;">// create servlet instance</span>
        MyServlet servlet <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyServlet<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// get current environment</span>
        <span style="color: #003399;">String</span> env <span style="color: #339933;">=</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;env.name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// configure servlet per environment (dev, test, prod, etc)</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// register and configure the servlet</span>
        ServletRegistration.<span style="color: #006633;">Dynamic</span> reg <span style="color: #339933;">=</span>
            event.<span style="color: #006633;">getServletContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">addServlet</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MyServlet&quot;</span>, servlet<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        reg.<span style="color: #006633;">setLoadOnStartup</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// configure mapping based on environment</span>
        <span style="color: #003399;">String</span> mapping <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;dev&quot;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>env<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> mapping <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;devctx&quot;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;test&quot;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>env<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> mapping <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;testctx&quot;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span> mapping <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;prodctx&quot;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
        reg.<span style="color: #006633;">addMapping</span><span style="color: #009900;">&#40;</span>mapping<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> 
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> contextDestroyed<span style="color: #009900;">&#40;</span>ServletContextEvent event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
        <span style="color: #666666; font-style: italic;">// ...</span>
    <span style="color: #009900;">&#125;</span> 
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Servlets must only be added dynamically during startup through a servlet context listener. For more information, see the <a href="http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContext.html">JavaDocs</a>.  Another idea would be to use a custom servlet that acts as a delegate but allows parameter substitution.  In this situation, you could use variables such as <code>${property}</code> within <code>init-param</code> values.  The delegating servlet could then load those parameters and substitute system properties for the variables before passing onto the corresponding servlet.</p>
<p><strong>Persistence Units (JPA)</strong></p>
<p>There are two was to use JPA in a Java EE application.  The first is using <code>@PersistenceContext</code> to inject an <code>EntityManager</code> into a bean.  The second is to directly use <code>Persistence.createEntityManagerFactory</code>.  In either scenario, JPA loads a matching <code>META-INF/persistence.xml</code>.  Within the persistence unit there are two ways to configure the associated database data source.  The first is using JNDI sources.  In this case, the container manages the actual configuration which is the best option as you would configure each container differently per environment.  However, if that is not a possibility for your scenario (such as using a standard SE application or simple web application), then you can use substitution values to override the persistence unit properties.  For example, when using Hibernate as the implementation to JPA, you must configure the <code>hibernate.connection.url</code> which would be different per environment.  The <code>Persistence.createEntityManagerFactory</code> method has a form that takes two arguments: the unit name and a set of override properties.  The second argument can be used to override or add properties to the persistence unit properties.  For example, you could pass the system properties as the second argument and then define the overrides as system properties.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;">EntityManagerFactory emf <span style="color: #339933;">=</span> Persistence.<span style="color: #006633;">createEntityManagerFactory</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;DataSource&quot;</span>, <span style="color: #003399;">System</span>.<span style="color: #006633;">getProperties</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
EntityManager em <span style="color: #339933;">=</span> emf.<span style="color: #006633;">createEntityManager</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>On the actual command line, you could define the property such as: <code>-Dhibernate.connection.url=jdbc:....</code>.  That will then override the URL setting in the persistence unit.  Each environment would pass a different option.</p>
<p><strong>MDB Queues</strong></p>
<p>MDBs and their resource adapter counterparts often times require a configuration for the actual JMS environment (hostname, port, etc).  These settings would once again be environment specific.  The problem is that the configuration is usually part of an EJB configuration file or using annotations.  The nice thing is that some containers allow some of these options to use substitution expressions using system properties.  For more information and specific examples for Glassfish, see the blog article on <a href="http://www.java.net/node/676383">The power of System properties substitution in Glassfish</a>.  Other containers such as JBoss, Websphere, etc offer similar support.  Consult your individual container for more information.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.znetdevelopment.com/blogs/2012/08/07/handling-environments-in-java-ee-projects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Grails 2.1 and Maven Integration: Multi-Module Projects</title>
		<link>http://www.znetdevelopment.com/blogs/2012/07/15/grails-2-1-and-maven-integration-multi-module-projects/</link>
		<comments>http://www.znetdevelopment.com/blogs/2012/07/15/grails-2-1-and-maven-integration-multi-module-projects/#comments</comments>
		<pubDate>Sun, 15 Jul 2012 17:13:30 +0000</pubDate>
		<dc:creator>Nicholas Hagen</dc:creator>
				<category><![CDATA[Grails]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Grails Maven Plugin]]></category>
		<category><![CDATA[Maven]]></category>
		<category><![CDATA[Maven Profiles]]></category>
		<category><![CDATA[Maven Release Plugin]]></category>
		<category><![CDATA[Maven Replace Plugin]]></category>
		<category><![CDATA[Multi-Module Projects]]></category>

		<guid isPermaLink="false">http://www.znetdevelopment.com/blogs/?p=600</guid>
		<description><![CDATA[In the previous two blog articles (simple projects) (plugins), we touched on setting up standard Grails applications and plugins through Maven. In this article, we are going to talk about multi-module projects and how to build plugins and applications together as a single Maven build. The first step is to create the directory structure and [...]]]></description>
				<content:encoded><![CDATA[<p>In the previous two blog articles (<a href="http://www.znetdevelopment.com/blogs/2012/07/11/grails-2-1-and-maven-integration-simple-project/" title="Grails 2.1 and Maven Integration: Simple Project">simple projects</a>) (<a href="http://www.znetdevelopment.com/blogs/2012/07/13/grails-2-1-and-maven-integration-plugins/" title="Grails 2.1 and Maven Integration: Plugins">plugins</a>), we touched on setting up standard Grails applications and plugins through Maven.  In this article, we are going to talk about multi-module projects and how to build plugins and applications together as a single Maven build.<span id="more-600"></span></p>
<p>The first step is to create the directory structure and the plugins and applications for the Grails project.  Use the following commands to setup the structure:</p>
<p><code><br />
#> mkdir sample<br />
#> cd sample<br />
#> grails create-plugin plugin-a<br />
#> grails create-plugin plugin-b<br />
#> grails create-app test<br />
</code></p>
<p>If we look at our directory structure, we now have three directories, two representing plugins and one representing the application that will make use of the plugins.  To enable this as a Maven multi module project, run the following command to generate all of the required POMs.</p>
<p><code><br />
grails create-multi-project-build com.mycompany.grails:sample:1.0.0-SNAPSHOT<br />
</code></p>
<p>This will now create a POM in the main directory and each of the sub-directories.  The command will also properly process the plugins as plugin projects and the applications as application projects.  The root POM will be the parent POM containing each directory as a module.  Note that this script will not generate or overwrite a given app or plugin if a POM already exists.  In those cases, you will need to make sure you set the parent properly.</p>
<p>Let&#8217;s start in the parent POM in the main directory.  After opening this POM you will notice it is pretty simple.  The <code>groupId</code>, <code>artifactId</code> and <code>version</code> are all set to the values passed on the command line.  The <code>packaging</code> is set to <code>pom</code> which associates this project as a parent POM.  The last portion of the POM is the list of modules that should be executed with this project. This should contain all of the children modules.</p>
<p>Note that you can also include standard Maven and Java projects that are in the same directory structure by simply adding them as a module as well.  This main project will resultingly build all children projects, regardless of type (<code>grails-plugin</code>, <code>grails-app</code>, <code>jar</code>, etc) and automatically build in the proper order based on the dependency management.</p>
<p>Now let&#8217;s look at the individual POMs.  Let&#8217;s start with one of the plugins, such as <code>plugin-a</code>.  If you recall from my <a href="http://www.znetdevelopment.com/blogs/2012/07/13/grails-2-1-and-maven-integration-plugins/" title="Grails 2.1 and Maven Integration: Plugins">previous article</a>, this closely resembles the standard POM from a Grails plugin.  The only real difference is that this POM contains a parent section that references the parent POM in the main directory.  The rest is the same.  You should follow the instructions in that article to ensure the POM and <code>*GrailsPlugin.groovy</code> file are in sync in terms of versioning.  One last thing to note is that for plugins, Grails sets the <code>groupId</code> to <code>org.grails.plugins</code> to follow its conventional practice for 3rd party plugins.  For internal sites, you may want to change this to your own company such as <code>com.mycompany.grails</code>.  Make sure that if you change the <code>groupId</code> in the plugin POM, you also change it in any dependencies of any other plugins or applications.</p>
<p>Similarly, if we look at the POM from the <code>test</code> application directory, it too is the same per my <a href="http://www.znetdevelopment.com/blogs/2012/07/11/grails-2-1-and-maven-integration-simple-project/" title="Grails 2.1 and Maven Integration: Simple Project">previous article</a>.  Again, the only difference is the inclusion of the parent section.  You should follow the instructions in that article to ensure the POM and <code>application.properties</code> file are in sync in terms of versioning.  While we are here, let&#8217;s also add the two plugins as dependencies.  The dependencies may already be listed if Grails automatically added them to the POM.  If you wanted to be even more interesting, though, you could have <code>plugin-a</code> be a dependency of <code>plugin-b</code> and then add just <code>plugin-a</code> as a dependency of the application.  Maven will correctly determine the order and build accordingly.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.grails.plugins<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>plugin-a<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.0.0-SNAPSHOT<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>zip<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>compile<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.grails.plugins<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>plugin-b<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.0.0-SNAPSHOT<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>zip<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>compile<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>At this point, you should be able to go back to the main directory and run <code>mvn clean package</code>.  That should build both plugins first followed by the application.  The application will install both plugins into it and then build itself.  In the end you will wind up with two ZIP archives that are installed in the Maven repository and one WAR archive that is installed.</p>
<p>We could stop here, but that&#8217;d be too easy.  Maven has much more powerful constructs to simplify maintenance and configuration of the POMs.</p>
<p>The first thing I would change is the versions.  Within multi-module projects, if all versions are the same and match the parent POM, then you do not need to include the version.  The same applies for the <code>groupId</code> (although remember that the groupId of plugins may be different by convention).  This will, then, remove the duplication of version in each POM (once in the parent and once in the children).  To update the version, simply use the Maven release plugin or the <code>mvn versions:set -DnewVersion=1.0.1-SNAPSHOT</code> command.  These will both cycle through the parent POM and all child modules to update the versions accordingly.  As an example, my application POM now looks like this:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;parent<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.znet.grails<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>sample<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.0.0-SNAPSHOT<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/parent<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>test<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;packaging<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>grails-app<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/packaging<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>The next thing I noticed is that each POM includes the Grails version via the <code>grails.version</code> property.  In theory, within a multi-module project setup, all modules would be built using the same Grails version.  As a result, we can place this in our parent POM and automatically inherit in our children POMs.  So, remove the <code>properties</code> block from each child POM and copy it into the parent POM.</p>
<p>Up next? Dependencies.  Maven has a neat construct called <code>dependencyManagement</code> that is particularly useful within parent POMs.  Dependencies within the <code>dependencyManagement</code> section are not actually dependencies of any given project.  Instead they are mere templates.  In this way, you can define the <code>groupId</code>, <code>artifactId</code>, <code>version</code>, <code>type</code>, and <code>scope</code> for any or all dependencies.  Then, within the <code>dependencies</code> section of any other child POM, you only reference the <code>groupId</code> and <code>version</code>.  If all three plugins all made use of the same dependency, you would only need to change the version once rather than three times.  Let&#8217;s add a couple dependencies to the parent POM (note that this works because we just added the <code>grails.version</code> property definition to the parent POM).  You may also choose to add all the dependencies in the application POM as needed.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependencyManagement<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.grails<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>grails-dependencies<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${grails.version}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>pom<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.grails<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>grails-plugin-testing<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${grails.version}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>test<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependencyManagement<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>We can now update each plugin and the main application POMs to change these two core dependencies to just the following.  Now, we have a single place of configuration and maintenance.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependencies<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.grails<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>grails-dependencies<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>pom<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.grails<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>grails-plugin-testing<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    ...
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependencies<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>You will notice that we just updated those two dependencies in all three children.  Being every Grails app and/or plugin will include those dependencies, we can just add them to the <code>dependencies</code> (NOT <code>dependencyManagement</code>) block of the parent POM and remove from the children POMs.  In this manner, all children projects that inherit from the parent will get those dependencies.  Once again, we have a single place of maintenance.  The Grails application child POM will still include several other dependencies, but those dependencies are not shared by ALL children dependencies.  Only the dependencies that exist in all children should be placed in the parent POM dependencies block.  One other potential issue is that if your multi module project also includes non-Grails projects such as Java/JAR projects, then this will also not work.  One solution for that is to have a top level multi module project for all projects, including Java, and a single Grails project which itself is a multi-module project.  For example:</p>
<pre>
sample/
    java-library-jar/
    grails-modules/
        plugin-a/
        plugin-b
        test-app/
</pre>
<p>Any of the Grails app can still reference the Java library but the <code>grails-module</code> will also be a multi-module project that will only contain Grails modules, thus allowing us the conventions in this article.</p>
<p>The next piece in comparing the POMs to simplify is the <code>plugin</code> section.  Once again, the same configuration has been included in every POM.  Rather than include in every POM, we can remove from each child POM and include in the main POM instead.  Again, take note that if non-Grails projects exist, this will not work (consider using <code>pluginManagement</code> instead).  Another workaround there other than the one above is to use Maven profiles and use a file existence activator based on the presence of the <code>grails-app</code> directory.  In other words, if a <code>grails-app</code> directory exists in a given module, then activate it as a Grails build and invoke a set of plugins.  Non-Grails projects will not have those plugins activated then.</p>
<p>If you remember from the previous articles we had to include some modifications to the Grails plugins, add custom plugins for doing search and replace for versioning artifacts, and update the Maven release process for it.  As we now have a single location for our plugin management, we can make those changes to the parent POM rather than each individual POM.  This dramatically improves maintainability.</p>
<p>One issue to be aware of is with the search and replace plugin.  That plugin had a hard-coded file name.  To avoid that issue, use the <code>filesToInclude</code> property instead to use GLOB-style matches.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.google.code.maven-replacer-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>replacer<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.5.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;executions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;execution<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;phase<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>validate<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/phase<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;goals<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;goal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>replace<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/goal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/goals<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/execution<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/executions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filesToInclude<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>*GrailsPlugin.groovy<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filesToInclude<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;replacements<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;replacement<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;token<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/\*\s*@@VERSION@@\s*\*/\s*&quot;.*&quot;\s*/\*\s*@@VERSION@@\s*\*/<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/token<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/* @@VERSION@@ */ &quot;${project.version}&quot; /* @@VERSION@@ */<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/replacement<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/replacements<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>This will only match those files in the main directories ending with <code>GrailsPlugin.groovy</code>, which, by convention, all Grails plugins do.</p>
<p>Another issue to be aware of is that for the plugins, we changed the <code>validate-plugin</code> goal to run in a different phase so that the search and replace would work properly.  If you keep this configuration in the parent POM, it will attempt to run the <code>validate-plugin</code> task on every project, which will fail in the application project as that is a Grails application, not a plugin.  This unfortunately is difficult to solve.  The best way to solve, without having to add duplicated plugin configuration to the POMs, is through Maven profiles.  Maven profiles can have custom builds and plugins.  Unfortunately, because plugins and apps look so similar, it is not easy out of the box to configure this.  The best way I found is to use the existence of a file.  For example, you could drop a <code>grails.plugin</code> file in each plugin directory and key off of that.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;profiles<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;profile<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>grails-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;activation<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;file<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;exists<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>grails.plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/exists<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/file<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/activation<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;build<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugins<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.grails<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>grails-maven-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${grails.version}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fork<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>true<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/fork<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;extensions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>true<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/extensions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;executions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;execution<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>default-validate-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;phase<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>initialize<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/phase<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;goals<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;goal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>validate-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/goal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/goals<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/execution<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/executions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugins<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/build<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/profile<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/profiles<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>This is not the cleanest approach and requires having an extra file in each plugin directory, but it works properly.  The good news is that the configuration and maintenance is only in one place.  You could even pull this configuration out to a super POM that all Grails projects (single module or multi-module) depend on.</p>
<p>If we now look at our child POMs, the plugins are almost empty, with the only piece being the <code>repository</code> and <code>tools</code> profile.  As those are once again shared by the plugins and applications, pull this out into the parent POM.  With that in place, the plugins and applications are now very simple only providing their metadata and dependencies.  The Grails configuration and build process is all centralized to the parent or super POM.</p>
<p>For a complete example of this setup that includes the search and replace plugin and Maven release plugin support for local checkins, see my examples on <a href="https://github.com/nicholashagen/blog/tree/master/grails/multi" target="_blank">GitHub</a>.</p>
<p>Hopefully all three of these articles will help you to configure your Grails projects as a Maven build.</p>
<p>Other Articles in this Series:</p>
<ol>
<li><a href="http://www.znetdevelopment.com/blogs/2012/07/11/grails-2-1-and-maven-integration-simple-project/" title="Grails 2.1 and Maven Integration: Simple Project">Grails 2.1 and Maven Integration: Simple Project</a></li>
<li><a href="http://www.znetdevelopment.com/blogs/2012/07/13/grails-2-1-and-maven-integration-plugins/" title="Grails 2.1 and Maven Integration: Plugins">Grails 2.1 and Maven Integration: Plugins</a></li>
<li>Grails 2.1 and Maven Integration: Multi-Module Projects</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.znetdevelopment.com/blogs/2012/07/15/grails-2-1-and-maven-integration-multi-module-projects/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Grails 2.1 and Maven Integration: Plugins</title>
		<link>http://www.znetdevelopment.com/blogs/2012/07/13/grails-2-1-and-maven-integration-plugins/</link>
		<comments>http://www.znetdevelopment.com/blogs/2012/07/13/grails-2-1-and-maven-integration-plugins/#comments</comments>
		<pubDate>Fri, 13 Jul 2012 11:19:48 +0000</pubDate>
		<dc:creator>Nicholas Hagen</dc:creator>
				<category><![CDATA[Grails]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Grails Maven Plugin]]></category>
		<category><![CDATA[Grails Plugins]]></category>
		<category><![CDATA[Maven]]></category>
		<category><![CDATA[Maven Release Plugin]]></category>
		<category><![CDATA[Maven Replacer Plugin]]></category>

		<guid isPermaLink="false">http://www.znetdevelopment.com/blogs/?p=582</guid>
		<description><![CDATA[My last blog article explained using the Maven integration in the recently released 2.1 version of Grails on simple applications. Today, we are going to look at using the Maven integration and build, manage, and deploy plugins. Plugins and applications in Grails are very similar to each other. The big difference is that a plugin [...]]]></description>
				<content:encoded><![CDATA[<p>My last blog <a href="http://www.znetdevelopment.com/blogs/2012/07/11/grails-2-1-and-maven-integration-simple-project/" title="Grails 2.1 and Maven Integration: Simple Project" target="_blank">article</a> explained using the Maven integration in the recently released 2.1 version of Grails on simple applications.  Today, we are going to look at using the Maven integration and build, manage, and deploy plugins.  Plugins and applications in Grails are very similar to each other.  The big difference is that a plugin has a special plugin descriptor file named <code>*GrailsPlugin.groovy</code> where <code>*</code> is the name of the plugin.  Other than that, they behave very similar to each other.  Plugins generally do not have as many dependencies, however, as they are not required to include the web application environment typically.<span id="more-582"></span></p>
<p>The first step is to create a plugin.  Instead of using <code>grails create-app</code>, we will use the plugin-variant <code>grails create-plugin</code>.  At that point, we can once again use the <code>create-pom</code> command to create the resulting POM.</p>
<p><code><br />
#> grails create-plugin my-plugin<br />
#> cd my-plugin<br />
#> grails create-pom com.mycompany.groupid<br />
</code></p>
<p>Before looking at the POM, we should update the plugin descriptor first.  Grails automatically creates a plugin descriptor based on the name of the plugin.  In this case, the descriptor is named <code>MyPluginGrailsPlugin.groovy</code>.  One of the first few lines is the defined version.  In order to be more Maven-compliant by using <code>SNAPSHOT</code> in the version while in development mode, change the version to <code>0.1-SNAPSHOT</code>.</p>
<p>Now, let&#8217;s crack open the POM.  You will notice that the generated POM for the plugin closely resembles the generated POM for a Grails application.  The only major difference is the number of required dependencies.  Per my previous article, Grails automatically generates the dependencies based on the dependencies block in the <code>BuildConfig.groovy</code> file.</p>
<p>The first step that is required is to change the packaging type.  Grails applications use the packaging type of <code>grails-app</code>; however, plugins need to use <code>grails-plugin</code>.  As a result, make sure to update the packaging type to <code>grails-plugin</code>.  This configures Maven for the proper plugins and commands when building and deploying the plugin.  Whereas the Grails application creates WAR artifacts, the Grails plugins creates ZIP artifacts.</p>
<p>The next step is to change the version.  The version in the POM must match precisely with what was configured in the plugin descriptor.  In fact, the validation plugin in the Grails Maven plugin ensures they match before deployment.  As a result, set the version to match what is in the plugin descriptor file (ie: 0.1-SNAPSHOT).</p>
<p>At this point, you may also choose to set the name, description, include other dependencies, etc.  Now we are ready to build and deploy.</p>
<p><code><br />
#> mvn clean install<br />
</code></p>
<p>This will build and install the plugin as a ZIP archive in your local Maven repository.  You could also deploy it to a remote Maven repository by configuring the deployment repository.</p>
<p>With our plugin built and deployed locally, we can now include this plugin in our previous sample application.  If you do not have a sample application, simply follow my previous article or run these commands:</p>
<p><code><br />
#> cd ..<br />
#> grails create-app myapp<br />
#> cd myapp<br />
#> grails create-pom com.mycompany.groupid<br />
</code></p>
<p>Within the application&#8217;s base directory, open its POM file so we can add our plugin as a dependency. Adding plugins as dependencies are just like any other dependency in Maven and include the GAV (groupId/artifactId/version) coordinates.  The important piece to include is the <code>type</code>.  Plugins in Grails are deployed as zip files, so the type should be <code>zip</code>.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.mycompany.groupid<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>my-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>0.1-SNAPSHOT<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>zip<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>The groupId and version should obviously match what was deployed/installed for the plugin.  You may also set the scope to compile or runtime depending on how the plugin is needed.  Otherwise, at this point, you should be able to build the application again.</p>
<p><code><br />
#> mvn clean package<br />
</code></p>
<p>If that runs successfully, you will notice that the my-plugin plugin is installed in the plugins subdirectory.  This means the plugin was properly installed into the application.  You now have a working Grails application, plugin included, built completely with Maven.</p>
<p>But not so fast&#8230;.</p>
<p>If you recall, we had to ensure that the version number was the same in both the POM and the plugin descriptor.  This becomes a problem if you are using the Maven release plugin.  The Maven release plugin automatically configures the version and re-builds the project to release it accordingly.  This will cause a validation error since the plugin descriptor will still be expressed as a <code>SNAPSHOT</code>, but the POM will be a release build without <code>SNAPSHOT</code>.  Similar, when the release plugin advances to the next development version, the plugin descriptor will still be off.</p>
<p>The solution to this, or at least my solution unless others have better ones, is to use search and replace in Maven to automatically replace the versions in the plugin descriptor with those in the POM.  There are a few different replacement options in Maven, but the one I&#8217;m using is the <a href="http://code.google.com/p/maven-replacer-plugin/" target="_blank">maven-replacer-plugin</a>.</p>
<p>The best way I thought of doing search and replace was doing a prefix and a suffix as comments.  This way you could easily match and replace with a simple regex as well as keep the file compilationly valid.  For example, here is a portion of my plugin descriptor:</p>
<p><code><br />
def version = /* @@VERSION@@ */ "0.1-SNAPSHOT" /* @@VERSION@@ */<br />
</code></p>
<p>We can now use our search and replace plugin in Maven to easily replace that.  You may also want to consider doing this for other properties such as the title, description, SCM path, etc so that you can control them all from a single location in the POM.  Then, using the search and replace plugin, you configure it in this manner:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.google.code.maven-replacer-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>replacer<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.5.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;executions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;execution<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;phase<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>validate<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/phase<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;goals<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;goal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>replace<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/goal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/goals<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/execution<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/executions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;file<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>MyPluginGrailsPlugin.groovy<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/file<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;replacements<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;replacement<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;token<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/\*\s*@@VERSION@@\s*\*/\s*&quot;.*&quot;\s*/\*\s*@@VERSION@@\s*\*/<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/token<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/* @@VERSION@@ */ &quot;${project.version}&quot; /* @@VERSION@@ */<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/replacement<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/replacements<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>You will notice that the phase it its bound to is validation.  However, that is also the phase that the validation of the plugin for the Grails Maven plugin occurs in.  Unfortunately, there is no way (that I&#8217;m aware of) to selectively order plugin execution when two goals are in the same phase.  My solution is to run the replacement in validate and then change the validation plugin to run in initialize, which occurs right after validate.  For example, change the Grails Maven configuration to the following.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.grails<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>grails-maven-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${grails.version}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fork<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>true<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/fork<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;extensions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>true<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/extensions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #808080; font-style: italic;">&lt;!-- RUN VALIDATION IN INITIALIZE PHASE SO REPLACEMENT PLUGIN</span>
<span style="color: #808080; font-style: italic;">         RUNS PROPERLY IN VALIDATE PHASE --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;executions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;execution<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>default-validate-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;phase<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>initialize<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/phase<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;goals<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;goal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>validate-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/goal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/goals<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/execution<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/executions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>You should now be able to change the version in the POM and run <code>mvn clean package</code> and have it update the plugin descriptor and successfully build it.  The last step, similar to my last article, is to perform an <code>scm:checkin</code> during <code>preparationGoals</code> and <code>completionGoals</code> of the release plugin so that the plugin descriptor is committed to source along with the associated POM.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>maven-release-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2.3.2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;preparationGoals<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>clean verify scm:checkin -Dmessage=&quot;perform release&quot;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/preparationGoals<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;completionGoals<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>validate scm:checkin -Dmessage=&quot;prepare for next release&quot;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/completionGoals<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>You need both preparation and completion goals so that you both commit the updates during the release and after the release and upon configuring the next version.  Note that this may unfortunately lead to 4 checkins per release (2 for the release plugin, 2 from this configuration).  </p>
<p>You can now build, package, release, and deploy the plugin purely using Maven.  You will most likely want to include the above configuration in a parent or super-POM so that all Grails projects can inherit from it to reduce the configuration and management of each Grails project.</p>
<p>Being this is the first release of the pure Maven integration into Grails, I do have the following feature requests for future updates:</p>
<ol>
<li>The <code>create-pom</code> script should detect a plugin and set packaging to grails-plugin</code></li>
<li>The <code>create-pom</code> script should consider changing the version to 0.1-SNAPSHOT, but then it would differ from what is in the plugin descriptor by default, so this may not be a good idea</li>
<li>The <code>create-pom</code> script should set the description to an empty string rather than null or the version number</li>
</ol>
<p>My next article will touch on the multi-module support in Maven and Grails and how to build the plugins and applications as a single build.</p>
<p>Other Articles in this Series:</p>
<ol>
<li><a href="http://www.znetdevelopment.com/blogs/2012/07/11/grails-2-1-and-maven-integration-simple-project/" title="Grails 2.1 and Maven Integration: Simple Project">Grails 2.1 and Maven Integration: Simple Project</a></li>
<li>Grails 2.1 and Maven Integration: Plugins</li>
<li><a href="http://www.znetdevelopment.com/blogs/2012/07/15/grails-2-1-and-maven-integration-multi-module-projects/" title="Grails 2.1 and Maven Integration: Multi-Module Projects">Grails 2.1 and Maven Integration: Multi-Module Projects</a></li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.znetdevelopment.com/blogs/2012/07/13/grails-2-1-and-maven-integration-plugins/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
