| By Murali Kashaboina, Geeth Narayanan | Article Rating: |
|
| July 19, 2007 01:15 PM EDT | Reads: |
26,274 |
In part one of this article we discussed the core competencies of Maven and how it fits into application development and build management. Now we'll delve into some of the practicalities in exercising Maven using a practical example that will walk you through some of the common tasks encountered in typical project development and how they can be done using Maven.
Downloading & Installing Maven
The latest version of Maven 2 can be downloaded at http://maven.apache.org/download.html, Maven's official site. When this article was written, Maven 2.0.6 was the latest version available. It can be downloaded and installed following these steps:
1. Download the appropriate binary file
2. Unarchive the binary file and install it in a local directory
3. Add a maven2InstallationDirectory/bin directory to the system PATH variable
4. By default, Maven 2 uses ~/.m2/repository as its local repository directory. In case the local repository should point to some other directory, edit maven2InstallationDirectory/conf/settings.xml, uncomment the <localRepository> element and set its value to point to the path location of the new repository directory. For example, <localRepository>/home/mavenuser/m2_repository<local-Repository>
5. Run the command mvn -help at a command prompt to verify that the installation was successful. Information as shown in Figure 1 should be displayed.
Installing the Maven 2 Plug-in for Eclipse IDE
We'll use Eclipse to demonstrate the example application. The first thing to do would be to install the Maven 2 plug-in for Eclipse so that Maven tasks can be done from inside Eclipse. The benefits of using this plug-in are, as listed on the plug-in's official site (http://m2eclipse.codehaus.org/):
- Launching Maven builds from within Eclipse
- Dependency management for the Eclipse build path is based on Maven's pom.xml
- Resolving Maven dependencies from the Eclipse workspace without installing to a local Maven repository
- Automatically downloading the required dependencies from the remote Maven repositories
- Wizards for creating new Maven projects, pom.xml, or enabling Maven support on plain Java project
- Quick search for dependencies in Maven remote repositories
- Quick fixes in the Java editor for looking up required dependencies/jars by the class or package name
1) On the Eclipse file menu, click on Help > Software Updates > Find and Install as shown in Figure 2.
2) The install and update window will open. Select the option Select for new features to install as shown in Figure 3. Click on the Next button.
3) The update sites window will be opened as shown in Figure 4. Click on the New Remote Site button.
4) The new update site dialog window will be opened as shown in Figure 5 and Figure 6. Enter Maven-Eclipse Plugin in the name textbox and http://m2eclipse.codehaus.org/update/ in the URL textbox. Click OK on the New Update Site dialog window. This will add the new site to the list of sites in the Update sites window. Click the Finish button.
5) The search results window with the Maven Plugin Installation option will be opened as shown in Figure 7. Select the plug-in option and click Next.
6) The Maven 2 plug-in license agreement window will open. Review and accept it and click the Next button (see Figure 8).
7) The installation verification window will be opened as shown in Figure 9. Click the Finish button.
8) The Eclipse download manager will start downloading and installing the Maven 2 plug-in from the specified site. Once the installation is complete, it will prompt you to restart Eclipse. The installation of the Maven 2
plug-in for Eclipse will complete and Maven will be ready to be used after restarting.
Hands-on Example
In this article we'll make an attempt to provide an example that will walk you through some of the common build tasks encountered in typical project development and how they can be executed using Maven.
Most J2EE developers might have been involved in creating and deploying J2EE deployable units such as JARs, WARs, RARs, and EARs. In our example, we considered a J2EE scenario that contains a simple Web application that gets and displays employee details using employee ID. Even though the use case is fairly simple, to illustrate a few of the cornucopia of features available in Maven, we added some complexity by including other components such as EJB, RAR, and an XSD-bound data model that are typically encountered in most J2EE-based applications. The sample application, known as "EmployeeInfo," consists of the following modules:
• The xmlbinding module contains an XSD schema that is used to generate XML-bound Java classes using Castor. This is another concept that had been widely used in Java projects lately that we thought would be useful to illustrate. This module generates a JAR artifact containing XML-bound Java classes.
• The connector module contains a JCA adapter that fetches employee details. This module generates a RAR artifact and uses XML objects interally to represent employee information. Of course, it returns hard-coded values for the details, since the purpose of this example is to illustrate Maven's use in different developmental scenarios.
• The ejb module contains a GetEmployeeDetailsEJB stateless session bean. This module generates a EJB JAR artifact and internally invokes a JCA connector to retrieve an employee info XML object.
• The web contains the JSPs that take in the employee ID user input and displays employee details by calling a stateless session EJB. This module generates a War artifact containing the web application.
• The ear module constitutes the enterprise application that contains the Web application, enterprise java beans, connector, and other dependencies as needed.
In this installment of the article, we will explain how to set up the top level project directory, the 'xmlBinding' module directory structure and how to execute Ant tasks during Maven build process. The rest of the module details along with examples of typical developmental tasks that can be easily accomplished using Maven will be elaborated in the final installment of the article to be published in the next issue of JDJ.
Using Maven Archetypes To Create a Top-Level Project Directory
Archetype is a project templating toolkit. There are built-in Archetypes in Maven that will let developers quickly create a standard project directory layout based on project type. For example, there are Archetypes to create WAR projects and Maven plug-in projects. Table 1 shows some common Archetypes used with Maven. There's a comprehensive list of Archetypes at http://docs.codehaus.org/display/MAVENUSER/Archetypes+List.
Archetype by itself is a Maven plug-in component with typical Maven artifact coordinates. Unlike regular Maven plug-ins that run during Maven build lifecycle phases, An Archetype plug-in gets executed when a Maven project is created. Using Maven Archetypes in creating a project directory structure inculcates consistency in the way project directories are laid out and saves time deciding what the project structure should be.
Published July 19, 2007 Reads 26,274
Copyright © 2007 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Murali Kashaboina
Murali Kashaboina is a lead architect at Ecommerce Technology, United Airlines, Inc. He has more than 10 years of enterprise software development experience utilizing a broad range of technologies, including JEE, CORBA, Tuxedo, and Web services. Murali previously published articles in WLDJ and SilverStream Developer Center. He has master’s degree in mechanical engineering from the University of Dayton, Ohio.
More Stories By Geeth Narayanan
Geeth Narayanan is a senior architect at Ecommerce Technology, United Airlines, Inc. He has 10 years of experience in the IT industry, specializing in solutions using Java EE technologies. Geeth has master's degree in electrical engineering from the University of Toledo, Ohio.
![]() |
Brad 10/05/07 04:58:49 PM EDT | |||
On page 2 the Figure 11 cuts off the command that is used. I think the command is supposed to be "mvn eclipse:eclipse" to generate Eclipse descriptor. |
||||
![]() |
Nazir Khan 09/27/07 05:37:33 AM EDT | |||
This is an amazing intro to maven 2, it has been very beautifully crafted, which starts telling about the internals of maven and how it works on different scenarios and then taking to practicalities of it. Hats off to the guys who made this wonderful work. |
||||
- Practical Approaches for Optimizing Website Performance
- SQL Anywhere Server and AJAX
- The Difference Between Web Hosting and Cloud Computing
- Ajax in RichFaces 3.3, JSF 2 and RichFaces 4
- Cloud Computing on Gartner's Top 10 List and SYS-CON Events' 2010 Calendar
- IBM Hardware Chief, Intel VC Exec Arrested in Insider Trading Scam
- US Post Office Hops a Ride on NetSuite’s Cloud
- Gang of Four Creates Cloud BI Stack
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- Confessions of a Ulitzer Addict
- AJAX World RIA Conference & Expo Kicks Off in New York City
- An Introduction to Abbot
- What is Web 3.0?
- AJAXWorld RIA Conference & Expo 2009 West: Call for Papers
- Interviewing Java Developers With Tears in My Eyes
- Adobe Enters Cloud Computing with LiveCycle
- REA Is Where RIA Becomes the Norm
- RIAs for Web 3.0 Using the Microsoft Platform
- Practical Approaches for Optimizing Website Performance
- Social Media Terrorists
- Building a Drag-and-Drop Shopping Cart with AJAX
- What Is AJAX?
- Google Maps! AJAX-Style Web Development Using ASP.NET
- Flashback to January 2006: Exclusive SYS-CON.TV Interviews on "OpenAjax Alliance" Announcement
- AJAXWorld Conference & Expo to Take Place October 2-4, 2006, at the Santa Clara Convention Center, California
- AJAX Sponsor Webcasts Are Now Available at AJAXWorld Website
- How and Why AJAX, Not Java, Became the Favored Technology for Rich Internet Applications
- "Real-World AJAX" One-Day Seminar Arrives in Silicon Valley
- AJAXWorld University Announces AJAX Developer Bootcamp
- AJAX Support In JadeLiquid WebRenderer v3.1



































