Maven by Example
   - 7.1. Introduction

7.1. Introduction

In this chapter, we create a multi-module project that evolves the examples from Chapter 6, A Multi-Module Project and Chapter 5, A Simple Web Application into a project that uses the Spring Framework and Hibernate to create both a simple web application and a command-line utility to read data from the Yahoo Weather feed. The simple-weather code developed in Chapter 4, Customizing a Maven Project will be combined with the simple-webapp project defined in Chapter 5, A Simple Web Application. In the process of creating this multi-module project, we’ll explore Maven and discuss the different ways it can be used to create modular projects that encourage reuse.

7.1.1. Downloading this Chapter’s Example

The multi-module project developed in this example consists of modified versions of the projects developed in Chapter 4, Customizing a Maven Project and Chapter 5, A Simple Web Application, and we are not using the Maven Archetype plug-in to generate this multi-module project. We strongly recommend downloading a copy of the example code to use as a supplemental reference while reading the content in this chapter. Without the examples, you won’t be able to recreate this chapter’s example code. This chapter’s example project may be downloaded with the book’s example code at:

http://books.sonatype.com/mvnex-book/mvnex-examples.zip

Unzip this archive in any directory, and then go to the ch-multi-spring directory. There you will see a directory named simple-parent that contains the multi-module Maven project developed in this chapter. In the simple-parent/ project directory you will see a pom.xml and the five submodule directories simple-model/, simple-persist/, simple-command/, simple-weather/ and simple-webapp/.

7.1.2. Multi-Module Enterprise Project

Presenting the complexity of a massive Enterprise-level project far exceeds the scope of this book. Such projects are characterized by multiple databases, integration with external systems, and subprojects which may be divided by departments. These projects usually span thousands of lines of code, and involve the effort of tens or hundreds of software developers. While such a complete example is outside the scope of this book, we can provide you with a sample project that suggests the complexity of a larger Enterprise application. In the conclusion we suggest some possibilities for modularity beyond that presented in this chapter.

In this chapter, we’re going to look at a multi-module Maven project that will produce two applications: a command-line query tool for the Yahoo Weather feed and a web application which queries the Yahoo Weather feed. Both of these applications will store the results of queries in an embedded database. Each will allow the user to retrieve historical weather data from this embedded database. Both applications will reuse application logic and share a persistence library. This chapter’s example builds upon the Yahoo Weather parsing code introduced in Chapter 4, Customizing a Maven Project. This project is divided into five submodules shown in Figure 7.1, “Multi-Module Enterprise Application Module Relationships”.

figs/web/multimodule-web-spring_projects.png

Figure 7.1. Multi-Module Enterprise Application Module Relationships


In Figure 7.1, “Multi-Module Enterprise Application Module Relationships”, you can see that there are five submodules of simple-parent. They are:

simple-model
This module defines a simple object model which models the data returned from the Yahoo Weather feed. This object model contains the Weather, Condition, Atmosphere, Location, and Wind objects. When our application parses the Yahoo Weather feed, the parsers defined in simple-weather will parse the XML and create Weather objects which are then used by the application. This project contains model objects annotated with Hibernate 3 Annotations. These annotations are used by the logic in simple-persist to map each model object to a corresponding table in a relational database.
simple-weather
This module contains all of the logic required to retrieve data from the Yahoo Weather feed and parse the resulting XML. The XML returned from this feed is converted into the model objects defined in simple-model. simple-weather has a dependency on simple-model. simple-weather defines a WeatherService object which is referenced by both the simple-command and simple-webapp projects.
simple-persist
This module contains some Data Access Objects (DAO) which are configured to store Weather objects in an embedded database. Both of the applications defined in this multi-module project will use the DAOs defined in simple-persist to store data in an embedded database. The DAOs defined in this project understand and return the model objects defined in simple-model. simple-persist has a direct dependency on simple-model and it depends upon the Hibernate Annotations present on the model objects.
simple-webapp
The web application project contains two Spring MVC Controller implementations which use the WeatherService defined in simple-weather and the DAOs defined in simple-persist. simple-webapp has a direct dependency on simple-weather and simple-persist; it has a transitive dependency on simple-model.
simple-command
This module contains a simple command-line tool which can be used to query the Yahoo Weather feed. This project contains a class with a static main() method and interacts with the WeatherService defined in simple-weather and the DAOs defined in simple-persist. simple-command has a direct dependency on simple-weather and simple-persist; it has a transitive dependency on simple-model.

This chapter contains a contrived example simple enough to introduce in a book, yet complex enough to justify a set of five submodules. Our contrived example has a model project with five classes, a persistence library with two service classes, and a weather parsing library with five or six classes, but a real-world system might have a model project with a hundred objects, several persistence libraries, and service libraries spanning multiple departments. Although we’ve tried to make sure that the code contained in this example is straightforward enough to comprehend in a single sitting, we’ve also gone out of our way to build a modular project. You might be tempted to look at the examples in this chapter and walk away with the idea that Maven encourages too much complexity given that our model project has only five classes. Although using Maven does suggest a certain level of modularity, do realize that we’ve gone out of our way to complicate our simple example projects for the purpose of demonstrating Maven’s multi-module features.

7.1.3. Technology Used in this Example

This chapter’s example involves some technology which, while popular, is not directly related to Maven. These technologies are the Spring Framework and Hibernate. The Spring Framework is an Inversion of Control (IoC) container and a set of frameworks that aim to simplify interaction with various J2EE libraries. Using the Spring Framework as a foundational framework for application development gives you access to a number of helpful abstractions that can take much of the meddlesome busywork out of dealing with persistence frameworks like Hibernate or iBatis or enterprise APIs like JDBC, JNDI, and JMS. The Spring Framework has grown in popularity over the past few years as a replacement for the heavy weight enterprise standards coming out of Sun Microsystems. Hibernate is a widely used Object-Relational Mapping framework which allows you to interact with a relational database as if it were a collection of Java objects. This example focuses on building a simple web application and a command-line application that use the Spring Framework to expose a set of reusable components to applications and which also use Hibernate to persist weather data in an embedded database.

We’ve decided to include references to these frameworks to demonstrate how one would construct projects using these technologies when using Maven. Although we make brief efforts to introduce these technologies throughout this chapter, we will not go out of our way to fully explain these technologies. For more information about the Spring Framework, please see the project’s web site at http://www.springsource.org/documentation. For more information about Hibernate and Hibernate Annotations, please see the project’s web site at http://www.hibernate.org. This chapter uses Hyper Structured Query Language Database (HSQLDB) as an embedded database; for more information about this database, see the project’s web site at http://hsqldb.org.












Become a Member

Are you a current user of:

Top