Automated copy/paste using ANT

Author: Kasper B. Graversen, 12/12/07
Keywords: ANT, Build script
Abstract: Making a new release can become a cumbersome task, if repetitive information needs be updated as it depends on the build. Ultimately, this can impede your desire to release often or do any new release at all. This guides shows you how to automate these tasks.
subscribe to my RSS feed


Bookmark and Share


Automated copy/paste using ANT

In our time, we have found that certain parts of a release spans not only the application, but also the documentation (whether it be text files, or home pages). In order to maintain a certain level of professionalism, Such information needs be updated upon each release. If this is not automated, you will be reluctant to do frequent releases, or any kind of release at all.

At other times, you have information you have static information you would like to supply in many files such as copyright notices and other lincense information. Such information is very important to have explicit in your code. While this information may change infrequently, it often seems to be "in the way" of what you as a developer really want to do (i.e. read the non-static text ;-).

The problem is two-fold. How do we automate the updation of information that cross-cuts code and documentation, and how do we hide "static information" which ultimately, is only there for the users and to you just seems to obstruct your reading.

Solution Ant Build Script

The solution to both problems is to insert "magic markers" in your files and have these be expanded to text upon compilation before a release. The markers can denote version numbers, copyright notices etc. which then automatically are repleced during the release-build. If your product is open-source and you supply both source code and binary files, it is important to replace the magic markers before compiling the binary files otherwise line numbers reported by the code will not match the released source code.

The "magic markers" can be placed in xml files, html files, source code etc. which ensures that the information is always the same. This is an alternative to using a static final String VERSION = "3.14"; since this information is only available to the source code.

Ant supports a very easy way to search/replace values in files. Magic markers and their corresponding values are defined in a properties file. Here is how you do it
  • Create a properties file with all you key/values
    
    %version=3.14
    %copyrightNotice=This is copyrighted (c) 2007 \n \
     * foo product bla bla \n \
     * more bla
    %thanksTo=John Doe, G. I. Jane
    
    
    magicmarkers.properties
  • Use the magic markers in your files
    
    /*
     * %copyrightNotice
     */
    public class Foo { ... }
    
    
    Foo.java

    
    <html>
    <body>
    <h1>
    Great product v %version</h1>
    ...
    
  • Use the following target in your build for replacing text in only one file
    <property name="source.file" value="configxml.xml" />
    <property name="dest.file" value="out.xml" />
    ...
    <target name="magicReplacerOneFile" depends="">
        <replace file="${dest.file}"  replacefilterfile="magicmarkers.properties" />
    </target>
    
  • Use the following target in your build to replace a bunch of files
    <property name="dir.target" value="output_src" />
    ...
    <target name="replacetester">
        <!-- copy files to another dir in order not to mess up the originals -->
        <delete dir="${dir.target}" failonerror="false"/>
        <mkdir dir="${dir.target}" />
        <copy verbose="false" failonerror="true" todir="${dir.target}">
            <fileset dir="." includes="**/*.java"/>
            <fileset dir="." includes="**/*.html"/>
        </copy>
    
        <!-- replace them files -->
        <replace replacefilterfile="info.properties" dir="${dir.target}" />
    </target>
    
You can also use this technique to build semi-dynamic webpages. This allows you to easily extract reuseable parts of a page and easily change these at one place rather than in many files. For inspiration see e.g. the projects Super CSV or The Spiffy Framework



Comments

If you have any comments to this article, please drop me a mail at firstclassthoughts at gmail dot com please indicate if I can't publish whole or parts of your comment on the site.


If you like this site consider subscribing to my RSS feed or how about subscribing by Email.


Help spread the word

Share this post on your favorite social bookmarking sites:
If you enjoyed this article, found it thought provoking, educative or otherwise good, please link to this page from your page or social bookmarking page. If you have any texts you think are worth publishing on First Class Thoughts, don't hesitate to send me a mail! Quality always welcome.


Bookmark and Share


The most recent contributions
28/07/09 Magic in mathematics II Fun with the number cyclic numbers, and specifically with 142857 as it is the smallest of such numbers.
13/07/09 My top 8 time-saving Firefox shortcuts This article presents my favorite top 8 time-saving shortcuts in Firefox 3.0 and Firefox 3.5. Get to know these and you'll be saving a lot of time. They have been ordered by "the element of most surprise"
20/05/09 Board Game Jungle speed / Arriba Review of the cool game "Jungle Speed" aka. "Arriba".
16/05/09 Danish Twin words "Twin words" are words that not only have multiple meanings, they must be composed next to each other in meaningful sentences. This article explores the concept of twin words.
Nothing of interest? Try browsing the entire article archive...