Geertjan is a DZone Zone Leader and has posted 468 posts at DZone. You can read more from them at their website. View Full User Profile

Hello World Maven on the NetBeans RCP

09.24.2010
| 9964 views |
  • submit to reddit

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.

 

Published at DZone with permission of its author, Geertjan Wielenga.

Comments

Tim Boudreau replied on Sat, 2010/09/25 - 10:49am

Honestly, after using Maven on a daily basis for a few months, my reaction to anybody saying they want to migrate to Maven is the same as if they told me they were going to give all their money and belongings to a cult. That is, I'd try as hard as hard as I could to convince them that they will only regret it.

Maven's library-downloading-and-management ability is the only worthwhile thing about it. And that's easy enough to use programmatically, from an Ant-based, or script-based build system. NetBeans organically grown Ant-based build system may have its warts - but at least they're fixable.

I can see why somebody would want to call Maven from an RCP app's build, and have it be easy to create library-wrapper modules around things available from a Maven repository. That's doable from any sort of build script you want. Everything else Maven does I'd strongly suggest keeping at arms' length. Maven is all about standardizing building software for the benefit of continuous build systems, at the expense of development-time productivity. It will just add overhead and make the experience of developing RCP app - other than the experience of creating library wrappers - worse.

Geertjan Wielenga replied on Sat, 2010/09/25 - 11:32am in response to: Tim Boudreau

Lots of people really prefer Maven over Ant. When using the NetBeans Platform as their Swing application basis, they'd simply like to continue using the things they already like. So that's why they'd like to use Maven in the context of their NetBeans Platform work too. And I'd like to make that as possible as possible.

Mladen Girazovski replied on Mon, 2010/09/27 - 10:49am in response to: Tim Boudreau

Honestly, after using Maven on a daily basis for a few months, my reaction to anybody saying they want to migrate to Maven is the same as if they told me they were going to give all their money and belongings to a cult. That is, I'd try as hard as hard as I could to convince them that they will only regret it. 

Maven's library-downloading-and-management ability is the only worthwhile thing about it. And that's easy enough to use programmatically, from an Ant-based, or script-based build system. NetBeans organically grown Ant-based build system may have its warts - but at least they're fixable.

I can see why somebody would want to call Maven from an RCP app's build, and have it be easy to create library-wrapper modules around things available from a Maven repository. That's doable from any sort of build script you want. Everything else Maven does I'd strongly suggest keeping at arms' length. Maven is all about standardizing building software for the benefit of continuous build systems, at the expense of development-time productivity. It will just add overhead and make the experience of developing RCP app - other than the experience of creating library wrappers - worse.

 Appearantly, your doing something wrong.

I'm working with Maven2 since the beginning 2008 and i do not have these problems, probably your missing someone who knows how to setup a proper maven build.

Currently i'm working on a Eclipse RCP project build by maven, no problems for the developers or the CI Server, it is just hard to get the setup right.

 

Fabrizio Giudici replied on Mon, 2010/09/27 - 1:43pm in response to: Mladen Girazovski

Before moving to Maven 1,5 years ago I thought the same as Tim. Now I'm a satisfied Maven user and I believe my productivity has definitely improved. It could be mostly an artifact management thing, and so it could have been the same with Ant + Ivy, but I strongly appreciate the CI easeness.

It's anyway something on which it's not possible to reach an agreement upon, as Geertjan said, because it depends heavily on one's own perspective. The Good Thing is that NetBeans IDE and Platform support well both systems.

Jose Maria Arranz replied on Tue, 2010/09/28 - 5:06am

Maven + Ant could be a good fit, but as you can read here there is no a serious compromise with Ant as a first class extension of Maven beyond:

"One of the main purposes of this plugin is to facilitate the migration from Ant based projects to Maven 2. Some projects may not currently be able to migrate because they depend on custom build functionality that Maven 2 doesn't provide by default."

Ant in Maven is just a "migration tool" not a necessary and basic extension.

There are other tools like Gradle

"There is no one-size-fits-all process for builds. Therefore Gradle does not impose a rigid process over people. Yet we think finding and describing YOUR process is very important. And so, Gradle has the very best support for describing it."

"We don't believe in tools that save people from themselves. Gradle gives you all the freedom you need."

Yes, it is marketing, but you must recognize is VERY GOOD marketing for developers like me running away of rigid tools making the simple things simpler and complex things much harder.

I don't know very much about Gradle, but according I've seen it is a mix of declarative and imperative programming and supports Maven repositories.  

Tim, Geertjan, you both work or worked for NetBeans (I'm not sure in the case of Tim). Why not is Gradle promoted as a first class building tool in NetBeans? I know that Ivy is the natural extension to Ant but I'm not sure whether Ivy "is the right tool".

 

 

Jesse Glick replied on Fri, 2010/10/08 - 9:58am

First a couple notes on the tutorial:

  1. The above commands probably will not work under Maven 3, due to a tiny but critical bug in nbm-maven-plugin. You can try:

    mvn -DarchetypeVersion=1.6 -DgroupId=com.mycompany -DarchetypeArtifactId=netbeans-platform-app-archetype -Dversion=1.0-SNAPSHOT -DarchetypeGroupId=org.codehaus.mojo.archetypes -DnetbeansVersion=RELEASE691 -Dpackage=com.mycompany.mavenplatformapp -DartifactId=mavenPlatformApp archetype:generate
    

    (This should work starting about a day from now, once the brand-new archetypes get propagated to the central repository. In the meantime you also need: -DarchetypeRepository=https://nexus.codehaus.org/content/repositories/public/)

    For the module you will want -DarchetypeVersion=1.5 as well.

  2. As of NB 7.0 you will be able to use annotations to register the action - no need to edit the XML layer.

To Jose's question about Gradle: yes there are many other build tools out there, with varying features and levels of popularity. All of them are more expressive and convenient than Ant; most use Ant's tasks and data structures to get things done. NetBeans supports Ant directly because it has been around for years and build.xml scripts are widespread, not because we "prefer" it in some way to other tools.

Ivy indeed brings some of Maven's dependency management and download capabilities to Ant builds. What it does not provide - and what other up-and-coming alternatives like Gradle also do not provide - is a declarative project definition that can be read as completely by an IDE inspecting a newly opened project, as by a command-line build tool. When the IDE encounters a Maven source project, it immediately knows how to open it without special tooling - it just reads the POM. You cannot in general guess from looking at an Ant script, or "script" in any language, what the project structure was intended to be, other than by trying to run it to see what happens. This is why the NB IDE must include a dedicated project type for Ant-based NetBeans Platform projects (modules and suites/applications), but can use the regular Maven project type for Maven-based Platform projects. For the same reason, Hudson can set up a job for any Maven project with little configuration, but Ant projects must be set up manually. (Providing dedicated support for particular Maven plugins will often improve the developer experience, but it is usually optional.) Comparing Maven to other "build tools" is thus ignoring a major aspect of why people use it.

Matt Coleman replied on Thu, 2012/11/01 - 1:29am in response to: Jesse Glick

I agree in the issues you posted here Jesse.

buffalo freelance website designer 

Cata Nic replied on Mon, 2013/09/02 - 7:30am

 This is the simplest and the most effective solution I've seen in years. I am happy to be a member of this community.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.