Replace file content in NAnt task

As JP suggested, I always compile all my layered projects into a huge dll to make NAnt build file eaiser. This solution is perfect until I tried to embeb structuremap config file into cruise control. Because assembly file name changed, I have to find a way to replace the config file content in NAnt.

It was not easy. NAnt doesn’t have a replace task!

I don’t want hack into NAnt source to re-build my own version. It seems Jay Flowers, the creator of CI Factory has his modified version of NAnt which has replace task in it, but, no document to direct me how to use it.

OK, I have to call Ant task in NAnt build file then.

[NAnt build file]
<exec program="c:\ant\bin\ant.bat" basedir="${base.dir} ">
<arg value="-Dcurrentproject.lib=${currentproject.lib}" />

    [Ant file]
    <project name="config" default="replace_config" basedir=".">

    <property name="build.dir" value="${basedir}\build"/>
    <property name="currentproject.lib" value="undefined"/>

    <target name="replace_config">
    <replace file="${build.dir}\StructureMap.config">
    <replacefilter token=",mybizlayer" value=",${currentproject.lib}"  />
    <replacefilter token=",mydatalayer" value=",${currentproject.lib}"  />



4 thoughts on “Replace file content in NAnt task

  1. The following target loads some template sql from a file, replaces some tokens, sets some variables and hands off to a generic restore task. This is used by us to customise database restores for targeted testing environments. It’s all nant.

  2. Try again

    <target name=”restore.db_name”>
    <loadfile file=”${dir.template}\template.db_name.sql” property=”restore.db.sql.db_name”>
    <!– this looks for tokens like @blah.blah@ in the file being loaded and replaces them–>
    <token key=”restore.db.prefix” value=”${restore.db.prefix}” />
    <token key=”backup.file.path” value=”${backup.file.path}” />
    <property name=”current.db” value=”db_name” />
    <property name=”current.log” value=”${dir.log}\${restore.db.logfile.prefix}_db_name.log” />
    <property name=”current.file” value=”${dir.template}\restore.db_name.tmp.sql” />
    <delete if=”${file::exists(current.file)}” file=”${current.file}” />
    <echo file=”${current.file}”>${restore.db.sql.db_name}</echo>
    <call target=”restore.database” />

  3. I didn’t know know to use token in Nant when I post this. Yes, token is very useful for replace file content, and is the standard way to localize dev/deploy environment.

    Thanks Luke.

  4. Pingback: NAnt: No string replace in files for you! « /dev/random

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s