Thursday, May 12, 2011

Improving your Netbeans Java Class Template And Including A License File

Well, working with Netbeans I have a problem occur over and over. One of those annoying programming tasks where you always make the change but are too lazy to deal with underlying problem. Today I decided to address on of them. Every time I create a class in Netbeans, there is a comment that appears at the top:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

My co-workers leave this in place; I am OCD about removing it every time. I don't like four lines of useless code at the top. This morning I decided to change this problem and maybe make my Java class templates a little better. I will walk through improving the class template, and adding the license file for display.

This is a stub class created before the changes:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.something.somewhere;

/**
 *
 * @author GFrick
 */
public class MyClass {
 // class here.    
}

We will start by creating a license file. You just have to create a file "license-company.txt" anywhere on your file system and fill it with your company license info. This can be any information you want in the header of all of your created Java classes. Netbeans has to be told about this file, so we choose Tools->Templates and then go down to the folder "Licenses"; highlight it and click "Add...". Navigate to the text file, choose it and click "Add". Now for any project to use this; simply open the project.properties file and add a line like this:

project.license=company

With this done, your license file should now appear when you create new files in Netbeans.

Now, we will improve the Java class template to include a little better javadoc and information at the top. Go to tools->templates again, find the "Java" folder and the "Java Class" item under it. You will want to copy/paste this to make a backup so you have the original. The original looks like this:

<#assign licenseFirst = "/*">
<#assign licensePrefix = " * ">
<#assign licenseLast = " */">
<#include "../Licenses/license-${project.license}.txt">
<#if package?? && package != "">
package ${package};

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

}

We will add some additional information, namely date and encoding to convenience; the updated version looks like this:

<#assign licenseFirst = "/*">
<#assign licensePrefix = " * ">
<#assign licenseLast = " */">
<#include "../Licenses/license-${project.license}.txt">
<#if package?? && package != "">
package ${package};

/**
* ${nameAndExt} (${encoding})
*
* ${date}
* @author ${user}
*/
public class ${name} {

}

Now when you create a new class, a much nicer version of the template appears and there is no need to remove the top comment. Here is an example class created:

/*
* Software property of Something Somewhere Companies. Copyright 2011.
*/
package com.something.somewhere;

/**
* MyClass.java (UTF-8)
*
* May 12, 2011
* @author gfrick
*/
public class MyClass {
 
}

There are of course plenty of other things you can add to the template. They are written using FreeMarker; so you can script them up to your desire. There are a list of variables here: Netbeans Template Variables

1 comment:

Sahabat MU said...

Informasi yang sangat bermanfaat. Terima kasih untuk admin.
Semoga dapat menambah wawasan Saya dengan topik yang telah admin bahas.
Visit: TEMPLATE BLOGGER PALING KEREN 2015

Create a "GG Button" With an IOT Button on Discord

I was having some fun with my internet button (found here: https://store.particle.io/collections/shields-and-kits), and also working on some...