Migrate to Freemarker from Velocity template engine in your NetBeans Module

Posted: September 6, 2007 in NetBeans

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"/>

  • 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
  1. kawazu says:

    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…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s