Registering FreeMarker Templates in NetBeans IDE 7.1

There's a simple way to create a new module providing file templates such as "Simple Java File" and "Complex Java File" shown below:

Here's the module structure providing the above two file templates:

The "package-info.java" class (which cannot be created by the New Java Class wizard because that doesn't accept hyphens, so I had to use the New Empty File wizard instead) has this content:

@TemplateRegistrations({
 
    @TemplateRegistration(folder = "Classes",
    displayName = "#SimpleTemplate_displayName",
    content = "simple/simplefile.java.template",
    description="simple/simplefile-desc.html",
    position=10,
    scriptEngine = "freemarker"),
 
    @TemplateRegistration(folder = "Classes",
    displayName = "#ComplexTemplate_displayName",
    content = "complex/complexfile.java.template",
    description="complex/complexfile-desc.html",
    position=15,
    scriptEngine = "freemarker")
 
})

@Messages({
    "SimpleTemplate_displayName=Simple Java file",
    "ComplexTemplate_displayName=Complex Java file"})

package org.fm.file;

import org.netbeans.api.templates.TemplateRegistration;
import org.netbeans.api.templates.TemplateRegistrations;
import org.openide.util.NbBundle.Messages;

As you can see, the class has no class signature and no body. It is an annotated package declaration, as discussed here.

Note that the "scriptEngine" variable has been set to "freemarker", meaning I can define templates with this kind of content:

<#assign licenseFirst = "/*">
<#assign licensePrefix = " * ">
<#assign licenseLast = " */">
<#include "../Licenses/license-${project.license}.txt">

<#if package?? && package != "">
package ${package};

</#if>
/**
 *
 * @author ${user}
 */
public class ${name} {

}

When I use the template registered above, the Java class below is generated:

 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication12;

/**
 *
 * @author geertjan
 */
public class newsimplefile {
 
}

Related tutorials:

One thing missing is a way to register your own iterator for creating the template.

Comments:

Do not use the "Java Class" wizard for making package-info.java; there is a separate wizard "Java Package Info".

It would be customary to put the registration in the same package as the actual template and description (e.g. org.fm.file.complex).

Registering an iterator is easy - just put the annotation on the class implementing InstantiatingIterator, and omit the content attribute.

Posted by Jesse Glick on December 12, 2011 at 10:56 PM PST #

I need to be adequately trained on net beans. How can that be possible because i live in Nigeria. I am sincerely serious not a spam please get in torch with me
Thanks.

Posted by guest on December 12, 2011 at 11:27 PM PST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today