Maven by Example
6.3. The Simple Weather Module

The first submodule we’re going to look at is the simple-weather
submodule. This submodule contains all
simple-weather Module POM.
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.sonatype.mavenbook.multi</groupId>
<artifactId>simple-parent</artifactId>
<version>1.0</version>
</parent>
<artifactId>simple-weather</artifactId>
<packaging>jar</packaging>
<name>Multi Chapter Simple Weather API</name>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.5</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
In simple-weathers 'pom.xml file, we see this module referencing a
parent POM using a set of Maven coordinates. The parent POM for
simple-weather is identified by a groupId of
org.sonatype.mavenbook.multi, an artifactId of simple-parent,
and a version of 1.0.
The WeatherService class shown in The WeatherService Class is
defined in src/main/java/org/sonatype/mavenbook/weather, and it
simply calls out to the three objects defined in Chapter 4, Customizing a Maven Project. In
this chapter’s example, we’re creating a separate project that
contains service objects that are referenced in the web application
project. This is a common model in enterprise Java development; often
a complex application consists of more than just a single, simple web
application. You might have an enterprise application that consists of
multiple web applications and some command-line applications. Often,
you’ll want to refactor common logic to a service class that can be
reused across a number of projects. This is the justification for
creating a WeatherService class; by doing so, you can see how the
simple-webapp project references a service object defined in
simple-weather.
package org.sonatype.mavenbook.weather;
import java.io.InputStream;
public class WeatherService {
public WeatherService() {}
public String retrieveForecast( String zip ) throws Exception {
// Retrieve Data
InputStream dataIn = new YahooRetriever().retrieve( zip );
// Parse Data
Weather weather = new YahooParser().parse( dataIn );
// Format (Print) Data
return new WeatherFormatter().format( weather );
}
}
The retrieveForecast() method takes a String containing a zip
code. This zip code parameter is then passed to the YahooRetriever's
retrieve() method, which gets the XML from Yahoo! Weather. The XML
returned from YahooRetriever is then passed to the parse() method
on YahooParser which returns a Weather object. This Weather
object is then formatted into a presentable String by the
WeatherFormatter.
