Welcome!

Eclipse Authors: Pat Romanski, Elizabeth White, Liz McMillan, David H Deans, JP Morgenthal

Related Topics: Java IoT

Java IoT: Article

An Introduction to Maven - Part II

How to use Maven in product development

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
The following steps will walk you through the installation process:

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.


More Stories By Murali Kashaboina

Murali Kashaboina leads Enterprise Architecture at United Airlines, Inc. He has 15+ 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.

Comments (2)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


IoT & Smart Cities Stories
BMC has unmatched experience in IT management, supporting 92 of the Forbes Global 100, and earning recognition as an ITSM Gartner Magic Quadrant Leader for five years running. Our solutions offer speed, agility, and efficiency to tackle business challenges in the areas of service management, automation, operations, and the mainframe.
At CloudEXPO Silicon Valley, June 24-26, 2019, Digital Transformation (DX) is a major focus with expanded DevOpsSUMMIT and FinTechEXPO programs within the DXWorldEXPO agenda. Successful transformation requires a laser focus on being data-driven and on using all the tools available that enable transformation if they plan to survive over the long term. A total of 88% of Fortune 500 companies from a generation ago are now out of business. Only 12% still survive. Similar percentages are found throug...
After years of investments and acquisitions, CloudBlue was created with the goal of building the world's only hyperscale digital platform with an increasingly infinite ecosystem and proven go-to-market services. The result? An unmatched platform that helps customers streamline cloud operations, save time and money, and revolutionize their businesses overnight. Today, the platform operates in more than 45 countries and powers more than 200 of the world's largest cloud marketplaces, managing mo...
The platform combines the strengths of Singtel's extensive, intelligent network capabilities with Microsoft's cloud expertise to create a unique solution that sets new standards for IoT applications," said Mr Diomedes Kastanis, Head of IoT at Singtel. "Our solution provides speed, transparency and flexibility, paving the way for a more pervasive use of IoT to accelerate enterprises' digitalisation efforts. AI-powered intelligent connectivity over Microsoft Azure will be the fastest connected pat...
Apptio fuels digital business transformation. Technology leaders use Apptio's machine learning to analyze and plan their technology spend so they can invest in products that increase the speed of business and deliver innovation. With Apptio, they translate raw costs, utilization, and billing data into business-centric views that help their organization optimize spending, plan strategically, and drive digital strategy that funds growth of the business. Technology leaders can gather instant recomm...
At CloudEXPO Silicon Valley, June 24-26, 2019, Digital Transformation (DX) is a major focus with expanded DevOpsSUMMIT and FinTechEXPO programs within the DXWorldEXPO agenda. Successful transformation requires a laser focus on being data-driven and on using all the tools available that enable transformation if they plan to survive over the long term. A total of 88% of Fortune 500 companies from a generation ago are now out of business. Only 12% still survive. Similar percentages are found throug...
As you know, enterprise IT conversation over the past year have often centered upon the open-source Kubernetes container orchestration system. In fact, Kubernetes has emerged as the key technology -- and even primary platform -- of cloud migrations for a wide variety of organizations. Kubernetes is critical to forward-looking enterprises that continue to push their IT infrastructures toward maximum functionality, scalability, and flexibility. As they do so, IT professionals are also embr...
CloudEXPO has been the M&A capital for Cloud companies for more than a decade with memorable acquisition news stories which came out of CloudEXPO expo floor. DevOpsSUMMIT New York faculty member Greg Bledsoe shared his views on IBM's Red Hat acquisition live from NASDAQ floor. Acquisition news was announced during CloudEXPO New York which took place November 12-13, 2019 in New York City.
In an age of borderless networks, security for the cloud and security for the corporate network can no longer be separated. Security teams are now presented with the challenge of monitoring and controlling access to these cloud environments, at the same time that developers quickly spin up new cloud instances and executives push forwards new initiatives. The vulnerabilities created by migration to the cloud, such as misconfigurations and compromised credentials, require that security teams t...
The graph represents a network of 1,329 Twitter users whose recent tweets contained "#DevOps", or who were replied to or mentioned in those tweets, taken from a data set limited to a maximum of 18,000 tweets. The network was obtained from Twitter on Thursday, 10 January 2019 at 23:50 UTC. The tweets in the network were tweeted over the 7-hour, 6-minute period from Thursday, 10 January 2019 at 16:29 UTC to Thursday, 10 January 2019 at 23:36 UTC. Additional tweets that were mentioned in this...