Maven by Example
   - 6.3. The Simple Weather Module

6.3. The Simple Weather Module

The first submodule we’re going to look at is the simple-weather submodule. This submodule contains all the code from the previous section Chapter 4, Customizing a Maven Project.

simple-weather Module POM. 

<project xmlns=""

    <name>Multi Chapter Simple Weather API</name>



In simple-weather's 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.

The WeatherService Class. 



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.

Become a Member

Are you a current user of: