Tuesday, November 3, 2009

Automated handling of header notices in source files

I recently implemented a web-based application that grew from a proof-of-concept into a real solution for a telecom operator, and it has also been offered to other potential clients. And, since it uses only open technologies such as JSF, Spring and Hibernate, it is pretty easy for other developers to reuse it for demos and prototypes without training on proprietary technologies. It is so that other teams in my company have been asking me to share the source code so they too can use it on their own projects.

While I've worked with few developers in the past who don't quite like the idea of sharing "their" code with other teams, I'm quite proud of this app and have no problems sharing it internally if it could help my company. However, to protect my company’s IP I thought I should at least include copyright messages on every single source code file before sharing it. I know I should have included it from the beginning but when we are working around the clock and we do not have to share the code these kinds of things are easily overlooked. So then I had a project with hundreds of classes and other artifacts without any copyright message. That should be a simple and common task to solve, right? Well, I thought as much... but when looking for a good way to handle it I found that my development tools do not have any functionality I could use to easily handle copyright messages on existing projects.

There are many possible ways to automate this task, from SED-based Unix shell scripts to regular expressions in search/replace functions of more advanced text editors, but I wanted to find something easier to use and more well-integrated to my development environment.

After digging for something in those lines I found a good free solution in an Eclipse plugin called Eclipse Releng Tools plugin, or simply Releng. It can be used to automate the task of including a header notice stating the code’s ownership and/or its license. This article will guide you step by step on how to install, setup and use this Eclipse plugin.

But before we start, a note of warning – YOU should make backups before following these instructions, install and run Releng. It did not cause me any troubles but I should not be held liable in any way if it causes you any problem.


Installation

If you still do not have Eclipse installed, download it here. You should select the distribution package that best suits your specific project, or just pick "Eclipse IDE for Java EE Developers" since it works best for most projects. Note that for Eclipse to run you also need a Java Development Kit – and if you do not have it and do not know how to install it you are not the target audience for this article and should probably not follow its instructions.

Once the download is finished extract the ZIP archive and start Eclipse. It will ask for a workspace folder. You can enter the path where your existing project is, or enter something else if you wish to start a new project or check out from a source code repository system. Click on the Workbench icon to close the welcome screen.

Eclipse's welcome screen


Since the Eclipse Releng Tools plugin is not installed by default in Eclipse we must install it now. In Eclipse’s top menu select “Help” / “Install New Software”.
In the “Work with” tab select “--All Available Sites--”. This will list all the available Eclipse plugins. In the filter bar enter “Releng” to filter the results and show only the plugin we need to install. Check the “Eclipse Releng Tools” plugin and complete the installation process.

Eclipse's available plugins window


After the plugin is installed Eclipse will ask you to restart it. Do that so that it is activated when Eclipse comes back up.

Open your project and note that when you right-click the project root you see the new menu item Fix Copyrights. Do not click on it now; this is just to show that the plugin was installed correctly.

Releng's new menu item



Setup

In Eclipse’s top menu select “Window” / ”Preferences”. In the Preferences window click on Copyright Tool. This will present you with the copyright template.

Releng's default copyright template


Replace the default message with this one below, including the empty lines (one at the top and two at the bottom):



This file contains proprietary information of MyCompany.
Copying or reproduction without prior written approval is prohibited.
Copyright (C) ${date} MyCompany. All rights reserved.




Set the year fields to 2009 or the current year.
Optionally you can enable or disable the copyright messages on properties and XML files. You can also instruct the plugin to replace existing copyright messages with this new template.


Usage

Now that Releng is properly setup, right-click on your project’s root node and select “Fix Copyrights”. All of your source code should now include a header message with your notice, as shown in the image below.

Project processed by Releng

No comments:

Post a Comment