Maven 2 通过该 pom.xml 文件了解您的项目。
该文件由 Archetype 按照 NumOps 生成,如清单 8 所示:
<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>com.ibm.devworks</groupId>
<artifactId>NumOps</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>Maven Quick Start Archetype</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
请注意:在测试阶段中(通过 标记),Archetype 如何定义模块的坐标、如何将类型定义为 JAR 归档以及如何将 JUnit 指定为一个依赖项。
要为您的新项目定制这个 pom.xml 文件,请参照清单 9 中突出显示的部分,稍作改动。
额外的 标记是必要的,用来覆盖源代码,以达到 Java 代码的水平。默认情况下,采用的是 JDK 1.4,但您的代码使用了泛型,因而需要 JDK 5.0 编译。
编译定制的项目
现在可以使用 mvn compile 命令编译 NumOps 项目。 这个命令使 Maven 2 引擎从构建生命周期一直运行到编译阶段,并执行相应的 mojo。您应该看到构建成功的报告,报告中在目标目录树里创建了三个类文件(如清单 10 所示)。如果这是第一次运行,那会花点时间,因为一些依赖项需要经过 Internet 从中央存储库下载。
NumOps 项目中 mvn 编译的输出
[code][INFO] Scanning for projects…
[INFO] -------------------------------------------------------------------------
[INFO] Building Intro to Maven 2 Example 1
[INFO] task-segment: [compile]
[INFO] -------------------------------------------------------------------------
[INFO] [resources:resources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:compile]
Compiling 3 source files to C:\temp\maven\NumOps\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Sat Dec 02 22:52:16 EST 2006
[INFO] Final Memory: 3M/7M
[INFO] ------------------------------------------------------------------------[/code]