The Life and Times of the Layer File

A recent question on the NetBeans Platform Users forum made me think again about the layer file and its current place in the platform. And so I decided to write this post. Note that this post is about the NetBeans Platform, and specifically version 7.1.

The layer file used to be the place where almost all extensions to the NetBeans Platform got registered. So what do I mean by extensions? When you are developing a NetBeans module, some of the typical things you might want to do is create new windows and menu items and toolbar buttons. These all plug into and extend the platform’s window system. The wizards provided by the IDE to simplify the creation of these extensions, created the layer file entries as well as the classes containing the actual functionality. At runtime the platform reads all the information in the layer file and uses those registrations to load the right classes and use them in the appropriate ways.

And then came the annotations. This is another way of representing the same information by applying annotations directly on the classes instead of keeping the registrations in a separate file. As far back as version 6.7 some annotations were introduced. (See Geertjan’s blog post about the annotations implemented back then.) Slowly but surely the layer file entries in the platform were replaced by the much more maintainable annotations. And in version 7.1 the platform is now at the point where pretty much all the extensions are no longer directly based on layer file registrations.

On a side note. I say ‘directly based’ because there is still a layer file in the background. Many (but not all) of the annotations have a source level retention (for example TopComponent.Registration). These annotations are processed at compile time, and one of the possible outputs is a generated layer file. The generated file will appear in the module’s .jar file once it is built, in the META-INF folder and will be called generated-layer.xml.

With the advent of this moment, another small but noticeable change was made. The new module wizard no longer allows the user to choose to create a layer file. It was the logical thing to do, since in almost all cases the layer file would remain empty and having an empty layer file contributing to the file system is inefficient. And for the rare cases where you still need a layer file, there is now a new file wizard that allows you to create one. It can be found at File > New File… under the Module Development category.

Edit: The one case where a layer file is still very useful is hiding menu items, toolbar buttons and other GUI elements provided by the platform. Have a look at this tutorial section for more information about that.

The layer file is dead… long live the layer file!

2 thoughts on “The Life and Times of the Layer File”

  1. Thank you very much for this clarification! I am trying to get an idea about how to create custom file templates for use in the IDE and all tutorials that I have come over is referring to the “the layer.xml file”. You finally confirmed that the layer.xml file is “the old way” of doing things. However, it would be nice to be pointed to an example with explanations about how and where I should use annotations to substitute the functionality of the xml file.

Leave a Comment