Tuesday, March 16, 2010

Changing the Default NetBeans License Template

In my previous blog post, I demonstrated how to remove the annoying template instruction comment added to all NetBeans-generated files. Peter Hull pointed out that one could also use this for a license as documented in Geertjan's Blog in the post Project-Level License Settings in NetBeans IDE 6.0. Although I generally don't need to include the license in the source code of software products I work on, this can be a useful feature for those that do.

The easiest way to apply one's own default template for licensing is to simply edit the default one that is used when you create a new NetBeans-generated source file. This is done by using the Template Manager which is accessed in NetBeans via the Tools->Templates menu. This is demonstrated by the next screen snapshot.



One can select the "Default License" and click on the "Open in Editor" button. When this is done, the initial text will include the comment that is all over the web about using of templates. However, the next screen snapshot demonstrates how I have changed its text.



With this template in place, I can generate a new class or interface or other source code artifact to test it out. In this case, I generated a class via NetBeans's class generation wizard. The generated class, with my altered license information, is demonstrated in the next screen snapshot.



The above examples demonstrated changing the default comments included in each generated source file by changing the "Default License" template as opposed as to removing it altogether as I showed in my last blog post. Removing reference to the license altogether or changing the "Default License" are the easiest ways to deal with this.

As Geertjan's post demonstrates, NetBeans has supported the ability to add custom license as well. In the first screen snapshot in this post, there are other licenses already available (such as an Apache 2 license) and one can generate custom licenses. Doing this involves coordination between a property called project.license in the nbproject/project.properties file and the reference to that file in the Templates Manager under the type of source file. For example, the Templates Manager entry for a "Java Class" contains this line:


<#include "../Licenses/license-${project.license}.txt">


As would be expected, the ${project.license} placeholder is replaced by the value that the property project.license is set to in the nbproject/project.properties file.


Caveat

As of this writing, I am having difficulty with custom licenses. When I set the property project.license=dustin and create a license template in the Template Manager named license-dustin.txt to match the format specified in the template for new Java class source code, the comment I get in a generated Java class file is "Error reading included file Templates/Classes/../Licenses/license-dustin.txt". I get this whether I try to select a file from the file system or "Duplicate" an existing license template and "Rename" it. Editing any of the pre-existing license templates provided with NetBeans 6.8 seems to work fine. This is the subject of a NetBeans Forum thread Topic 11600.

There is one significant disadvantage to simply editing the "Default License": this will affect all new source files regardless of project. The advantage of specifying the license file via the project.license property is that this is set on a per-project level. However, as I stated, I cannot get this to work properly at the moment.


Conclusion

I generally prefer to simply turn off the auto-generated comments at the top of my source code files either by removing reference to them altogether or by editing the "Default License" to be empty. However, if one wants a custom comment, such as license information and/or contact information in the top of source files, NetBeans supports this through the license templates.

1 comment:

Peter Hull said...

Yes that feature does seem to be broken. It appears that each entry in the Template manager has a display name and a 'real' file name. You see the display name on screen but project.license uses the file name. What you need to do is right click and call up the license's properties in Template manager, then set the file name to be the same as the display name. Then it should be OK (at least, that worked for me.)