Writing Templates
Writing templates is a lot easier and faster than it might seem. Templates come in two types: those that generate text files with Velocity or XML files, which are more easily generated with Jelly. If you are intuitive and pick a good starting template that does things that you know (or reference around to other templates that are checked in for ideas), you shouldn't be intimidated by not knowing either templating language. Just try it!
There are two quick criteria I use for choosing a template to duplicate.
- Does the template generates one file per item in the metadata or one file containing all of the metadata items found?
- Which template language do I want to use?
Once you have that, then follow these steps:
- Starting with the template you selected, copy the template base class and the template itself to new files (often in a different package).
- Modify the template class. If you made a good match with the two criteria above, you will just have to change the shouldGenerate() method to pick the right items to generate from, but you may need to change the regexp for the filename and other things.
- The implementation of shouldGenerate in AbstractJavaGeneratingBase simply looks at the List of Stereotypes defined, or if none is defined, uses the name of the class itself as as the stereotype it should look for. If you are searching on stereotypes (which is common), get familiar with that code to save yourself lots of time.
- When you are editing templates, create what you want outside of the template wwindow. For instance, if you are generating Java with a syntax-highlighting editor available, edit the code (or output of the last generation run), then copy it into the Velocity template file when you are ready to try a run. (This is less important with Jelly because Jelly script is properly formatted XML.)