Maven学习日记(二)---MAVEN创建多模块的项目

手动构建多模块maven项目,这个simple-parent项目下有两个子模块,一个是jar包型的simple-weather和一个war型的simple-webapp
1.创建一个父的simple-parent项目
D:>mvn archetype:create -DgroupId=org.sonatype.mavenbook.ch06 -DartifactId=simp
le-parent -DpackageName=org.sonatype.mavenbook -DarchetypeArtifactId=maven-arche
type-webapp

2.修改里面的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>
<groupId>org.sonatype.mavenbook.ch06</groupId>
<artifactId>simple-parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>simple-parent Maven Webapp</name>
<url>http://maven.apache.org</url>
<modules>
<module>simple-weather</module>
<module>simple-webapp</module>
</modules
>
<build>
    <pluginManagement>
    <plugins>
       <plugin>
        <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>
3.在simple-parent下创建maven子项目是会出错.如下
D:simple-parent>mvn archetype:create -DgroupId=org.sonatype.mavenbook.weather -
DartifactId=simple-weather

org.apache.maven.reactor.MavenExecutionException: Could not find the model file
'D:simple-parentsimple-weatherpom.xml'. for project unknown
        at org.apache.maven.DefaultMaven.getProjects(DefaultMaven.java:376)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:289)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:126)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:282)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)

将这个pom移到别的地方就能正常运行(simple-parent目录下的pom.xml)

4.修改simple-weather中的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.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>

其它包中的内容直接copy过来

5.创建simple-webapp
D:simple-parent>mvn archetype:create -DgroupId=org.sonatype.mavenbook.web -Dart
ifactId=simple-webapp -DpackageName=org.sonatype.mavenbook -DarchetypeArtifactId
=maven-archetype-webapp

6.修改simple-webapp中的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.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>
7.从sample中copy包源码与web.xml

8.进入simple-parent中构建多模块项目mvn clean install

9.运行mvn jetty:run

10.可以构建为eclipse项目mvn eclipse:eclipse

11.访问http://localhost:8080/simple-webapp/weather?zip=110

相关资料:
参与书籍:maven权威指南
链接:http://www.sonatype.com/books/maven-book/reference_zh/public-book.html
书中源码下载地址:http://www.sonatype.com/book/mvn-examples-1.0.zip

原文地址:https://www.cnblogs.com/superjt/p/3193050.html