一个多模块项目通过一个父POM引用一个或多个子模块来定义。在simple-parent/目录中你能找到一个父POM(也称为顶层POM)为simple-parent/pom.xml

 

拷贝一个pom.xml文件,修改

 

<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>

	<groupId>org.sonatype.mavenbook.ch06</groupId>
	<artifactId>simple-parent</artifactId>
	<packaging>pom</packaging>
	<version>1.0</version>
	<name>Chapter 6 Simple Parent Project</name>
 
	<modules>
		<module>simple-weather</module>
		<module>simple-webapp</module>
	</modules>

	<build>
		<pluginManagement>
			<plugins>
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-compiler-plugin</artifactId>
					<configuration>
						<source>1.5</source>
						<target>1.5</target>
					</configuration>
				</plugin>
			</plugins>
		</pluginManagement> 
	</build>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
</project>


 simple-parent定义了一组Maven坐标:groupIdorg.sonatype.mavenbookartifactIdsimple-parentversion1.0。这个父项目不像之前的项目那样创建一个JAR或者一个WAR,它仅仅是一个引用其它Maven项目的POM。像simple-parent这样仅仅提供项目对象模型的项目,正确的的打包类型是pompom.xml中下一部分列出了项目的子模块。这些模块在modules元素中定义,每个modules元素对应了一个simple-parent/目录下的子目录。Maven知道去这些子目录寻找pom.xml文件,并且,在构建的simp-parent的时候,它会将这些子模块包含到要构建的项目中。

 

定义了一些将会被所有子模块继承的设置。simple-parent的build部分配置了所有Java编译的目标是Java 5 JVM。因为compiler插件默认绑定到了生命周期,我们就可以使用pluginManagement部分来配置。我们将会在后面的章节详细讨论pluginManagement,区分为默认的插件提供配置和真正的绑定插件是很容易的。dependencies元素将JUnit 3.8.1添加为一个全局的依赖。build配置和dependencies都会被所有的子模块继承。使用POM继承允许你添加一些全局的依赖如JUnit和Log4J。

 

simple-weather 模块

<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.ch06</groupId>
		<artifactId>simple-parent</artifactId>
		<version>1.0</version>
	</parent>
	<artifactId>simple-weather</artifactId>
	<packaging>jar</packaging>

	<name>Chapter 6 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>


 在simple-weatherpom.xml文件中我们看到该模块使用一组Maven坐标引用了一个父POM。simple-weather的父POM通过一个值为org.sonatype.mavenbookgroupId,一个值为simple-parentartifactId,以及一个值为1.0version来定义。注意子模块中我们不再需要重新定义groupId和version,它们都从父项目继承了

 

 WeatherService 类

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 );
    }	
}

retrieveForecast()方法接受一个包含邮政编码的String。之后这个邮政编码参数被传给YahooRetrieverretrieve()方法,后者从Yahoo! Weather获取XML。从YahooRetriever返回的XML被传给YahooParserparse()方法,后者继而又返回一个Weather对象。之后这个Weather对象被WeatherFormatter格式化成一个像样的String

 

simple-webapp 模块

simple-webapp模块是在simple-parent项目中引用的第二个子模块。这个web项目依赖于simple-weather模块,并且包含了一些用来展示从Yahoo! Weather服务查询到的结果的servlet。

<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.ch06</groupId>
		<artifactId>simple-parent</artifactId>
		<version>1.0</version>
	</parent>

	<artifactId>simple-webapp</artifactId>
	<packaging>war</packaging>
	<name>simple-webapp Maven Webapp</name>
  
	<dependencies>
		<dependency>
			<groupId>org.apache.geronimo.specs</groupId>
			<artifactId>geronimo-servlet_2.4_spec</artifactId>
			<version>1.1.1</version>
		</dependency>
		<dependency>
			<groupId>org.sonatype.mavenbook.ch06</groupId>
			<artifactId>simple-weather</artifactId>
			<version>1.0</version>
		</dependency>
	</dependencies>

	<build>
		<finalName>simple-webapp</finalName>
		<plugins>
			<plugin>
				<groupId>org.mortbay.jetty</groupId>
				<artifactId>maven-jetty-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

 

WeatherServlet

 

package org.sonatype.mavenbook.web;

import org.sonatype.mavenbook.weather.WeatherService;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class WeatherServlet extends HttpServlet {
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws ServletException, IOException {
	String zip = request.getParameter("zip" );
	WeatherService weatherService = new WeatherService();
	PrintWriter out = response.getWriter();
        try {
	    out.println( weatherService.retrieveForecast( zip ) );
	} catch( Exception e ) {
	    out.println( "Error Retrieving Forecast: " + e.getMessage() );
	}
        out.flush();
        out.close();
    }
}

 在WeatherServlet中,我们初始化了一个在simple-weather中定义的WeatherService类的实例。在请求参数中提供的邮政编码被传给retrieveForecast()方法,并且返回结果被打印至HTTP响应的Writer

 

web.xml将所有这一切绑在一起


<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <servlet>
    <servlet-name>simple</servlet-name>
    <servlet-class>org.sonatype.mavenbook.web.SimpleServlet</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>weather</servlet-name>
    <servlet-class>org.sonatype.mavenbook.web.WeatherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>simple</servlet-name>
    <url-pattern>/simple</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>weather</servlet-name>
    <url-pattern>/weather</url-pattern>
  </servlet-mapping>
</web-app>


 

构建这个多模块项目

simple-parent项目运行mvn clean install命令

 

当你在命令行运行Maven的时候你经常会在任何其它生命周期阶段前指定clean生命周期阶段。当你指定clean,你就确认了在编译和打包一个应用之前,Maven会移除旧的输出。运行clean不是必要的,但这是一个确保你正执行“干净构建”的十分有用的预防措施。

 

运行Web应用

mvn jetty:run