Contents

Netbeans and Templates

We are going to see how to use Netbeans templates, trying to stablish the license in spite of current header. It will be done in two easy steps.

Java

First steps.

The first thing to do is to add the license we want. We open the menu: “Tool->Templates”, opening the Template Manager. Only to see how is it, we can open the file “Java->Java Class”. If it has not been edited, it is something like:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<#assign licenseFirst = "/*">
<#assign licensePrefix = " * ">
<#assign licenseLast = " */">
<#include "../Licenses/license-${project.license}.txt">

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

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

}

This is the way to express what is going to be written in the file ../Licenses/license-${project.license}.txt as a comment in the header of our files.

But… Where is that file?

Set the license of our proyect

This file depends on our project. As all things that depends on our proyect, it will be in the proyect properties. It’s a pity, but I haven’t found any fast way to find the file, so we can open the file view and open the file nbproject/project.properties or we can open it with any other editor different from Netbeans.

You will see it is an INI file, and adding the next line is enought:

1
project.license=gpl3

You can add any string you like, things like “gpl3”, “apache”, “my_enterprise” (I recommend you not using spaces) etc. But remember the string you type here because we will use it.

Create the license file.

The license file is another template, so we come back to templates: ../Licenses/license-${project.license}.txt, and create the file license-${project.license}.txt inside the folder Licenses. Remember that Netbeans will substitute ${project.license} by the value we wrote in the previous point.

Final

So it seems a difficult way, it has some advantages. We can create the templates we use usually: GPL, Apache, our enterprise, etc. And when they are created, we indicate which one to use in each proyect. So simple. By the way we will have modified a lot of templates at the same time (that is the reason to not editing the template) and we will find that every new file has the license.

Of course, we can use variables in the license file that are stablished in the file nbproject/project.properties or the environment ones, such as ${user}, that will be substituted by the user name who is editing the file or ${name} with the name of the file without its extension, or any other variable on our proyect.

Good luck!