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

Comments:

It's also possible to define the user var in the user properties file. Look there : http://blogs.sun.com/geertjan/entry/freemarker_baked_into_netbeans_ide2

Posted by pollux on November 24, 2007 at 01:56 PM CET #

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.

Posted by Rahul on June 02, 2008 at 11:56 PM CEST #

Please Email me if u have some solution to the above problem

Posted by Rahul on June 02, 2008 at 11:58 PM CEST #

Hello Rahul,
please, send your question to the users@netbeans.org alias.

I couldn't send it for you as I don't know your email address.

Thanks,
Marek

Posted by Marek on June 04, 2008 at 03:43 AM CEST #

How to globally change the user name that is used in file templates?
http://wiki.netbeans.org/FaqChangeUserGlobal

Posted by CHEN on June 26, 2008 at 09:15 PM CEST #

465

Posted by guest on November 10, 2008 at 01:27 AM CET #

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?

Posted by Hang on January 01, 2009 at 10:37 PM CET #

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?

Posted by Hang on January 01, 2009 at 10:48 PM CET #

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)\\"'

Posted by Age on February 17, 2009 at 07:43 AM CET #

Thanks, a lot.
Adding it to netbeans.conf works fine for Netbeans 6.7 as well.
Seems like the option is not available from the GUI in 6.7 either...

Posted by misterdom on September 02, 2009 at 03:03 AM CEST #

good

Posted by guest on December 10, 2009 at 12:19 AM CET #

whole day i search to find information about how to declare a method in NetBeans that can add a new data(specially date). I tryed to add new item in database table. all data i can add except date... can you help me guys to create a method to add date from the textfield/ formatter into database table.....! Thank you.

Posted by benjir on December 08, 2010 at 09:08 AM CET #

Benjir, this is not a NetBeans help site :) Please try to send your question to one of the NetBeans mailing lists: http://netbeans.org/community/lists/

Posted by marek.potociar on December 08, 2010 at 09:32 AM CET #

Post a Comment:
  • HTML Syntax: NOT allowed
About

potociar

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today