本文译自:http://maven.apache.org/pom.html
超级POM
与面向对象编程中的对象继承类似,POM会扩展某些继承自父工程的值,而且跟Java对象都继承自基本的java.lang.Object一样,Mavne的所有工程对象模型都继承自基本的超级POM。以下是Maven3.0.4中超级POM的片段:
<project>
<modelVersion>4.0.0</modelVersion>
<repositories>
<repository>
<id>central</id>
<name>Central Repository</name>
<url>http://repo.maven.apache.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<name>Central Repository</name>
<url>http://repo.maven.apache.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
</pluginRepository>
</pluginRepositories>
<build>
<directory>${project.basedir}/target</directory>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<finalName>${project.artifactId}-${project.version}</finalName>
<testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
<scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>${project.basedir}/src/test/resources</directory>
</testResource>
</testResources>
<pluginManagement>
<!-- NOTE: These plugins will be removed from future versions of the super POM -->
<!-- They are kept for the moment as they are very unlikely to conflict with lifecycle mappings (MNG-4453) -->
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
</plugin>
<plugin>
<artifactId>maven-release-plugin</artifactId>
<version>2.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<reporting>
<outputDirectory>${project.build.directory}/site</outputDirectory>
</reporting>
<profiles>
<!-- NOTE: The release profile will be removed from future versions of the super POM -->
<profile>
<id>release-profile</id>
<activation>
<property>
<name>performRelease</name>
<value>true</value>
</property>
</activation>
<build>
<plugins>
<plugin>
<inherited>true</inherited>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<inherited>true</inherited>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<inherited>true</inherited>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<updateReleaseInfo>true</updateReleaseInfo>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
通过创建一个最小的pom.xml,并在命令行执行mvn help:effective-pom命令,你可以看到这个超级POM是如何影响你的对象模型的。
依赖管理
除了某些顶级元素之外,父POM会把一些元素的值和传递性依赖配置给子POM。dependencyManagement就是这样的元素之一。
dependencyManagement:通过使用这个元素来帮助管理其所有子工程的依赖信息。如果my-parent使用dependencyManagement来定义一个依赖:junit:junit:4.0,那么只要在继承它的子工程中把它们的依赖设置为groupId=junit、artifactId=junit,Maven就会使用它的父工程的版本号来设置version。这种方法的好处是显而易见的。依赖的详细信息能够被集中设置在一个地方,并且会在所有继承的POM中传播。另外,从传递性依赖中合并过来的部件版本和范围,也可以通过在一个依赖管理段里指定它们来进行必要的控制。
聚合(或多模块)
一个带有多个模块的工程被叫做大多模块或集合工程。这些模块会被组织在POM的列表中,并且会作为一组来执行。一个被打包成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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.codehaus.mojo</groupId>
<artifactId>my-parent</artifactId>
<version>2.0</version>
<packaging>pom</packaging>
<modules>
<module>my-project</module>
<module>another-project</module>
</modules>
</project>
在列出这些模块的时候,你不需要考虑模块间的依赖,也就是说,POM中给出的模块顺序是不重要的。Maven会对这些模块进行排序,以便相关被依赖的模块始终是在依赖模块之前编译。
关于继承和聚合的最后一点说明
使用继承和聚合可以创建一个单一的、高级的POM来动态的控制编译。我们经常会看到既是根又是聚集的工程。例如,通过一个单一的、基本的POM org.apache.maven:maven来运行整个Maven内核,通过一个单一的命令:mvncompile,就和执行Maven工程的编译。但是,尽管一个工程既可以是根工程也聚集工程,但是这两者之间还是有区别的,不应该混淆。一个可以被继承的POM工程不一定要有多个聚集模块,同样一个聚集的工程也不一定要被继承。
原文:http://blog.csdn.net/think_soft/article/details/43758685