One of the nice things about Rails is how it can automatically spit out common code for you. For example, if you need a new Model or Controller, Rails can spit one out for you, ready to be customized, with one line of code.

I've thought for a while that similar capabilities could be obtained using Apache ANT, which comes built in to Eclipse or can be downloaded separately and run from the command prompt. Last night I had a few minutes so I decided to try it for fun. And it seems to work pretty well!

For example, in a Model-Glue app, I could execute an ANT task called create_controller.xml. What this would do is prompt me for the name of the controller. Say I enter "StoreController". Then it copies the Controller from the Model-Glue application template folder into my application's controller folder and names it StoreController.cfc. Then it embeds new XML into the Model-Glue config XML file like:

<controller name="StoreController" type="deli.controller.Controller">
	<message-listener message="OnRequestStart" function="OnRequestStart" />
	<message-listener message="OnQueueComplete" function="OnQueueComplete" />
	<message-listener message="OnRequestEnd" function="OnRequestEnd" />

into the controllers block. It does this by doing some replace commands on the Model-Glue XML, as well as replacing the controller name and the application name. It does the same sort of replace within the CFC to embed the proper name and path.

I also did this with a create_event.xml file. This prompts me for the name of the event, and a view file name, and then creates the XML for the event, copies a view template into my views folder, and name it to match the view file name I specified.

If you've used the Model-Glue application template to create a new application, you know it already does some copying of files and some replacing. It wouldn't be hard to imagine it also copying and creating a set of build XML files to perform common tasks like this. These build XML files would all be set up specific to the newly generated app, with the proper paths and application name already embedded within them. And obviously the same sort of thing could be done with any framework (ColdBox, Fusebox, Mach-II, etc.)

These two examples were quite simple to create. And while they don't save a huge amount of time, it does save some time. I don't have to worry about creating new view or controller stub files, or creating the base XML in the config file. The build XML will do this for me in about one second. I could imagine making these do much more complex things if necessary, since ANT has a very large set of tasks that it can perform, including RegEx processing and replacement.

I was curious to see if anyone had thought about something like this before, and if this sounds like it would be useful.

Comments Comments (7) | | Digg It! Digg It! | Linking Blogs Linking Blogs | 7558 Views

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)

  • # Posted By Terrence P Ryan | 7/23/07 12:47 PM

    I wonder if you can combine this with the undocumented <CFAnt> tag to make the framework do this on spinup for you.

    Reference for <Cfant>

  • # Posted By Derek P. | 7/23/07 12:55 PM

    I've messed around with ANT before doing things similar to this, I think that this is a great use of ANT and would like to see it actually become more utilized by frameworks. It'd be great if I had ANT tasks builtin to say "create-service.xml" and it would write all the config into ColdSpring, how cool would that be? :)

  • # Posted By Brian | 7/23/07 1:20 PM

    @Derek: That would be quite easy to do, and might be a bit useful, but I'm not sure how much. All it would really be able to do is write the bean definition to the ColdSpring XML file. Since most of the time services depend on other things, you'd still have to go in and manually define the dependencies. I was thinking of the generators more for things that would copy one or more files in addition to adding things to the config files. For generating the actual components you might have a look at my CFC Stub Generator, which will actually write out your service CFCs, as well as unit tests, build XML, and ColdSpring XML.

    @Terrence: interesting idea but I'm a bit leery of relying on an undocumented tag. However, if you go the route of actually using CF to generate the target item, you could actually skip ANT altogether and just have the framework do the generation. For example, imagine in Model-Glue (or any framework), when in development mode I could run a special event like index.cfm?event=generate&item=controller&name=StoreController and the framework itself would copy or create the appropriate files as well as modify the config XML. Heck, maybe even a hidden div could always show up when you're in dev mode that you can open up. It would show a form that contains drop downs and text input fields to let you perform these actions any time you want. I suppose the real issue with all of the generation is how much control is one willing to give up over how things get generated, in exchange for making the generation easy?

  • # Posted By Boyan | 7/23/07 1:27 PM


    I've had something similar in mind especially for Model-Glue apps. It's been on my list to do but I hadn't gotten to it yet. The idea was not just to generate a controller or events but the whole Model-Glue app from the template. This would include other files with the ColdSpring config and the Reactor config. A web interface would be kick ass to have. On top of that, generating new event handlers and refreshing the model-glue app would be awesome. Maybe we can work on something like that together if you are interested.

  • # Posted By Rob Gonda | 7/24/07 12:35 AM

    It's a great idea; similar (or identical) to CairnGen: Ant tasks to generate Caignform classes for Flex (
    I'm actually writing the same for ColdBox ...

  • # Posted By Mark Drew | 7/24/07 7:09 AM

    This gives me a great idea for an addition to the frameworks explorer in CFEclipse...

    *runs of to code madly*

    *thunder, evil laugh, lightening*

  • # Posted By Mark Drew | 7/24/07 7:11 AM

    Also, are you going to share these ant tasks?