Tutorial: license headers in NetBeans (reloaded)

An alternative solution to the native NetBeans license header management (see my previous post) is the “License Changer” plugin by nhoffmann available on NetBeans plugin portal (http://plugins.netbeans.org/plugin/17960/license-changer).

The plugin is based on license template concept. NetBeans installation includes several license templates (Tools → Templates) for the most popular Open Source licenses: 2014-02-25_pic01The plugin uses one of the available templates. If you have your own license file, you have to include it in the template list (but the plugin help on this).

NOTE: all custom license files will be saved in the NetBeans User Directory. You can have a look at the beginning of View → IDE Log for your real path.

With the plugin we can do more that native NetBeans solution except for one use-case: when you have a proprietary and project specific license. In this case NetBeans let us save the license file within the project (so all team members can use it) while, with the plugin, all licenses are stored in the user NetBeans installation (which is not shared across the team).

Let’s describe now the plugin.

After plugin installation, you can select a file or a package and apply “Change License Header” (see picture below).

2014-02-25_pic02A wizard starts. On the first page we can select the files types on which the plugin should operate on (see 1). Nice added feature is the possibility to change the line endings character (see 2).



Second page of the wizard is the following:


We can select the license from the template list (see 1) or browse the disk for a custom license file (see 2) which will be included in the template list by the plugin.

IMPORTANT: The checkbox “Set as default project license” (see 3) is very important. If checked, the plugin will save the selected license in the project configuration (the same used by native NetBeans solution). All new created files (including file types not supported by the plugin) will inherit the setting and they will have the correct license header.

After license selection:

2014-02-25_pic05The rest of the wizard is very simple.

NOTE: One little annoying problem with the plugin is that the selected license is not kept for the following plugin invocations.

Typical procedure :

  1. install the plugin
  2. run the plugin on a project file or package or folder,in order to include all existing project files
  3. use standard NetBeans File → New File procedure to create new project files

Tutorial: license headers in NetBeans

NetBeans has special support for the license headers we usually add at the begin of the source files for copyright messages and references to license agreement.

In the project properties we can find a dedicated section to license headers:

2014-02-18_pic01The “Use Global license” options let us choose among popular license header: Apache, GPL, MIT, NetBeans but in this tutorial we will create a custom license header.

First step is to create a file for our project license header. Let’s create a text file on the root of the project which will contain the text of the license header.



In this example, the file is named mylicense.txt and it is placed in the root of the project.

Now we open the mylicense.txt file to add some contents.


The text is free but it should formatted as a comment (in my case I used the Java block comment: /* */ ).

Important: actually the syntax of the license file can be more complex than in our example because it is processed by a tool during the inclusion process. I will post a new tutorial on this topic.

Save the file and close the editor window.

Now we return to the project properties window to specify the path of our project specific license header:


I used the variable ${project.basedir} which is the symbolic path to the project root dir. The window shows the preview of the license header.

Now, when we create a new file (in the example a new Java Class file), we have our template header in place:


For the moment, there is no option in NetBeans to add the template header to an already existing file but I filled an enhancement request on this.