Chapter 5. Spicing Up a Maven Project

So far we have built the base, and now we are finally ready to launch the rocket. Rocket! Exciting, isn't it? Let's put our knowledge from previous chapters to practice; we will use Maven to create a simple web application, MyDistance, which lets the user convert distance between different units. In the process of building this application, we will also learn to customize the project's information and generate different artifacts. The topics that will be covered in this chapter are categorized as follows:

  • Creating the MyDistance project
  • Changing the project information
  • Adding dependencies
  • Adding resources
  • The application code
    • Adding a form to obtain an input
    • Adding the servlet
    • Adding a utility class
  • Running an application
  • Writing unit tests
  • Running unit tests
  • Generating site documentation
  • Generating unit tests—HTML reports
  • Generating javadocs

Creating the MyDistance project

To create the MyDistance application, we need to perform the following steps:

  1. From the menu, navigate to File | New | Other.... A new project wizard window appears. Search for maven in the textbox, select Maven Project, and click on the Next button, as shown in the following screenshot:
    Creating the MyDistance project
  2. A New Maven Project wizard appears; select the Use default Workspace location checkbox, and ensure the Create a simple project (skip archetype selection) checkbox is unchecked, as shown in the following screenshot:
    Creating the MyDistance project
  3. Next, choose an archetype from the new archetype wizard. Since we are building a web application, in Filter, search for webapp, choose maven-archetype-webapp, and click on Next, as shown in the following screenshot:
    Creating the MyDistance project
  4. Specify the Maven coordinates, also termed as Group-Artifact-Version (GAV) in technical parlance, with the following values, and click on Finish:

    Field

    Value

    Group Id

    com.packt.mvneclipse

    Artifact Id

    MyDistance

    Version

    0.0.1-SNAPSHOT

    Package

    com.packt.chpt5.mydistance

    Your screen will look like the following screenshot once you perform the previous step:

    Creating the MyDistance project

    Note

    A snapshot in Maven indicates the current development copy, that is, the current snapshot of the code. Maven checks for a new SNAPSHOT version in a remote repository at a configured interval, for a default time of 24 hours. For more information on Maven versions, refer to http://docs.oracle.com/middleware/1212/core/MAVEN/maven_version.htm.

  5. The web application skeleton gets created and the structure would look like the following screenshot:
    Creating the MyDistance project

Don't worry if you see a red cross that indicates an error in the project; we will learn more about it in the upcoming section, Application code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.218.45