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:
,
,

9 thoughts on “Using file templates in NetBeans 6.0: Customizing author in generated files

  1. Rahul

    I am having a problem in using the template feature. It is as follows.

    I have created a jsf page with menus and saved it as Template.
    After that I created a new page from that Template. I got all the design like menus I have created. Till now it is OK.

    After that I felt that menu color should be different so I edited the template and saved and closed it. The page that was created from that template was not changed.

    My requirement is simple I want when I will make change in Template it should reflect in other pages that were derived from that.

    What other I can use to do this functionality in Netbeans6.0. I want the functionality like Master Page in .net

    So that I can change all the pages(master content only) by changing the single master page.

  2. Hang

    What’s the actual scripting language used to edit the template? I want to add the date for the java class template, but do not know the syntax. Can someone give me some guidance?

  3. Hang

    What’s the actual scripting language used to edit the template? I want to add the date for the java class template, but do not know the syntax. Can someone give me some guidance?

  4. Age

    Alternatively, you can add the property to the ‘netbeans.conf’ file located in the ‘etc’ folder. This safes you from having to supply the property when launching Netbeans. To do so:

    – Open the .conf file
    – Locate the ‘netbeans_default_options’ property
    – Add the discussed property to the list of options as ‘-J-Duser.name=\"Marek Potociar (marek.potociar at sun.com)\"’

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>