Not everybody knows that you can specify for some ant targets to run only when some set of files was updated. It is particullary useful when working with GWT or Ivy.

In my GWT projects I usually have tasks like run-shell (which runs a hosted-mode browser) and compile-gwt (which runs the GWT compiler). The run-shell task depends on the compile-gwt task, but this would cause my GWT code to be recompiled every time I run the hosted browser. You may ask - why would you even rerun GWT shell, if you didn’t modify any of the GWT source files? The answer is quite simple - sometimes you don’t make any changes on the client, but only on the server-side and wan’t to see how these changes affect the client. Or maybe you just don’t want to recompile your GWT code every time you need to create the .war file and deploy your application or run ivy:retrieve everytime you run your build script. Anyway, I find this trick pretty usefull.

So how does the magic hapen? The whole trick is possible thanks to Ant’s uptodate core task. Below is the snippet from the example build.xml file:


<uptodate property="gwtBuild.notRequired"
    targetfile=".gwtBuild">
        <srcfiles dir="src"/>
</uptodate>

Uptodate task sets Ant property (here called “gwtBuild.notRequired”) when targetfile is newer (more up-to-date) than any of the files in the srcfiles fileset. Here I’m comparing it to the all directory contents, but srcfiles is a normal ant fileset so you can always use include and exclude tasks within. You can also use mapper subtask instead of targetfile for more complicated use cases.

So now that we know how the magic is done, it’s time to put it to a good use:


<target name="compile-gwt" unless="gwtBuild.notRequired">
    <!-- invoke the GWT compiler here -->
    <touch file=".gwtBuild"/>
</target>

The touch target will create .gwtBuild file if it doesn’t exist or update it’s last modified time otherwise. It should be invoked after the compilation succeeds, bacause if it wasn’t, in case of compilation failure the target would not be executed again unless any of the source files was modified. The “compile-gwt” target will execute only if the gwtBuild.notRequired property is not set thanks to the “unless” attribute.

Now for the finishing touch we need to delete the flag file in our clean task:


<target name="clean">
    <!-- Delete the flag file for compile-gwt -->
    <delete file=".gwtBuild"/>
    <!-- Delete other files and directories -->
</target>

To illustrate usage of this technique I’m attaching to this post a simple GWT project with an Ant script. The project was tested on Windows with GWT 1.5M1 and GWT 1.4.61. To run it simply change the gwt.home property in build.properties file to point to your GWT installation directory.

Click here to download sample project

Hope you’ll find this tip usefull:)

Psyho

Share/Save/Bookmark

Posted in ant, gwt at April 2nd, 2008. by Adam 'Psyho' Pohorecki 1 Comment.

Hi there,

If you read this post, it means that you are either really bored, and have nothing else better to do (my advice: go play some games at One More Level - it’s more interesting), or you are just so very interested in my writing, that you decided to read everything on my blog (in this case I’m really flattered and even more so - surprised). Either way I’d like to welcome you to my blog and hope that you’ll enjoy reading my posts.

As this is my first blog post (ever ;-)), I guess I should write a couple of words about myself. My name is Adam Pohorecki and my friends call me Psyho (BTW: call me Psyho). I’m a CS student from Krakow, Poland. I’m pretty much a geek, and a Google Reader addict (seriously, I couldn’t live without it). As for my professional life, I started a small IT company called DraftSoft with a couple of my friends (don’t try and google us - we don’t have a web page yet). My work revolves mostly around Java-related technologies, but I’m always a sucker for any “new and shiny” programming language, tool or framework - just don’t have time to learn them all. Right now I’m fascinated by Groovy on Grails, GWT and Spring.

In case you were wondering about this post’s title - the thing that everybody elese does is technical blogging. It really seems that everyone has one of those blogs nowadays. However, the other people aren’t the reason why I started this blog - I bought this domain some time ago and just had to do something with it:)

I hope that you’ll find my blog informative - I’ve really started it to discuss my ideas and problems with the community (ah, what a beautiful word ;)) Hopefully in time, I’ll post some tools, tips&tricks etc. that I develop for my own use or just find somewhere on the web. Since English isn’t my native language, I expect to make lots of mistakes in my blog posts. If you ever see any - just let me know. That’s the only way I’ll ever learn:)

To thank you, my Dear Reader, for patiently reading this whole Yet Another Introductory Post, I’d like to reward you with something:

This is the first episode of ICN series “2/8 life” and, guess what? It’s about blogging:) Enjoy, and stick around, there’s more to come.

Psyho

Share/Save/Bookmark

Posted in Uncategorized at March 31st, 2008. by Adam 'Psyho' Pohorecki No Comments.