Monthly Archives: September 2007

Using file templates in NetBeans 6.0: Customizing author in generated files

Whenever you ask NetBeans to create a new file (such as Java class or interface or an XML file) for you, it internally uses respective file template to generate the file content for you. This is particularly useful as it relieves you from a need to write boilerplate text, that may in case of Java files include things like package name, class definition, basic documentation (including author name) or a license text.

If you want to see, how file templates look like, simply select “Tools”->”Templates” from a NetBeans menu. A dialog window opens, where you can browse for a particular file template responsible for the content generated in a certain type of file:

Code Templates Browser Dialog

By selecting a template and clicking on “Open in Editor” button you can view or edit the template in NetBeans Editor window. In this blog I will be working primarily with Java Class template, but modifications covered here are generic and affect other templates as well.

As the blog title suggests, I am going to customize author information that will be generated into all newly created (Java) files. First of all, lets have a look at how Java File template looks like:


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

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


/**
*
* @author ${user}
*/
public class ${name} {

}

As you can see, file template uses a simple scripting language that controls the code generation. Since we want to customize author name, there is a single line interesting for us:

* @author ${user}

Customizing the user name

Bad news first: in NB5.x it was possible to customize the user variable by string table via advanced options. This is however not possible in NB6.0 and I have filed a regression issue, so hopefully this will be fixed when NB6.0 is officially released.

Good news is, that there is a workaround. You just need to know that by default, the value of the user property is taken from Java user.name system property. So if you want to customize the author user name generated into the new files, simply customize this property when starting NetBeans with additional -J-Duser.name="Name Surname (name.surname at myemail.com)" command line argument. For instance, my line for starting NetBeans looks like this:

/opt/netbeans-$NB_VERSION/bin/netbeans -J-Duser.name="Marek Potociar (marek.potociar at sun.com)" --userdir $NB_USER_DIR

…as you can see, in my script I am also customizing the version of NetBeans and NetBeans user directory where NetBeans store all user configuration (such as opened projects, window settings, cached classes etc). This makes it easy for me to maintain several different environment configuration which is particularly useful as I am working on more than one project and each projects requires different setup. If you really want to customize only the user name, your startup command may look like this:

/opt/netbeans-6.0/bin/netbeans -J-Duser.name="Marek Potociar (marek.potociar at sun.com)"

Technorati:
,
,