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
René Bostic is the Technical VP of the IBM Cloud Unit in North America. Enjoying her career with IBM during the modern millennial technological era, she is an expert in cloud computing, DevOps and emerging cloud technologies such as Blockchain. Her strengths and core competencies include a proven record of accomplishments in consensus building at all levels to assess, plan, and implement enterprise and cloud computing solutions. René is a member of the Society of Women Engineers (SWE) and a m...
Andrew Keys is Co-Founder of ConsenSys Enterprise. He comes to ConsenSys Enterprise with capital markets, technology and entrepreneurial experience. Previously, he worked for UBS investment bank in equities analysis. Later, he was responsible for the creation and distribution of life settlement products to hedge funds and investment banks. After, he co-founded a revenue cycle management company where he learned about Bitcoin and eventually Ethereal. Andrew's role at ConsenSys Enterprise is a mul...
In his general session at 19th Cloud Expo, Manish Dixit, VP of Product and Engineering at Dice, discussed how Dice leverages data insights and tools to help both tech professionals and recruiters better understand how skills relate to each other and which skills are in high demand using interactive visualizations and salary indicator tools to maximize earning potential. Manish Dixit is VP of Product and Engineering at Dice. As the leader of the Product, Engineering and Data Sciences team at D...
Dynatrace is an application performance management software company with products for the information technology departments and digital business owners of medium and large businesses. Building the Future of Monitoring with Artificial Intelligence. Today we can collect lots and lots of performance data. We build beautiful dashboards and even have fancy query languages to access and transform the data. Still performance data is a secret language only a couple of people understand. The more busine...
Nicolas Fierro is CEO of MIMIR Blockchain Solutions. He is a programmer, technologist, and operations dev who has worked with Ethereum and blockchain since 2014. His knowledge in blockchain dates to when he performed dev ops services to the Ethereum Foundation as one the privileged few developers to work with the original core team in Switzerland.
Whenever a new technology hits the high points of hype, everyone starts talking about it like it will solve all their business problems. Blockchain is one of those technologies. According to Gartner's latest report on the hype cycle of emerging technologies, blockchain has just passed the peak of their hype cycle curve. If you read the news articles about it, one would think it has taken over the technology world. No disruptive technology is without its challenges and potential impediments t...
If a machine can invent, does this mean the end of the patent system as we know it? The patent system, both in the US and Europe, allows companies to protect their inventions and helps foster innovation. However, Artificial Intelligence (AI) could be set to disrupt the patent system as we know it. This talk will examine how AI may change the patent landscape in the years to come. Furthermore, ways in which companies can best protect their AI related inventions will be examined from both a US and...
Bill Schmarzo, Tech Chair of "Big Data | Analytics" of upcoming CloudEXPO | DXWorldEXPO New York (November 12-13, 2018, New York City) today announced the outline and schedule of the track. "The track has been designed in experience/degree order," said Schmarzo. "So, that folks who attend the entire track can leave the conference with some of the skills necessary to get their work done when they get back to their offices. It actually ties back to some work that I'm doing at the University of San...
When talking IoT we often focus on the devices, the sensors, the hardware itself. The new smart appliances, the new smart or self-driving cars (which are amalgamations of many ‘things'). When we are looking at the world of IoT, we should take a step back, look at the big picture. What value are these devices providing. IoT is not about the devices, its about the data consumed and generated. The devices are tools, mechanisms, conduits. This paper discusses the considerations when dealing with the...
Bill Schmarzo, author of "Big Data: Understanding How Data Powers Big Business" and "Big Data MBA: Driving Business Strategies with Data Science," is responsible for setting the strategy and defining the Big Data service offerings and capabilities for EMC Global Services Big Data Practice. As the CTO for the Big Data Practice, he is responsible for working with organizations to help them identify where and how to start their big data journeys. He's written several white papers, is an avid blogge...