Developing Great Software

Thursday, March 26, 2009

Netbeans Maven Projects

Developing Java applications today is a lot like building with Lego blocks. Applications are typically composed of numerous modules and libraries, some of which you write yourself and others which are obtained from open source projects. Relying on open source to provide functionality to your applications offers many advantages. Following the DRY (Don't Repeat Yourself) principle, why recreate the wheel?
  • Feedback on the Web is readily available; just Google the library or functionality you are interested in. Chatter on the web will either confirm your choice or send you looking elsewhere.
  • Bug issues can often be resolved by a quick Google search.
  • Numerous support groups are available for the most popular open source projects.
Incorporating open source into your application brings a unique set of challenges. Many open source projects rely on other open source projects to provide APIs and/or implementations to their products. This is, after all, just another example of the DRY principle. For example, numerous open source libraries incorporate logging but instead of 'hard coding' to a specific logging implementation, such as Log4J, they code to the commons-logging or the Simple Logging Facade for Java (SLF4J) library, which are open source projects which act as a bridge between different logging implementations. This is an example of the Service Provider pattern where an API and possibly abstract classes provide a contract and that contract is fulfilled by numerous implementations. So how does one create applications today when faced with piecing them together from the Lego blocks made up of open source libraries and where the challenge is in resolving all the interdependencies between libraries that exist in such an environment? Trying to manually create a build script and gather all the required libraries and their correct versions has become a monumental task. It would be far better if there were a means of offloading this responsibility from the developer. That is where Maven comes into play. Maven Maven is defined on the Apache Maven project's web site as follows:
Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project's build, reporting and documentation from a central piece of information.
As far as descriptions go, the above is obviously acurate. But what it doesn't describe is how Maven actually resolves the issues of dependency when developing applications in today's open source environment. Imagine that you are a provider of an open source library. If your library is anything other than trivial, you most likely incorporate other open source libraries to satisfy some API, feature or functionality. You test your library and once you are satisfied that your library works as expected you publish it and make it available to the open source community. But you also provide a POM that users of your library can use to incorporate your library into their own Maven based projects. The POM not only 'defines' your library but also defines all the dependencies that your library has on other libraries. You publish your POM on one of the POM repositories so that other developers can discover it and use it to incorporate your library into their applications. Now imagine that you are a developer writing the next killer Web application and you want to incorporate the functionality provided by the open source library mentioned above. Using Maven, how would you do that and how would you do that using the Netbeans IDE? Well, Netbeans provides excellent support for Maven based projects. In Netbeans v6.5, this support comes in the form of a Netbeans plugin. My understanding is that future versions of Netbeans will incorporate Maven directly and will not require a plugin to provide Maven project support. Installing The Netbeans Maven Plugin If you haven't already installed the Netbeans Maven plugin, doing so is easy. Please follow the steps below:
  1. Open Netbeans v6.5.
  2. Select Tools | Plugins from the main menu. This will open up the Plugins window.
  3. Select the Available Plugins tab and type 'Maven' into the search text box which is located above and to the right of the Plugin window's main two pannels.
  4. Select the Maven plugin and click the Install button located beneath and to the left of the Plugin window's main two pannels.
Follow the prompts. When all is completed you will have installed Maven project support into the Netbeans IDE. Creating a Netbeans Maven Based Project I will use Netbeans' Maven project support to create a Web project based on the Apache Wicket framework. To create a Wicket application please follow the steps below:
  1. Open Netbeans v6.5.
  2. Select File | New Project from the main menu. This will open the New Project window.
  3. Select Maven from the Categories pannel and Maven Project from the Projects pannel and click the Next button on the bottom of the New Project window.
  4. In the Maven Archetypes list box select Archetypes from remote Maven Repositories. Notice that when you select this option that Netbeans goes out to the Maven Repositores and discovers all the Archetypes available there and presents these as available options for your selection. In the list of available Archetypes select Wicket Quickstart Archetype as pictured below:
  5. Click the Next button and then click Finish. Netbeans will now create a Maven based project based on your having previously selected the Wicket Quickstart Archetype and when it is done it will open the project in the Project window as pictured below:
You have now created an Apache Wicked Maven based application. Now lets explore some of the advantages that Maven provides to project management. POMs Maven projects in Netbeans do not use ANT build scripts. Instead, it uses a POM that it generates when the project is created. In our case, the POM it generated for our project was from the POM repository and because we selected Wicket Quickstart Archetype the POM it generated is specifc to Maven based Apache Wicket applications. When you expand the Project Files folder in the Projects pane you can see 2 items there as pictured below: If you double click on the pom.xml file, Netbeans will open it in its XML editor as pictured below: POM dependency Tag When viewing the pom.xml file in the editor, scroll down until you see the following:
<!-- WICKET DEPENDENCIES --> <dependency> <groupid>org.apache.wicket</groupid> <artifactid>wicket</artifactid> <version>${wicket.version}</version> </dependency> The dependency tag above declares a project build dependency. In this case, it represents a dependency on Apache Wicket. The groupId and artifactId tags above serve to uniquely identify the target of the dependency, and the version tag above serves to identify the version of the dependency, which in this case is identified by a property declared as follows: <wicket.version>1.4-rc2</wicket.version> Also notice that in the POM file there are numerous other dependency declarations including the following:
  • slf4j - a a simple facade or abstraction for various logging frameworks such as Log4J
  • log4j - the actual logging implementation
  • junit - testing framework
The dependeny declarations in the POM declare all the dependencies for our project and are used when building the project. Building POM Based Projects Go ahead and build the project by right clicking the project node in the Project pane and selecting Build. The build process generates output in the Output window. Go ahead and take a look at the generated output by clicking the Output tab. If you have never installed the Apache Wicket framework and specifically, never installed the specified version declared in the POM, you will notice that all the required jar files were downloaded for you. You didn't have to do anything like going to a web site and downloading the jars yourself. And this is true for all the other dependencies declared in the POM, too. This is one of the great benefits of using Maven; declare a dependency in the POM and when the project is built dependencies that are not already located in in your local or other repository will be downloaded onto your computer. Yes, this can add overhead to the build process but it only occurs when the required dependencies aren't already present in your local respository or some other repository that you can declare. Selecting A Server For Deployment Before we can run our application we have to tell Netbeans what server we want to deploy it on. Right click the project node in the Project folder and select Properties which will open the Project Properties window. Select Run from the Categories pannel which will display numerous runtime options, one of which is the Server option. From the Server drop down menu located on the right panel of the Project Properties window select a server of your choice such as GlassFish V3 Prelude as pictured below: Click OK. When we build and run our application, Netbeans will deploy it to the server we just selected. Running A POM Based Project Now run the application by right clicking the project node in the Project window and selecting Run. You should see the following rendered in your browser window: Summary Using Netbeans and Maven we were able to create a brand new project based on the Wicket Quickstart Archetype we selected. All the dependencies for the project were declared in the application's pom.xml file for us. When building the project, any dependency that couldn't be resolved locally was satisfied by downloading the dependency. This only took a few minutes. Imagine if you had to do all this manually. Netbeans with Maven support has taken the hassle out of working with open source. This will allow you to take better advantage of the many great open source products that are available when you create your next killer application. URLs To Some Of The Items Mentioned Netbeans Maven Wicket

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.

About Me

My photo
New York, NY, United States
Software Developer