Saturday, June 4, 2011

Some simple parameter passing with XSLT in ANT

There is a simple why to manage environment differences in your build process when managing configuration files by using XSLT in ANT.

Say for example your configuration file contains database connection details. These are likely different per environment (testing, QA, staging, etc). Rather then having many copies of the configuration with the unique values, you can create an XSL template.

Let's call the template "database.xsl". The simple template would look like this:

First, take note of the "xsl:param" values at the top of the file. These declare simple variables and default values that you would like to take place on your template. Second, note the "{$....}" formatting, this sets the parameter to be set in that location on your template.

Next, let's look at the ANT XSLT task:

A couple things here. First the input, output and style. Note the "database.xsl" is the style, this is what we just defined above. This will transform the input and generate the output. The goal is for the output to be generated when your ANT target runs. To pass the runtime build values into the transformation, you can see the "param" entries within the "xslt" block. These can be different per environment.

The result is managing the dynamic variables as seperate verses the entire XML structure, or whatever it is (you can XSLT on text files for example, doesn't matter). In this example the values are hard coded, but you can put them in your file as well.

Why mention if so simple?

I think this is a major plus verses have targeted environment files. It reduces structural updates to the file when there are changes needed. For example, if a new value is added to the configuration, rather than updating several files, you update the template. If it's static, you dont have to transform it, if it is, then it can easily be managed in this way.

Why mention it? I learned this trick about a year ago, not sure how "used" the concept is, so I thought I would share. I didn't invent it, but I like to use it.

No comments:

Share on Twitter