Over a million developers have joined DZone.

Personalized Code Templates in NetBeans 7.0

· Java Zone

Discover how AppDynamics steps in to upgrade your performance game and prevent your enterprise from these top 10 Java performance problems, brought to you in partnership with AppDynamics.

The NetBeans 7.0 IDE provides several templates for new files created within the IDE.  This tip will demonstrate how you can easily embed your full name and email address into your Java class's Javadoc and also generate a license header comment at the top of your new files -- without editing the individual templates themselves.  This is accomplished by simply setting a couple of properties: 

  • The user property within the NetBeans IDE
  • The project.license property of your NetBeans project(s)

Example Java Class Template

Before we establish the user and project.license properties, let's first take a look at the default Java Class template (below) just to see how these properties are used. You can access (and edit) this template via:

  1. Select Tools | Templates
  2. Browse to Java > Java Class
  3. Select Open in Editor

Note the the placement of the user property in the JavaDoc and the context of project.license property within a filename string:

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

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

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

}

 

User Property

Establishing the user property is simple:

  1. Select Tools | Templates | Settings
  2. Edit the User.properties file to establish the user property: e.g., user=your full name <your email/contact info>

By default the user property in the User.properties file is commented out.  Uncomment and edit this line to provide a pretty string to the template processor.  Note the possibility of adding other properties to this file and then using them in customized templates.

 

Project License Property

Generating a license header in your new files requires that you set the project.license property in your project's project.properties file to one of the files found in the Tools | Templates | Licenses folder.  However, the propery value must be set such that a proper filename string is created by the template processor.  The template processor builds a filename by prefixing "license-" and appending ".txt" to the project.license property. 

So, for example, to use the the license-gpl20.txt template (one of the files found in Tools | Templates | Licenses folder), perform the following steps:

  1. In the Project Explorer, navigate to <Your Project> | Important Files | Project Properties
  2. Add or Edit the project.license property: project.license=gpl20

 (The gpl20 property value is the license-gpl20.txt filename stripped of "license-" prefix and the ".txt" suffix.)

Some other built-in license templates that you can use via this technique include:

  • apache20
  • gpl20
  • gpl30
  • mit

The Java Zone is brought to you in partnership with AppDynamics. AppDynamics helps you gain the fundamentals behind application performance, and implement best practices so you can proactively analyze and act on performance problems as they arise, and more specifically with your Java applications. Start a Free Trial.

Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}