Wednesday, March 31, 2010

How to create a simple Naked Objects Application in NetBeans with Maven

Despite all Marco's painstaking insistence, I have never been a great fan of Maven. I'm not criticizing it, it's just I'm used with the Ant build system embedded in NetBeans: that's why when I last wrote about Naked Objects in NetBeans I sticked to it.

Partially out of curiosity, partially solicited, I decided to give it a try, using this article as a reference. All procedures refer to version 6.8, other versions might be different (I know for sure 6.7.1 is).

The first thing to do is to create a new project:

Then select Maven from the categories tree and Maven Project from the projects list

In the next screen we have to choose the archetype we want to use; under the Archetypes from Local Repository node select the Naked Objects application archetype (4.0.0):

Complete the form with the informations you find in the aforementioned article...

...and there you go; NetBeans (through Maven, of course) has created one project for each pom file, the main one being highlited:

If you check the file section you'll find the recommended directory structure also quoted in the article. This approach might seem overwhelming at first, and probably overkill for just a simple test, but it actually separates different concerns in different projects.

Now we should execute the package phase, which is not defined in NetBeans yet. To bind a custom action to the phase right click the main project and click on Custom - Goals...

In the Goals textfield you specify what goal you want Maven to execute:

You can also tell NetBeans to remember your instructions; for the most curious, this actually creates a nbactions.xml file in the same directory in which the pom resides.

After Maven has finished his stuff all you have to do is run the application. I'm not sure whether there is a different way to do it, but this one works and it's not too weird: switch to the Files view and locate the startup script, right click on it and select Run from the dropdown menu:

Et voila... here's your login form. Enter username and password...

And there you go!

If you read up to here, I owe you a (reiterated) disclaimer: I am not a Maven expert, nor a Naked Object expert, but this tutorial should help the rubber hit the road.

How does this compare to my previous procedure? Even if it was quite hard to come to an end due to my lack of knowledge of Maven, to which I owe the mixed bag of feelings I have about this, I must admit that someone that is already proficient with it gets a faster start than with the Ant approach. The projects structure generated in NetBeans is more complex, but that is not necessarily a bad thing (actually it is, if you are only trying to write a plain vanilla CRUD application that operates on just a few tables).

Which road should you follow? Right at the moment I am not even able to say which one I should follow, so I won't express myself :-)

A very interesting feature is the possibilty to use customized templates in Eclipse to speed up your development; now I'm looking forward to something like this in NetBeans...

No comments: