October 13, 2009

Dynamic libraries in NetBeans

I hear often that NetBeans is not as configurable as Eclipse (as a sample). Don't want to start a religious blind war, you'll find nothing like that here.

Regarding this problem my answer is that NetBeans is configurable, but not in a ways as you would expect at the first glance. NetBeans for some things offers a limited GUI to manage things. As a sample, when you create a library. You have a GUI which gives you the possibility to create one. You can build that library only in one way: by selecting individual jars and folders for classes, sources and javadocs (I talk about Java SE libraries). If you want to create a customized library, there's no other GUI possibility.

But you have programming. By offering a friendly and stable API you can extend the platform and IDE as you like. If you know Emacs and Lisp you know what I mean. Is the same philosophy as in Unix/Linux world. No need of GUIs with thousands of screens full of usually unused information. A text file well commented is more than enough. In NetBeans is translated: "You have a very well documented API, stable, samples, why do you need a GUI?".

I will give a 10 minute way to create dynamic libraries in NetBeans. In 10 minutes spent you can do much more than with the best GUI ever.

First, what do I mean by dynamic library. I library which can change in time by your own rules. As a sample, you could want to upgrade the jar files without selecting the new jar files according to the new versions. Or, you would like to have your libraries in different folders (one for development, other for testing machine). Or simply, you know somehow where are your libraries, you have some piece of code to get this information, but you don't want to spent you vacation selecting dozens of jars. You imagination is your limit. With this very simple method you can achieve that in minutes.

First you need to create a NetBeans module project. Call it how do you like, is not important. You do that by File -> New Project -> NetBeans Modules -> Module. After Next, you give a name to your module and can choose Standalone Module. After another next you give the base package, a code name and you have it.
First we will create a class which extends ModuleInstall. This class will be called when the module is started up. In order to do this, we have to add an manifest entry like:

OpenIDE-Module-Install: com/my/package/MyModuleInstall.class

This class has a method called restored(). This method is called when the module is restored during startup. Here we will put our code:

public class ModuleHook extends ModuleInstall {

    public void restored() {
        Map<String, List<URL>> map = new HashMap<String, List<URL>>();
        List<URL> classpath = new ArrayList<URL>();
        map.put("classpath", classpath);
        List<URL> javadoc = new ArrayList<URL>();
        map.put("javadoc", javadoc);

        // fill the URL map as you like, with jars, sources and javadocs
        // .....
        // by the way, you can use FileUtil.urlForArchiveOrDir(File file)
        // to easy get an URL from a File
       
        Library library = libraryManager.getDefault().getLibrary(
               "MyLibrary");
        if (library != null) {
            // remove it if is an old version there
            LibraryManager.getDefault().removeLibrary(library);
        }
       
        LibraryManager.getDefault().createLibrary("j2se", "MyLibrary", map);
    }
}

And that is all. One thing though. You need to add dependencies to some Platform Libraries. In the contextual menu of the project action Properties -> Libraries -> Add. If you don't know what dependency you need simply put the class name in filter field and you will find what you search for.

I consider that NetBeans is a tool for programmers. And programmers don't fear coding. That is why this the best tool to configure your environment. Much flexible and better that a GUI.

Hope you like it.