Wednesday, December 14, 2011

NB CLI Application with Maven

Sometimes you want to harness the power of the NetBeans platform without being worried about a user interface.
Lets’ see how to do it with Maven

Select the Create Module Project item


In the application module (the orange module icon)
Change the dependencies to


        
            ${project.groupId}
            NbCli-branding
            ${project.version}
                
        
            ${project.groupId}
            NbCli-sample
            ${project.version}
        
        
            org.netbeans.modules
            org-netbeans-bootstrap
            ${netbeans.version}
            nbm
        
        
            org.netbeans.api
            org-openide-filesystems
            ${netbeans.version}
            nbm
        
        
            org.netbeans.api
            org-openide-util-lookup
            ${netbeans.version}
            nbm
        
        
            org.netbeans.modules
            org-netbeans-core-startup
            ${netbeans.version}
            nbm
        
        
            org.netbeans.api
            org-openide-modules
            RELEASE701
            nbm
        
     

Change the dependencies section of your NbCli-Sample to the following


        
            org.netbeans.api
            org-netbeans-api-annotations-common
            ${netbeans.version}
        
        
            org.netbeans.api
            org-netbeans-modules-sendopts
            ${netbeans.version}
            nbm
        
        
            org.netbeans.api
            org-openide-modules
            ${netbeans.version}
        
        
            org.netbeans.api
            org-openide-util
            ${netbeans.version}
        
        
            org-openide-util-lookup
            org.netbeans.api
            ${netbeans.version}
        
    

Although it seems like we’re adding more dependencies we’re actually putting the platform on a diet and getting rid of all the User Interface related stuff that we don’t need.

In order to give your command line application a static main you need to use an Installer / Activator




The restored() becomes your public static void main

package com.mycompany.nbcli;

import org.openide.LifecycleManager;
import org.openide.modules.ModuleInstall;
import org.openide.util.Exceptions;

public class Installer extends ModuleInstall {

    @Override
    public void restored() {
        try {
            System.out.println("My NetBeans CLI application has started");
            Thread.sleep(5000);
            System.out.println("Ok we've done all our super duper hard work. Lets exit");
            LifecycleManager.getDefault().exit();
        } catch (InterruptedException ex) {
            Exceptions.printStackTrace(ex);
        }
    }
}

However there is still one problem...


Splashscreens aren't very CLI, so lets remove it.

Go to your application module and add the following snippet to the POM

        
            
                org.codehaus.mojo
                nbm-maven-plugin
                
                    --nosplash
                
            
        
    

No comments:

Post a Comment