Over a million developers have joined DZone.

Personalized Code Templates in NetBeans 7.0

· Java Zone

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

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

Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}