My cape and sword

Crying matadorIn the Walt Disney short animated movie Ferdinand The Bull the matador never get to show off his cape and sword. Sometimes I feel like the matador and want to show off my cape and sword (i.e. my programming skills), but for various reasons I can’t. The bull (i.e. the task at hand) might be too big, too small, or not even there…

Right now I’m pretty confident in writing custom Ant tasks and using HttpClient. It’s a pretty small bull, but not too bad.

 

The case of the failing ant copy filterset

I wanted to set a couple of Windows environment variables from Ant. The environment key-value pairs are stored on the HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironment key in the registry. As a starting point I used the registry editor to export all keys. I called the file environment.reg.template and modified it to look something like this:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment]
"FOO"="@FOO"
"BAR"="@[email protected]"

My ant script contains something like this:

<copy file="environment.reg.template" tofile="environment.reg"
    overwrite="true">
  <filterset>
    <filter token="FOO" value="${foo}"/>
    <filter token="BAR" value="${bar}"/>
  </filterset>
</copy>
<exec executable="regedit.exe">
  <arg value="/s"/><!-- "silent" -->
  <arg value="environment.reg"/>
</exec>
 

I ran the ant script and looked in environment.reg. It still looked just like environment.reg.template! Running again obviously didn’t work, but I had to try! đŸ™‚ When did a cut & paste of the above and sent to a colleague he could not reproduce the issue. I thought hard about it and then it dawned on me: the registry editor exported a file in Unicode format. I used notepad to convert it to ANSI and then everything worked fine. Case closed.

Oracle PL/SQL declare, begin, end from Ant

Ant again. We had a file with Oracle PL/SQL statements using declare, begin and end. When using the sql task in Ant, it complained about reaching end-of-file on the line with “declare”. The problem is that Ant assumes that one line is one SQL statement, but it’s not that simple when using declare, begin and end. The solution is to set slash (‘/’) as a statement delimiter. The excellent blog article Executing Oracle PL/SQL from Ant describes the problem and solution in more depth.

Apache Ant is regrettably (?) using XML

I’ve done some Apache Ant build file hacking today and that made me recall that the original author of Ant, James Duncan Davidson, actually regretted using XML as the file format.

Mysteriously missing from today’s world wide web, but fortunately captured by the Wayback Machine, the blog entry Ant and XML is worth reading. The entry is concluded like this:

If I knew then what I knew now, I would have tried using a real scripting language, such as JavaScript via the Rhino component or Python via JPython, with bindings to Java objects which implemented the functionality expressed in todays tasks. Then, there would be a first class way to express logic and we wouldn’t be stuck with XML as a format that is too bulky for the way that people really want to use the tool.

Or maybe I should have just written a simple tree based text format that captured just what was needed to express a project and no more and which would avoid the temptation for people to want to build a Turing complete scripting environment out of Ant build files.

Both of these approaches would have meant more work for me at the time, but the result might have been better for the tens of thousands of people who use and edit Ant build files every day.

Hindsight is always 20/20.

The “real scripting language” part sounds just like Ruby’s build program Rake and “a simple tree based text format” sounds very much like YAML, a format well worth considering for hierarchical data structures.