Tuesday, 11 September 2012

5 easy steps to writing a Netbeans 7.x plugin

I was fed up with configuring one of the tools that we use at work. It's a data migration tool that takes data from a legacy data store and migrates it to a more modern RDBMS. Now this tool reads a whole bunch of properties from a properties file, and then uses them to configure the migration. This doesn't sound so bad on paper. The problem appears when a user has to work with multiple projects frequently. Each project requires a separate set of configuration (ie. the source/target dbs, along with a whole slew of other configurations specific for that project.) This properties file gets passed around, with people moving the keys within the file between runs. This presents a problem when someone is trying to find the differences for two given .properties files. The standard diff tool doesn't really know to sort the properties files by key first, then remove the any comments before actually performing the diff.

I was so fed up that I decided to make my own plugin in Netbeans to help with diffing two .properties files. In this post I will describe how I did it. Feel free to look at the source code locate at https://github.com/tommytcchan/properties-file-differ-netbeans

*UPDATE* - The plugin is approved and live. Check it out at: http://plugins.netbeans.org/plugin/44324/?show=true

Step 1: Create the Netbeans project.


First open Netbeans (version 7.2 as of this writing). Then click File -> New Project -> Netbeans Module -> Module. Follow the steps to create the project.

Step 2: Create the Action for someone to click to initialize the diff process.


Right click Source Packages -> New -> Action. Follow the steps to create the project. I won't go into details because this page explains it pretty well.

Step 3: Implement the logic for the diff process.


The logic for this is pretty simple. When someone clicks on the properties diff button, we pop up a file chooser dialog for the user to select the left file to compare. After they have selected a file, then we display another file chooser for the right file. After we have the two input files, we then read in the properties file, sort them by key, and then pass it into the Diff api.

I won't show the code here: if you are interested in the code, please go to project src repo:

https://github.com/tommytcchan/properties-file-differ-netbeans

One gotcha here that took me half an hour to figure out was how to include and use the various Netbeans api. To include the ones you need, you will want to right click on the project root -> Properties -> Libraries -> Module Dependencies -> Add.. and add the ones you want to use.

Step 4: Deploy the plugin so a NPM file is created.


For this part, I needed to set up the signing process before the system can build the signed NPM file.

Create a .keystore file by doing the following in a command line:


keytool -genkey -alias propertiesDiff -keypass password1

Create a LICENSE file in your project root that contains the license type of your project.


In your Module Manifest (aka manifest.mf), add in the 'OpenIDE-Module-Short-Description' key
In your Project Properties (aka proeject.properties), add in the following keys (obviously changing the values to suit your project).


keystore=~/.keystore
license.file=LICENSE
nbm.homepage=https://github.com/tommytcchan/properties-file-differ-netbeans
nbm.module.author=Tommy Chan
storepass=password1
nbm_alias=propertiesDiff

Finally, right click on the project root and click on Create NBM. In will build a *.nbm file in your `build` directory.


Step 5: Upload the plugin to the Netbeans repository.


The final step involves uploading the plugin at the Netbeans site. It's pretty self-explanatory, so I won't go into details. Go to the following url to start the process.

http://plugins.netbeans.org/plugin-publish-step1

That's it! It's pretty easy once you get the steps. I did find myself scratching my head on occasions, and searching via Google doesn't usually give me much. I hope this how-to helps somebody out there.

No comments:

Post a Comment