Over a million developers have joined DZone.

Hello World Maven on the NetBeans RCP

· Java Zone

Discover how AppDynamics steps in to upgrade your performance game and prevent your enterprise from these top 10 Java performance problems, brought to you in partnership with AppDynamics.

Let's use Maven to create a simple NetBeans RCP application. We'll create a new application, add a module, and add an Action to it. In the process, you'll learn all the basic things needed to set up Maven applications on the NetBeans RCP.

Do the following:

mvn -DarchetypeVersion=1.5 
-Darchetype.interactive=false
-DgroupId=com.mycompany
-DarchetypeArtifactId=netbeans-platform-app-archetype
-DarchetypeRepository=http://repository.codehaus.org/
-Dversion=1.0-SNAPSHOT
-DarchetypeGroupId=org.codehaus.mojo.archetypes
-Dpackage=com.mycompany.mavenplatformapp
-DartifactId=mavenPlatformApp
--batch-mode org.apache.maven.plugins:maven-archetype-plugin:2.0-alpha-4:generate

Navigate into your brand new "mavenPlatformApp" folder. Then:

mvn reactor:make -Dmake.folders=application

Navigate into your brand new "application" folder. Then:

mvn nbm:run-platform

You now see a splash screen and the NetBeans Platform starts up, with a set of default modules included:

Navigate back to the "mavenPlatformApp" folder. Then:

mvn -DarchetypeVersion=1.4 
-Darchetype.interactive=false
-DgroupId=com.mycompany
-DarchetypeArtifactId=nbm-archetype
-DarchetypeRepository=http://repository.codehaus.org/
-Dversion=1.0-SNAPSHOT
-DarchetypeGroupId=org.codehaus.mojo.archetypes
-Dpackage=com.mycompany.mavenplatformmodulea
-DartifactId=mavenPlatformModuleA
--batch-mode org.apache.maven.plugins:maven-archetype-plugin:2.0-alpha-4:generate

Now look at the source structure generated by the above:

Let's now create a new Action in our module. Go to the "java/com/mycompany/mavenplatformmodulea" directory and add a file named "HelloWorldAction.java" with this content:

package com.mycompany.mavenplatformmodulea;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;

public class HelloWorldAction implements ActionListener {

@Override
public void actionPerformed(ActionEvent e) {
NotifyDescriptor d = new NotifyDescriptor.Message("hello...");
DialogDisplayer.getDefault().notify(d);
}

}

Register the above class in the "layer.xml" file, which is in "resources/com/mycompany/mavenplatformmodulea". In that file, put the following between the <filesystem> tags:

    <folder name="Actions">
<folder name="Window">
<file name="com-mycompany-mavenplatformmodulea-HelloWorldAction.instance">
<attr name="delegate" newvalue="com.mycompany.mavenplatformmodulea.HelloWorldAction"/>
<attr name="displayName" bundlevalue="com.mycompany.mavenplatformmodulea.Bundle#CTL_HelloWorldAction"/>
<attr name="instanceCreate" methodvalue="org.openide.awt.Actions.alwaysEnabled"/>
</file>
</folder>
</folder>
<folder name="Menu">
<folder name="Window">
<file name="HelloWorldAction.shadow">
<attr name="originalFile" stringvalue="Actions/Window/com-mycompany-mavenplatformmodulea-HelloWorldAction.instance"/>
</file>
</folder>
</folder>

In the "Bundle.properties" file found in the same folder as the above, add a key/value pair to set the display name of the Action:

CTL_HelloWorldAction=Hello World

Edit the "pom.xml" in the "mavenPlatformModuleA" folder, by adding the following dependency, since you're using the NetBeans Dialogs API in the Action above:

    <dependency>
<groupId>org.netbeans.api</groupId>
<artifactId>org-openide-dialogs</artifactId>
<version>RELEASE691</version>
</dependency>

Navigate to the "mavenPlatformModuleA" folder and install it as follows:

mvn install

Now let's register the new module into the application. Navigate to the "application" folder and open the "pom.xml" that you find there. Add a dependency in the application on the module:

    <dependency>
<groupId>com.mycompany</groupId>
<artifactId>mavenPlatformModuleA</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>

Navigate back to the "mavenPlatformApp" folder and then build the application:

mvn reactor:make -Dmake.folders=application

Now that the application is built, navigate to the "application" folder and run it:

mvn nbm:run-platform

The application starts up again, this time including your new Action:

Click the menu item and you will see your small dialog with a message.

Your "hello world" Maven application is now complete.

Note: The above instructions are based on the document Creating NetBeans Platform Applications from Maven Archetypes.

 

The Java Zone is brought to you in partnership with AppDynamics. AppDynamics helps you gain the fundamentals behind application performance, and implement best practices so you can proactively analyze and act on performance problems as they arise, and more specifically with your Java applications. Start a Free Trial.

Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}