Library Wrapper the Maven way

(Applies to : Netbeans IDE v7.3 onwards)

If your are working with the Netbeans Platform from within the Netbeans IDE then you may have noticed that when it comes to Maven there’s no project template to create a Netbeans Library Wrapper module.

So what to do ?  The answer is that it is actually quite simple to create this yourself. Just follow these steps :

Suppose we want to wrap some library, say example.jar, into a Netbeans Module. The module we are creating is part of a bigger application called MyWinnerApp.

Create a new Project and choose “Netbeans Module” from the “Maven” category:

In the next step you must choose a Project Name and a Project Location for your new module.  It doesn’t really matter what name you give to your project but I like to prefix it with the application name – especially if I know I will not be using the module outside of the application. Furthermore I make sure that my Project Location is that of my application so that the module’s location is essentially a folder below this location (you can see this in the Project Folder value). When you do it this way the IDE will make your module’s POM inherit from the application’s POM. Also it will add the new module to the application’s parent project.

On the next page you are asked about which Netbeans Platform version to use. If you are really just creating a pure library wrapper (a module that has no source code of its own) then you do not actually have to depend on any specific version of the NetBeans Platform. For the moment just pick the version used in the application (i.e. in MyWinnerApp) and then we can fix that later by hand-editing the generated pom.xml. Then click “Finish”.

When done you should see a project structure like this:

You can now add example.jar as a dependency on MyWinnerApp-ExampleWrapper. That’s it. The module is now a library wrapper.

There’s one more thing you need to do: At the moment the new module doesn’t really expose any classes to the outside world and is therefore pretty useless. Fix this by right-clicking on the MyWinnerApp-ExampleWrapper node and choose Properties. From the Categories pane choose “Public packages” and then select the packages that you want the module to expose to other modules. Most likely the only packages you want to expose are those from example.jar and you may even not want to expose all of them. Your choice !

UPDATE: Note that from 7.3 onwards the “Public packages” window will be empty by default. This has been reported as a bug but seems to be intentional. The bottom line is that you now need to manually edit the pom.xml to include those packages you want to export. This is done via the <publicPackages> parameter in the pom.xml file. In fact all the “Public packages” window did in the past was to edit your pom.xml file.

And I almost forgot: I promised we would go back and hand-edit the POM for our new module. If your Library Wrapper doesn’t actually depend on any NetBeans Platform code but is indeed purely a wrapper for a library then you should delete the dependency that the “New Project –> NetBeans Module” wizard has created for you. There’s no reason to have it.
At the moment the POM for MyWinnerApp-ExampleWrapper has a dependency section like this:

<dependencies>
    ...
    ...
    <dependency>
        <groupId>org.netbeans.api</groupId>
        <artifactId>org-netbeans-api-annotations-common</artifactId>
        <version>RELEASE73</version>
    </dependency>
</dependencies>

Just delete that dependency if your NBM is a pure wrapper. By removing it you are essentially saying that the wrapper module has no dependency on any specific version of the platform.

That’s it ! It would have been nice if there had been a project template to assist you with this but now that there isn’t it is not too hard to do it yourself.

Posted in Uncategorized
5 comments on “Library Wrapper the Maven way
  1. Michael Hatherly says:

    This does not work in versions 7.3 / 7.4 (not sure about 7.2).

    Nothing comes up in the ‘public packages’ section of the module properties. I’ve reverted back to 7.1.2 to create my maven netbeans platform program.

    • petersblogwp says:

      Confirmed. I see the same problem as you.

      Looks like a IDE bug to me. As far as I know all that window does is to edit your pom.xml. So you can still add your public packages manually to the pom.xml.

      Something like this:

                  <plugin>
                      <groupId>org.codehaus.mojo</groupId>
                      <artifactId>nbm-maven-plugin</artifactId>
                      <extensions>true</extensions>
                       <configuration>
                         <publicPackages>
                             <publicPackage>org.apache</publicPackage>
                         </publicPackages>
                      </configuration>
                  </plugin>
      

      Note that after you do this the package(s) you’ve added will now suddenly start to appear in the Public Packages window.

      Personally I wouldn’t revert to NB 7.2 solely for this reason. 🙂

  2. petersblogwp says:

    The problem with the empty Public Packages window has already been reported in BugZilla. The developer says it is intentional.

  3. Matias says:

    hi, i need to wrapp my jar in my netbeans platform project, if you have a guide to add a own jar to my netbeans module i will be very gratefull. thanks

    • petersblogwp says:

      Your question doesn’t relate to Netbeans Platform as such, it relates to Maven. In Maven all jars must reside in a repository somewhere. Typically people in your situation will ‘upload’ the jar to their local Maven repo. If you google for this you can find lots of answers on such a Maven question.

Leave a comment