Eclipse & Ant tidbit

Although this is nothing new to Eclipse, or ant but it is not really used that much in the Coldfusion Development world.

Well at least I rarely have seen anyone using it. One of the things that is built into Eclipse, is ant and being able to build a project. There is many configurations that can come from such a tool, and this is not the place to discuss everything.

After awhile I got a small amount of time to modify my CFEclipse project to allow a more robust structure. This structure is as follows on my development machine.

-Root

--Templates

--Coldfusion Code

--BuildScripts

Now the above could be anything, but I have done it this way to keep the final source code seperate from the generated code by the build script.

The technique I am going to show is how to modify sections of a template on the fly when being built, this allows for version number control, build control for revision and/or version numbering.

So how do we do this.

In the templates directory, I am going to create a file called Application.cfc_tpl. The actual name is not important, what is important is that you can distinguise between the template (tpl) and a normal cfml file.

So what you need to do now is Right Mouse Button (RMB) on the project and select properties, from here we are going to select builders, and select New. From here we are now going to select Ant Builder, to create a build script that can be used when we build our project.

The main thing to change in here is the location to the buildfile, and the base directory. The Build Directory, will be the location of the build.xml file which is the ant controller, and the base directory is the location that is the base to the project. So by using the Browse Workspace tabs, you can then define these settings. With Buildfile, the build.xml file is not created as of yet. But we will place that in the BuildScripts folder.

So, lets look at the Application.cfc_tpl file...

<cfcomponent>
<cfset this.name = 'Test @Version@' />
<cfset this.clientManagement = true />
<cfset this.sessionManagement = true />
<cfset this.setClientCookies = true />
<cfset this.sessionTimeout = createTimeSpan(0, 0, 30, 0) />
<cfset this.applicationTimeout = createTimeSpan(1, 0, 00, 0) />
</cfcomponent>

So what we have done here is used a template to modify a section of our code on the fly, here we have set @Version@ as the section to be replaced. So what we need to do now is add the build.xml file to do this.

<?xml version="1.0" ?>
<project name="ProjectName" default="deployApplication">

<!-- Project directories -->
<property name="Version" value="1.5" />
<property name="TemplateDir" value="./Templates" />
<property name="SrcDir" value="./Coldfusion Code" />

<!-- Targets -->
<target name="deployApplication">
</target>

<!-- Setups the Build and version number for deployment -->
<target name="antTemplates">
<delete file="${SrcDir}/dsp_Welcome.cfm" quiet="true"/>
<buildnumber file="${TemplateDir}/source.number"/>
<copy file="${TemplateDir}/Application.cfc.tpl" tofile="${SrcDir}/Application.cfc" >
<filterchain>
<striplinecomments>
<comment value="!"/>
</striplinecomments>
<replacetokens>
<token key="Version" value="${Version}.${build.number}"/>
</replacetokens>
</filterchain>
</copy>
</target>
</project>

You will notice one other thing in the aboce script, we are referencing another file called source.number in the TemplateDir. This is a system created, maintained file that holds an incremented number. This is also a very simplistic version, but you should get the drift as to what is happening. The other thing to note is that the replace is based on one template here, but you can use wildcards to recreate the template in the srcDir.



  • Jim Priest's Gravatar Good post! I do something similar during my build process. When I checkout my files from SVN I grab the latest version and write that and the date to my application.cfc. I'll add your post to my wiki: www.thecrumb.com/wiki/ant
    # Posted By Jim Priest | 12/27/07 8:14 AM