Click to search Andy Jarrett.co.uk RSS feed

Loading Twitter

My blog has moved

Please update your bookmarks and feeds for my site.

I now have a Mango Blog at:

http://www.andyjarrett.com/blog

Feed URL: http://feeds.feedburner.com/andyjarrett

Comments Comments (0) | Print Print | Send Send | 1196 Views

Ant: Passing attributes to Build.xml

Critter on a previous post asked "if you can pass in attributes to the build.xml file?" From the Terminal this is as easy as adding another argument "-D".

-D<property>=<value>
This will update a property in you build.xml file with the value you passed in

For example you would use something similar to

view plain print about
1ant -buildfile ~/desktop/build.xml -DechoMsg=NewEchoMessage

Putting this into practice here is a sample build.xml which I am going to save to my Desktop

view plain print about
1<?xml version="1.0" encoding="UTF-8"?>
2<project name="myProject" default="runEcho" basedir=".">
3
4    <property name="echoMsg" value="I am the original message" />
5
6 <target name="runEcho">
7<!-- Simply echo's a message to the screen -->
8 <echo message="${echoMsg}"/>
9 </target>
10</project>

From the Terminal no run

view plain print about
1ant -buildfile ~/desktop/build.xml -DechoMsg='I overwrite the echo message'

In your terminal window you should see something like

view plain print about
1Buildfile: [PATH TO BUILD FILE]build.xml
2
3runEcho:
4[echo] I overwrite the echo message
5
6BUILD SUCCESSFUL
7Total time: 0 seconds

Comments Comments (3) | Print Print | Send Send | 5458 Views

Installing ANT on Windows and or Mac OS X

I'm gonna try and cover the two OS's here so be kind.

First things first, goto http://ant.apache.org/bindownload.cgi and download the appropriate archive (zip) file: apache-ant-[VERSION NUMBER HERE]-bin.zip

Then for Windows: Extract the .zip to c:\ant. From the command line set the following environment variables:

view plain print about
1set ANT_HOME=c:\ant
2set JAVA_HOME=c:\jdk-1.5.0.05
3set PATH=%PATH%;%ANT_HOME%\bin
For Mac: Unpack the zip file to /usr/local/ant/. Then from the command line set the following environment variables:
view plain print about
1export ANT_HOME=/usr/local/ant
2export PATH=${PATH}:${ANT_HOME}/bin

Now open up the Terminal(Mac)/Command line(Win) and just type "ANT" and hit return. You should see:

view plain print about
1Buildfile: build.xml does not exist!
2Build failed

To demonstrate calling a file we're gonna create a quick build.xml. Create a new text file, enter the following bit of XML and save it as build.xml

view plain print about
1<?xml version="1.0" encoding="UTF-8"?>
2<project name="myProject" default="runEcho" basedir=".">
3    <target name="runEcho">
4        <!-- Simply echo's a message to the screen -->
5        <echo message="Is anybody out there"/>
6    </target>
7</project>

If you are on Windows save this file to the root of c:\ and from the command line run the following

view plain print about
1ant -buildfile c:\build.xml

If you are on Mac then just save this to the desktop and from the Terminal run:
view plain print about
1ant -buildfile ~/desktop/build.xml
You should now see
view plain print about
1Buildfile: {PATH_TO_YOU_BUILD_FILE}build.xml
2
3runEcho:
4[echo] Is anybody out there
5
6BUILD SUCCESSFUL
7Total time: 0 seconds

That's pretty much it, from here you you are now set to run Ant from the Terminal/Command line so in theory you should be able to schedule tasks etc!

Comments Comments (15) | Print Print | Send Send | 9013 Views

Ant: Copying files and directories

I know that I've done a post on copying files with Ant, but I really want to take this further and into more detail. To save my fingers a lot of the text below is taken from my first post as moving and copying directories/files is a similar command in principle.

I also want to mention that I am running all my Ant tasks via Eclipse and not the command line. If there is a demand to know how to install Ant and run it from the command line I'll do that post separately. At this point I'm assuming you've got Eclipse and you know you way around it enough? Before we do begin you will need to ensure that you can see the Console 'View', you can get to this by going to:

view plain print about
1Window >> Show View >> Other >> General >> Console

Before we jump in, if you are on Unix then know that the file permissions are not retained when files are copied; they end up with the default UMASK permissions instead. This is down to the current Java runtimes and the inability to set the permissions. You can get around this by using ANT to call Copy Path view the terminal i.e. instead. If you are on Windows and you copy a file to a directory where that file already exists, but with different casing, the copied file takes on the case of the original. The workaround is to delete the file in the destination directory before you copy it.

Lets begin!

Create a new project in your Eclipse workspace. The type of project doesn't matter, though I'm creating a CFEclipse one as I want to cpoy .cfm pages. From within the project create a new file called Build.xml. Create two more folders in your Ant project called 'folder_test' and 'folder_live' and in the first folder put a file called 'index.cfm'. Your project should now look like this

  • build.xml
    • [folder_live]
    • [folder_test]
      • index.cfm
The idea of the next task to copy index.cfm from the test folder to the live one, i'm also going to introduce properties, think of them as variables.
view plain print about
1<?xml version="1.0" encoding="UTF-8"?>
2<project name="myFirstBuildFile" default="" basedir=".">
3    <description>
4        A description of what this build file does
5    </description>    
6    <!-- Relative lo    cation of test folder -->
7    <property name="test" value="folder_test/" />
8    <!-- Relative location of live folder -->
9    <property name="live" value="folder_live/" />
10
11    <!-- copy file -->
12    <copy file="${test}index.cfm" todir="${live}" />    
13    <echo message="The index.cfm file has been copyd" />
14</project>
Now while still in the editor right-click anywhere on your build.xml file and choose the following:
view plain print about
1Run As >> Ant Build
If all has gone well you should of just copied your index.cfm file from test to live. Thats all good and well but I doubt you ever have to copy just one file my guess is that you have a host of folders and files. In the directory 'folder_test' add two other directories called 'images' and 'css' and copy 'index.cfm' back as well. You new directory structure should look like this: (feel free to stick some more files into the newly created folders)
  • build.xml
    • [folder_live]
    • [folder_test]
      • [css]
      • [images]
      • index.cfm

The plan now is copy the complete directory of 'folder_test' to 'folder_live':

view plain print about
1<?xml version="1.0" encoding="UTF-8"?>
2<project name="myFirstBuildFile" default="" basedir=".">
3    <description>
4        A description of what this build file does
5    </description>    
6    <!-- Relative lo    cation of test folder -->
7    <property name="test" value="folder_test/" />
8    <!-- Relative location of live folder -->
9    <property name="live" value="folder_live/" />
10
11    <!--
12        copy directories and files
13
14        From Ant 1.6.3 you can write the following as:
15        <copy file="${test}" tofile="${live}" />
16    -->

17    <copy todir="${live}">
18        <fileset dir="${test}"/>
19    </copy>            
20
21    <echo message="The contest of folder_test have been copyd" />
22</project>
Again while in the editor right-click anywhere on your build.xml file and choose the following:
view plain print about
1Run As >> Ant Build

Of course there is more you can do, rather than going through full set examples here or some of the other features:

Copy a set of files to a directory

view plain print about
1<copy todir="${live}">
2    <fileset dir="${test}">
3        <exclude name="**/*.cfm"/>
4    </fileset>
5</copy>
6
7<copy todir="${live}">
8    <fileset dir="${test}" excludes="**/
*.cfm"
/>

9</copy>

Copy a set of files to a directory, appending .bak to the file name on the fly

view plain print about
1<copy todir="${live}">
2    <fileset dir="${test}"/>
3    <globmapper from="*" to="*.bak"/>
4</copy>

Copy a set of files to a directory, replacing @TITLE@ with Foo Bar in all files.

view plain print about
1<copy todir="${live}">
2    <fileset dir="${test}"/>
3        <filterset>
4        <filter token="TITLE" value="Foo Bar"/>
5    </filterset>
6</copy>

Collect all items from the current CLASSPATH setting into a destination directory, flattening the directory structure.

view plain print about
1<copy todir="${live}" flatten="true">
2    <path>
3        <pathelement path="${test}"/>
4    </path>
5</copy>

Copies some resources to a given directory.

view plain print about
1<copy todir="${live}" flatten="true">
2    <resources>
3        <file file="${test}/index.cfm"/>
4        <url url="http://www.andyjarrett.co.uk/andy/blog/index.cfm"/>
5    </resources>
6</copy>

Copies the two newest resources into a destination directory.

view plain print about
1<copy todir="${live}" flatten="true">
2    <first count="2">
3        <sort>
4            <date xmlns="antlib:org.apache.tools.ant.types.resources.comparators"/>
5            <resources>
6                <file file="${test}/file1.txt"/>
7                <file file="${test}/file2.txt"/>
8                <file file="${test}/file3.txt"/>
9                <url url="http://ant.apache.org/index.html"/>
10            </resources>
11        </sort>
12    </first>
13</copy>

More resources:

Comments Comments (4) | Print Print | Send Send | 20760 Views

Ant: Moving files and directories

First off I wanna say that I'm running all my Ant task via Eclipse and not the command line. If there is a demand to know how to install Ant and run it from the command line I'll do that post separately.

So, what is Ant? Well to steal a quote from Mark Drew, think of Ant as .bat files on steroids. You use XML to describe a set of commands to run a whole range of tasks to do anything from SVN/CVS checkouts, unit tests, FTP, emails, sql, moving/copying folders/files generally just name it!

In this guide I want to cover creating a build.xml file and moving files/folders. I'm assuming you've got Eclipse and you know you way around it enough. Before we do begin you will need to ensure that you can see the Console View.

view plain print about
1Window >> Show View >> Other >> General >> Console

Lets begin!

Create a new project in your Eclipse workspace. The type of project doesn't matter, though I'm creating a CFEclipse one as I want to move .cfm pages around. From within the project create a new file called Build.xml

view plain print about
1right-click on the project folder >> new >> file >> file name (build.xml)
In your build.xml add the following and save:
view plain print about
1<?xml version="1.0" encoding="UTF-8"?>
2    <project name="myFirstBuildFile" default="" basedir=".">
3        <description>
4            A description of what this build file does
5        </description>
6        <!-- Simply echo's a message to the screen -->
7        <echo message="Hello foo, I am the build.xml" />
8    </project>

Now while still in the editor right-click anywhere on your build.xml file and choose the following:

view plain print about
1Run As >> Ant Build
In the console window you should see something like
Buildfile: /[your eclipse work space root]/Ant/build.xml
[echo] Hello foo, I am the build.xml
BUILD SUCCESSFUL
Total time: 136 milliseconds

You've ran your first build.xml file excellent. Now lets move a file. Create two more folders in your Ant project called 'folder_test' and 'folder_live' and in the first folder put a file called 'index.cfm'. Your project should now look like this

  • build.xml
    • [folder_live]
    • [folder_test]
      • index.cfm
The idea of the next task to copy index.cfm from the test folder to the live one, i'm also going to introduce properties, think of them as variables.
view plain print about
1<?xml version="1.0" encoding="UTF-8"?>
2    <project name="myFirstBuildFile" default="" basedir=".">
3        <description>
4            A description of what this build file does
5        </description>    
6        <!-- Relative lo    cation of test folder -->
7        <property name="test" value="folder_test/" />
8        <!-- Relative location of live folder -->
9        <property name="live" value="folder_live/" />
10    
11        <!-- move file -->
12        <move file="${test}index.cfm" todir="${live}" />    
13        <echo message="The index.cfm file has been moved" />
14    </project>
Now while still in the editor right-click anywhere on your build.xml file and choose the following:
view plain print about
1Run As >> Ant Build
If all has gone well you should of just moved your index.cfm file from test to live. Thats all good and well but I doubt you ever have to move just one file my guess is that you have a host of folders and files. In the directory 'folder_test' add two other directories called 'images' and 'css' and move 'index.cfm' back as well. You new directory structure should look like this:
  • build.xml
    • [folder_live]
    • [folder_test]
      • [css]
      • [images]
      • index.cfm

The plan now is move the complete directory of 'folder_test' to 'folder_live':

view plain print about
1<?xml version="1.0" encoding="UTF-8"?>
2<project name="myFirstBuildFile" default="" basedir=".">
3    <description>
4        A description of what this build file does
5    </description>    
6    <!-- Relative lo    cation of test folder -->
7    <property name="test" value="folder_test/" />
8    <!-- Relative location of live folder -->
9    <property name="live" value="folder_live/" />
10
11    <!--
12        move directories and files
13
14        From Ant 1.6.3 you can write the following as:
15        <move file="${test}" tofile="${live}" />
16    -->

17    <move todir="${live}">
18        <fileset dir="${test}"/>
19    </move>            
20
21    <echo message="The contest of folder_test have been moved" />
22
23    <!--
24        You'll notice that when moving the directories Ant deletes the
25        original folder. Because of this we are going to use <mkdir>
26        to re-create the original folder
27    -->
    
28
29 <mkdir dir="${test}" />
30    <echo message="We have recreated the the folder_test directory" />
31
32</project>
Again while in the editor right-click anywhere on your build.xml file and choose the following:
view plain print about
1Run As >> Ant Build

Of course there is more you can do, rather than going through full set examples here or some of the other features:

Move a set of files to a new directory

view plain print about
1<!-- move all .cfm files and leave the scribble file in test -->
2<move todir="${live}">
3 <fileset dir="${test}">
4 <include name="*.cfm"/>
5 <exclude name="scribble.cfm"/>
6 </fileset>
7</move>

Move a list of files to a new directory

view plain print about
1<move todir="${live}">
2 <filelist dir="${test}">
3 <file name="index.cfm"/>
4 <file name="fridays_joke.cfm"/>
5 </filelist>
6</move>

Append ".bak" to the names of all files in a directory.

view plain print about
1<!--- Notice the new parameter here, includeemptydirs -->
2<move todir="${live}" includeemptydirs="false">
3 <fileset dir="${test}">
4 <exclude name="**/*.bak"/>
5 </fileset>
6 <mapper type="glob" from="*" to="*.bak"/>
7</move>

More resources:

Comments Comments (1) | Print Print | Send Send | 8961 Views

Installing Ant on Mac OS X

Just been looking around for tips on installing Ant on the MacBook Pro and came across this. Not only is it a good guide but it takes time to explain the unix commands you are using.

http://www.asceticmonk.com/blog/?p=388

Comments Comments (1) | Print Print | Send Send | 12753 Views

Ant basics: Copying directories

Thanks to Eclipse making Ant so simple to use I've got it doing a few tasks for me. Well I thought I'd show how simple it is to use. Below is a simple Ant build.xml that I use to copy the ModelGlueAppliactionTemplate files over to a new project. In a nutshell it copies a bunch of files and directories from one location into the location the build.xml was ran from.

view plain print about
1<?xml version="1.0" encoding="UTF-8"?>
2<!--
3*
4* Name: is anything you want it to be, for this example its build-mg
5* default: is the first target (method) to run
6* basedir: is the base directory, this can be any local location
7*
8-->

9
10<project name="build-mg" default="deploy" basedir=".">
11
12    
13    <!--
14    *
15    * Description: Describes what the build file does
16    *
17    -->

18    <description>
19 Builds a new ModelGlue project
20 </description>
21
22    
23    <!--
24    *
25    * The property tag is just setting a variable.
26    * name: variable name. You can reference this via ${xxxxx}
27    * value: variable value
28    *
29    -->
    
30 <property name="mgfolder" value="/Path2ModelGlueDirectory/ModelGlue/modelglueapplicationtemplate" />
31
32    
33    <!--
34    *
35    * Target: In effect this is your method
36    * The next set of tags are self explanitory.
37    *    
38    -->

39    <target name="deploy">
40     <copy todir="${basedir}">
41     <fileset dir="${mgfolder}"/>
42     </copy>            
43    </target>
44
45    
46        <!--
47        *
48        * Thats it
49        *
50        -->

51</project>

Comments Comments (3) | Print Print | Send Send | 5903 Views

ColdFusion Meeting Calendar Pod

I've added a new pod to my site which now reads the ColdFusion Google calendar so you can see up coming events.

I've managed to add a couple of events myself including some from the Online CF meetup group, also thanks to Mark Drew as well as he is keeping up the UKCFUG schedule.

If you are an CFUG organiser (or any CF related organiser) leave me a comment here and i'll add you as an administrator so you can add your events. You'll need a Gmail account!

To subscribe to the calendar use one of the following links:

[XML]
[iCal]

Comments Comments (2) | Print Print | Send Send | 2857 Views

Reactor Ant Build

They say Laziness is the mother of all invention, well it strikes again. I've created another Ant build file for use with Doug Hughes Reactor. The build file will download the lastest set of Reactor files the Doug's SVN server, which at the moment has just reached a beta candidate.

The code for build.xml is below.

To run the file from Eclipse just put the file into any project, update line 9 (the <project> tag) with your Reactor folder location, right-click and then select "Run As -- Ant Build".

view plain print about
1<?xml version="1.0" encoding="UTF-8"?>
2
3<!-- =================================
4The basedir in <project> must be
5the root folder you want your reactor
6files extracted to.
7================================== -->

8
9<project name="Reactor_build" default="build" basedir="d:\My Documents\sites\test\">
10 <description>
11 Download the latest Reactor build
12    </description>
13    
14    <!-- =================================
15    Default values including SVN settings
16    Note: the forward slashes for the
17    reactor folder
18    ================================== -->

19    <property name="reactor.Folder" value="reactor" />
20    <property name="reactor.svn" value="http://svn.reactorframework.com/reactor/trunk/reactor"/>
21
22    
23        <!-- =================================
24        A little clean up
25        ================================== -->
        
26
27        <delete dir="${reactor.Folder}" />
28            
29    
30    <target name="build">
31
32        <!-- =================================
33        Check out the code
34        ================================== -->
        
35        <exec executable="svn">
36            <arg line="export ${reactor.svn} ${reactor.Folder}"/>
37        </exec>
38        
39
40    </target>
41
42</project>

Comments Comments (4) | Print Print | Send Send | 2935 Views

Cfeclipse ANT build update

For anyone who is using Ant script to get the CFE nightly builds will need to update the srcURL property.

The srcURL should now be http://cfeclipse.org/nightly/cfeclipse_nightly.zip

Below is the updated code

view plain print about
1<?xml version="1.0" encoding="UTF-8"?>
2<project name="build-nightly-cfe" default="setup" basedir=".">
3 <description>
4 Download the CFE nightly build and extracts to your pluggin folder
5    </description>
6    
7    <!-- =================================
8    Some values
9    ================================== -->

10    <property name="cfePluginFolder" value="d:/program files/eclipse/" />
11 <property name="srcURL" value="http://www.cfeclipse.org/beta/cfeclipse_beta.zip" />
12    
13 <!-- =================================
14 target: setup
15 ================================= -->

16 <target name="setup" depends="init,downloadZip,unzip,cleanDirs"
17 description="-->Sets up your CFeclipse nightly build">

18 <tstamp/>
19 <echo message="Build fininshed at ${DSTAMP}" />
20 </target>
21
22 <!-- - - - - - - - - - - - - - - - - -
23 target: init
24 - - - - - - - - - - - - - - - - - -->

25 <target name="init" description="Build directory structure">
26 <mkdir dir="tmp"/>
27 </target>
28
29
30 <!-- - - - - - - - - - - - - - - - - -
31 target: cleanDirs
32 - - - - - - - - - - - - - - - - - -->

33 <target name="cleanDirs" description="clean up">
34     <delete dir="${basedir}/tmp" />
35 </target>
36
37 <!-- - - - - - - - - - - - - - - - - -
38 target: unzip
39 - - - - - - - - - - - - - - - - - -->

40 <target name="unzip" description="Unzip the file">
41 <unzip src="${basedir}/tmp/cfeclipse_nightly.zip" dest="${cfePluginFolder}" />
42 </target>
43
44 <!-- - - - - - - - - - - - - - - - - -
45 target: downloadZip
46 - - - - - - - - - - - - - - - - - -->

47 <target name="downloadZip" depends="init" description="Download the
48 zip file for ModelGlue"
>

49 <get src="${srcURL}" dest="${basedir}/tmp/cfeclipse_nightly.zip"
50 verbose="true" usetimestamp="true" />

51 </target>
52
53</project>

Comments Comments (6) | Print Print | Send Send | 4426 Views

More Entries

BlogCFC by Raymond Camden + Twitter @AndyJ + ColdFusion jobs + Contact Me + Snippets/Downloads + RSS .