You may have seen that when you create a new class, interface or something with Netbeans, the IDE automatically generates some code for you.
It is possible to customize this code using the Netbeans File Templates.
Under the “Tools” menu there is an option called “Templates”. This dialog let you create and edit every template used in the IDE. Just click on the “Open on Editor” button to edit the desired template inside Netbeans.
Let’s see how the Java/Class template looks like:
<#assign licenseFirst = "/*"> <#assign licensePrefix = " * "> <#assign licenseLast = " */"> <#include "../Licenses/license-${project.license}.txt"> <#if package?? && package != ""> package ${package}; </#if> /** * * @author ${user} */ public class ${name} { }
If you edit this file and save it, the IDE will be using your new template for every new Java class you create.
Since Netbeans 6.0 you can use the FreeMarker template language in your file templates. This way you can add logic to the templates via directives such as if/elseif/else and loop constructs.
You can also use some predefined variables:
- ${date} — Inserts the current date, in this format: Feb 16, 2008
- ${encoding} — Inserts the default encoding, such as: UTF-8
- ${name} — Inserts the name of the file.
- ${nameAndExt} — Inserts the name of the file, together with its extension.
- ${package} — Inserts the name of the package where the file is created.
- ${time} — Inserts the current time, in this format: 7:37:58 PM
- ${user} — Inserts the user name.
This is an example of a modified file template for Java classes:
//////////////////////////////////////////////// /// File: ${nameAndExt} /// Created: ${date} ${time} /// Encoding: ${encoding} //////////////////////////////////////////////// <#assign licenseFirst = "/*"> <#assign licensePrefix = " * "> <#assign licenseLast = " */"> <#include "../Licenses/license-${project.license}.txt"> <#if package?? && != ""> package ${package}; </#if> /** * * @author ${user} */ public class ${name} { }