Monday 13 August 2007

Managing ant dependencies

We tried several packages to manage ant build dependencies. Antlion looked useful, but the documentation was dreadful and we never worked out if it would do what we wanted or not. Ivy looked useful at first, but it didn't really do the dependency management we wanted. We even looked at Maven. Stone me that is complicated, and even in version 2, too full of bugs to be useful. I wasted two days on that before giving up in despair and returning to ant.

My colleague Jon Siddle decided to write his own ant dependency manager which he has called decorum. It does just enough dependency management -- it doesn't download stuff from ibiblio, but it does know about other ant projects in the same file system and will build them if their targets are out of date. I works very well in a project with many sibling sub-projects. It also builds the path refs for the compile and test targets. And it handles external jars (which we don't usually build) nicely as well.

Here's a simple example: say my project needs log4j. All I need to do it put these lines in my build.xml:

<taskdef name="dependencies" classname="org.trapdoor.decorum.Decorum">
<classpath location="${lib.dir}/decorum-0.2.jar" />
</taskdef>

This tells ant to add Decorum's task definition. Now I can add dependencies entries to my build.xml:
<dependencies id="main" libdir="${lib.dir}" excludes="clean">
<dependency name="log4j" />
</dependencies>
When I use the generated path in my compile target, it will find the most recent version of log4j in the library directory:
<target name="compile" depends="init">
<javac srcdir="${src.dir}/main/java" destdir="${build.dir}/main/classes"
includes="**/*.java" classpathref="path.main">
In this example "path.main" refers back to the id I gave my dependencies.

Here is a complete [FILE REMOVED -- I no longer use decorum] example with minimal decorum dependencies. In my next post I will illustrate some of the more advanced features like version and building.

Thursday 9 August 2007

Making the most of vim: vimproject saves typing

If Java sources are arranged in a hierarchy of directories which reflects their package names, they can end up quite deep down. Opening a different file from vim can mean a lot of typing. I first wrote vimproject in 2001 to get around this. I didn't want an IDE --- real men don't use those. I just wanted something to allow me to open files without a load of typing.

The solution is a small Python script and the vim-python package available on some Linux distributions (Ubuntu, for example). By adding a few lines to ~/.gvimrc and writing a simple XML file, you can get vim to add a menu which allows you to open files quickly, with no typing and without resorting to a huge, slow IDE. Full instructions are on the vimproject page.

vimproject works with vim in GUI mode -- it needs the GUI's menu bar, so it won't work with the non-GUI version. It could probably be done in vim's built-in language, but I don't know that and don't have the time or the inclination to learn it.

Making the most of vim: ant and javac

With a bit of rather tricky configuration it is possible integrate vim, ant and javac (the standard Java compiler).

First, vim allows us to set an external program to be called for the :make command. This goes in ~/.vimrc

set makeprg=ant

Now, when you issue a :make command to vim, it will call ant to build the project. Now with a bit more configuration, we can get vim to parse the error messages from javac and step through them.

Back to ~/.vimrc to tell vim what javac's messages look like:

set efm=\ %#[javac]\ %#%f:%l:%c:%*\\d:%*\\d:\ %t%[%^:]%#:%m,\%A\ %#[javac]\ %f:%l:\ %m,%-Z\ %#[javac]\ %p^,%-C%.%#

Make sure that horrendous code goes on one line. It came from a bit of head scratching and a lot of vim's documentation.

Now, after a :make you can step forward and back through the compiler messages using :cn and :cp.

That is all the IDE I need -- just enough help without getting in the way.

Wednesday 8 August 2007

Using the Presentation Model pattern

I came across the Presentation Model pattern while looking at other libraries at JGoodies. This is an interesting alternative to the usual Model, View, Controller approach to implementing a Java & Swing GUI. It fits better with the way Swing works and JGoodies binding puts it all together beautifully. The pattern is described in detail on Martin Fowler's site.

At work, we are are about to begin coding a pretty large Swing application which will model most of the company's business. We are going to be using JGoodies binding and Presentation Model for this. I wrote a reasonably complex prototype to test out some ideas we are considering using. I used the same prototype to try out Presentation Model and binding. I like it a lot. There is a bit of a learning curve at the start, but this is rewarded with a lot less code to write. In particular, there is less boring, repetitive and error-prone code to write to synchronise the view with the domain model.

Anyone about to implement a Java Swing application should take a look.

Monday 6 August 2007

Configuring Linux WiFi (wireless) without a GUI

I put together a collection of bits to make a small sever this weekend. I wanted to use Debian Etch, but it doesn't support my RaLink RT2561/RT61 wireless card out of the box and the build & install instructions for the driver were hopeless.

I decided to use Ubuntu 7.04 Server instead -- it supports the RaLink chipset out of the box. But configuring the card was a nightmare -- everywhere I looked for help suggested I fire up the network configuration GUI which I don't have as I installed the server version. I almost gave up, but did try a find/grep in /etc to see what I could find. And there it is, plain and simple at the bottom /etc/network/interfaces:

iface ra0 inet dhcp
wireless-essid whisperingwind
wireless-key FFFFFFFFFF

The last line is the WEP key. So all that was needed was to edit this file and insert the appropriate values. Easy when you know.