Migrate to Freemarker from Velocity template engine in your NetBeans Module

In
Portal Pack Plug-ins, earlier Velocity template engine was used for all type of code generations and templating. But after going through some NetBeans doc I found that
NetBeans 6 (Currently under development) bundles
FreeMarker template engine with it. But FreeMarker apis are not exposed directly rather those can be used through JSR 223 Library Integration (
JSR223: Scripting for Java Platform) in NetBeans.
You can follow the below steps for a easy migration from Velocity to Freemarker template engine in your module
- Convert all your velocity templates to equivalent Freemarker templates. There is a command line utility to convert velocity templates to Freemarker templates automatically. For more details click here.
- Change your module's layer.xml as below to mention the "javax.script.ScriptEngine" attributes as "freemarker" for your template.
<folder name="mytemplatefolder">
<folder name="templates">
<file name="mytemplate.java" url="nbresloc:templates/mytemplate.template">
<attr name="position" intvalue="0"/>
<attr name="javax.script.ScriptEngine" stringvalue="freemarker"/>
</file>
</folder>
</folder>
- Add JSR 223 Integration to Module Dependencies
- To get the template FileObject add/use following two methods in your module's code
public static FileObject getTemplateFile(String name) {
FileObject fo = getFolder() != null ? getFolder().getFileObject(name) : null;
return fo;
}
public static FileObject getFolder() {
if (folder == null) {
folder = Repository.getDefault().getDefaultFileSystem().findResource(templateFolder);
}
return folder;
}
In the above example templateFolder value should be "mytemplatefolder/templates"
- Call following method on template FileObject to merge and create the new file
public final DataObject createFromTemplate(DataFolder f, String name, Map parameters)
- You can also directly use the JSR 223 API for template processing and merging
Posted at
02:07AM Sep 07, 2007
by Satya Ranjan in NetBeans |
I like the idea of embracing freemarker as I've been using this package for quite a while now. Now if there only was a NetBeans IDE plugin allowing for custom freemarker template manipulation...
Posted by kawazu on September 12, 2007 at 05:08 PM IST #