[转]Maven的pom.xml文件详解

Maven的pom.xml文件详解------Build Settings
2013年10月30日 13:04:01
阅读数:44678
根据POM 4.0.0 XSD,build元素概念性的划分为两个部分:BaseBuild(包含poject build和profile build的公共部分,见下)和poject build包含的一些高级特性。
  1. projectxmlns"http://maven.apache.org/POM/4.0.0"
  2.         xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance"
  3.         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0  
  4.                         http://maven.apache.org/xsd/maven-4.0.0.xsd"
  5.     <!-- "Project Build" contains more elements than just the BaseBuild set -->
  6.     build</build
  7.     profiles
  8.         profile
  9.             <!-- "Profile Build" contains a subset of "Project Build"s elements -->
  10.             build</build
  11.         </profile
  12.     </profiles
  13. </project
<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">
	...
	<!-- "Project Build" contains more elements than just the BaseBuild set -->
	<build>...</build>


	<profiles>
		<profile>
			<!-- "Profile Build" contains a subset of "Project Build"s elements -->
			<build>...</build>
		</profile>
	</profiles>
</project>

BaseBuild元素集合

basic elements

  1. build
  2.     defaultGoalinstall</defaultGoal
  3.     directory${basedir}/target</directory
  4.     finalName${artifactId}-${version}</finalName
  5.     filters
  6.         filterfilters/filter1.properties</filter
  7.     </filters
  8. </build
<build>
	<defaultGoal>install</defaultGoal>
	<directory>${basedir}/target</directory>
	<finalName>${artifactId}-${version}</finalName>
	<filters>
		<filter>filters/filter1.properties</filter>
	</filters>
	...
</build>
1、defaultGoal:执行build任务时,如果没有指定目标,将使用的默认值,如:在命令行中执行mvn,则相当于执行mvn install;
2、directory:build目标文件的存放目录,默认在${basedir}/target目录;
3、finalName:build目标文件的文件名,默认情况下为${artifactId}-${version};
4、filter:定义*.properties文件,包含一个properties列表,该列表会应用的支持filter的resources中。也就是说,定义在filter的文件中的"name=value"值对会在build时代替${name}值应用到resources中。Maven的默认filter文件夹是${basedir}/src/main/filters/。

resources

build的另一个特征是指定你的项目中resources的位置。resources(通常)不是代码,他们不被编译,但是被绑定在你的项目或者用于其它什么原因,例如代码生成。

  1. build
  2.     resources
  3.          resource
  4.             targetPathMETA-INF/plexus</targetPath
  5.             filteringfalse</filtering
  6.             directory${basedir}/src/main/plexus</directory
  7.             includes
  8.                 includeconfiguration.xml</include
  9.             </includes
  10.             excludes
  11.                 exclude**/*.properties</exclude
  12.             </excludes
  13.          </resource
  14.     </resources
  15.     testResources
  16.     </testResources
  17. </build
<build>
	...
	<resources>
		 <resource>
			<targetPath>META-INF/plexus</targetPath>
			<filtering>false</filtering>
			<directory>${basedir}/src/main/plexus</directory>
			<includes>
				<include>configuration.xml</include>
			</includes>
			<excludes>
				<exclude>**/*.properties</exclude>
			</excludes>
		 </resource>
	</resources>
	<testResources>
		...
	</testResources>
	...
</build>
1、resources:一个resource元素的列表,每一个都描述与项目关联的文件是什么和在哪里;
2、targetPath:指定build后的resource存放的文件夹。该路径默认是basedir。通常被打包在JAR中的resources的目标路径为META-INF;
3、filtering:true/false,表示为这个resource,filter是否激活。
4、directory:定义resource所在的文件夹,默认为${basedir}/src/main/resources;
5、includes:指定作为resource的文件的匹配模式,用*作为通配符;
6、excludes:指定哪些文件被忽略,如果一个文件同时符合includes和excludes,则excludes生效;
7、testResources:定义和resource类似,但只在test时使用,默认的test resource文件夹路径是${basedir}/src/test/resources,test resource不被部署。

Plugins

  1. build
  2.     plugins
  3.         plugin
  4.             groupIdorg.apache.maven.plugins</groupId
  5.             artifactIdmaven-jar-plugin</artifactId
  6.             version</version
  7.             extensionsfalse</extensions
  8.             inherited</inherited
  9.             configuration
  10.                 classifier</classifier
  11.             </configuration
  12.             dependencies</dependencies
  13.             executions</executions
  14.         </plugin
  15.     </plugins
  16. </build
<build>
    ...
	<plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-jar-plugin</artifactId>
			<version>2.0</version>
			<extensions>false</extensions>
			<inherited>true</inherited>
			<configuration>
				<classifier>test</classifier>
			</configuration>
			<dependencies>...</dependencies>
			<executions>...</executions>
		</plugin>
    </plugins>
</build>
除了groupId:artifactId:version标准坐标,plugin还需要如下属性:
1、extensions:true/false,是否加载plugin的extensions,默认为false;
2、inherited:true/false,这个plugin是否应用到该POM的孩子POM,默认true;
3、configuration:配置该plugin期望得到的properies,如上面的例子,我们为maven-jar-plugin的Mojo设置了classifier属性;

如果你的POM有一个parent,它可以从parent的build/plugins或者pluginManagement集成plugin配置。

为了阐述继承后的关系,考虑如果parent POM中存在如下plugin:

  1. plugin
  2.     groupIdmy.group</groupId
  3.     artifactIdmy-plugin</artifactId
  4.     configuration
  5.         items
  6.             parent-1</
  7.             parent-2</
  8.         </items
  9.         properties
  10.             parentKeyparent</parentKey
  11.         </properties
  12.     </configuration
  13. </plugin
<plugin>
	<groupId>my.group</groupId>
	<artifactId>my-plugin</artifactId>
	<configuration>
		<items>
			<item>parent-1</item>
			<item>parent-2</item>
		</items>
		<properties>
			<parentKey>parent</parentKey>
		</properties>
	</configuration>
</plugin>

然后在继承的孩子POM中做如下配置:

  1. plugin
  2.     groupIdmy.group</groupId
  3.     artifactIdmy-plugin</artifactId
  4.     configuration
  5.         items
  6.             child-1</
  7.         </items
  8.         properties
  9.             childKeychild</childKey
  10.         </properties
  11.     </configuration
  12. </plugin
<plugin>
	<groupId>my.group</groupId>
	<artifactId>my-plugin</artifactId>
	<configuration>
		<items>
			<item>child-1</item>
		</items>
		<properties>
			<childKey>child</childKey>
		</properties>
	</configuration>
</plugin>

这样孩子POM和parent POM中都存在groupId为my.group的plugin,Maven默认的行为将是根据属性名称将两个plugin的configuration的内容进行合并。如果孩子POM中有一个属性,则该属性是有效的,如果孩子POM中没有一个属性,但parent POM中存在,则parent中的属性是有效的。

根据这些规则,上面的例子在Maven中将得到:

  1. plugin
  2.     groupIdmy.group</groupId
  3.     artifactIdmy-plugin</artifactId
  4.     configuration
  5.         items
  6.             child-1</
  7.         </items
  8.         properties
  9.             childKeychild</childKey
  10.             parentKeyparent</parentKey
  11.         </properties
  12.     </configuration
  13. </plugin
<plugin>
	<groupId>my.group</groupId>
	<artifactId>my-plugin</artifactId>
	<configuration>
		<items>
			<item>child-1</item>
		</items>
		<properties>
			<childKey>child</childKey>
			<parentKey>parent</parentKey>
		</properties>
	</configuration>
</plugin>

通过在configuration元素中增加combine.children和combine.self属性,孩子POM可以控制Maven怎么合并plugin的configuration。

假定这儿是孩子POM的configuration:

  1. configuration
  2.     itemscombine.children"append"
  3.         <!-- combine.children="merge" is the default -->
  4.         child-1</
  5.     </items
  6.     propertiescombine.self"override"
  7.         <!-- combine.self="merge" is the default -->
  8.         childKeychild</childKey
  9.     </properties
  10. </configuration
<configuration>
	<items combine.children="append">
		<!-- combine.children="merge" is the default -->
		<item>child-1</item>
	</items>
	<properties combine.self="override">
		<!-- combine.self="merge" is the default -->
		<childKey>child</childKey>
	</properties>
</configuration>

则,现在合并后的效果如下:

  1. configuration
  2.     itemscombine.children"append"
  3.         parent-1</
  4.         parent-2</
  5.         child-1</
  6.     </items
  7.     propertiescombine.self"override"
  8.         childKeychild</childKey
  9.     </properties
  10. </configuration
<configuration>
	<items combine.children="append">
		<item>parent-1</item>
		<item>parent-2</item>
		<item>child-1</item>
	</items>
	<properties combine.self="override">
		<childKey>child</childKey>
	</properties>
</configuration>

combine.children="append"表示父POM和子POM的属性合并起来;

combine.self="override"表示子POM的属性完全覆盖父POM的。

4、dependencies:同base build中的dependencies有同样的结构和功能,但这里是作为plugin的依赖,而不是项目的依赖。
5、executions:plugin可以有多个目标,每一个目标都可以有一个分开的配置,甚至可以绑定一个plugin的目标到一个不同的阶段。executions配置一个plugin的目标的execution。

假定一项绑定antrun:run目标到verify阶段,我们希望任务响应build文件夹,同时避免传递配置到他的孩子POM。你将得到一个execution:

  1. build
  2.     plugins
  3.         plugin
  4.             artifactIdmaven-antrun-plugin</artifactId
  5.             version</version
  6.             executions
  7.                 execution
  8.                     echodir</
  9.                     goals
  10.                         </
  11.                     </goals
  12.                     phaseverify</phase
  13.                     inheritedfalse</inherited
  14.                     configuration
  15.                         tasks
  16.                             Build Dir: ${project.build.directory}</
  17.                         </tasks
  18.                     </configuration
  19.                 </execution
  20.             </executions
  21.         </plugin
  22.     </plugins
  23. </build
<build>
    <plugins>
		<plugin>
			<artifactId>maven-antrun-plugin</artifactId>
			<version>1.1</version>
			<executions>
				<execution>
					<id>echodir</id>
					<goals>
						<goal>run</goal>
					</goals>
					<phase>verify</phase>
					<inherited>false</inherited>
					<configuration>
						<tasks>
							<echo>Build Dir: ${project.build.directory}</echo>
						</tasks>
					</configuration>
				</execution>
			</executions>
		</plugin>
    </plugins>
</build>

id:标识,用于和其他execution区分。当这个阶段执行时,它将以这个形式展示:[plugin:goal execution: id]。在这里为: [antrun:run execution: echodir];

goals:一个plugin的execution的目标列表;

phase:目标执行的阶段,具体值看Maven的生命周期列表;

inherited:是否继承;

configuration:在指定的目标下的配置。

Plugin Management

pluginManagement的元素的配置和plugins的配置是一样的,只是这里的配置只是用于集成,在孩子POM中指定使用。例如,在父POM中做如下配置:

  1. build
  2.     pluginManagement
  3.         plugins
  4.             plugin
  5.               groupIdorg.apache.maven.plugins</groupId
  6.               artifactIdmaven-jar-plugin</artifactId
  7.               version</version
  8.                 executions
  9.                     execution
  10.                         pre-process-classes</
  11.                         phasecompile</phase
  12.                         goals
  13.                             </
  14.                         </goals
  15.                         configuration
  16.                             classifierpre-process</classifier
  17.                         </configuration
  18.                     </execution
  19.                 </executions
  20.             </plugin
  21.         </plugins
  22.     </pluginManagement
  23. </build
<build>
    ...
    <pluginManagement>
		<plugins>
			<plugin>
			  <groupId>org.apache.maven.plugins</groupId>
			  <artifactId>maven-jar-plugin</artifactId>
			  <version>2.2</version>
				<executions>
					<execution>
						<id>pre-process-classes</id>
						<phase>compile</phase>
						<goals>
							<goal>jar</goal>
						</goals>
						<configuration>
							<classifier>pre-process</classifier>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
    </pluginManagement>
    ...
</build>
则在孩子POM中,我们只需要配置:
  1. build
  2.     plugins
  3.         plugin
  4.             groupIdorg.apache.maven.plugins</groupId
  5.             artifactIdmaven-jar-plugin</artifactId
  6.         </plugin
  7.     </plugins
  8. </build
<build>
    ...
    <plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-jar-plugin</artifactId>
		</plugin>
    </plugins>
    ...
</build>
这样就可以大大的简化孩子POM中的配置。

Reporting

Reporting包含的属性对应到site阶段(见Maven生命周期)。特定的Maven插件能产生定义和配置在reporting元素下的报告,例如:产生Javadoc报告。

  1. reporting
  2.     outputDirectory${basedir}/target/site</outputDirectory
  3.     plugins
  4.         plugin
  5.             artifactIdmaven-project-info-reports-plugin</artifactId
  6.             version2.0.1</version
  7.             reportSets
  8.                 reportSet</reportSet
  9.             </reportSets
  10.         </plugin
  11.     </plugins
  12. </reporting
<reporting>
    <outputDirectory>${basedir}/target/site</outputDirectory>
    <plugins>
		<plugin>
			<artifactId>maven-project-info-reports-plugin</artifactId>
			<version>2.0.1</version>
			<reportSets>
				<reportSet></reportSet>
			</reportSets>
		</plugin>
    </plugins>
</reporting>
对于reportSets:
  1. reportSets
  2.     reportSet
  3.         sunlink</
  4.         reports
  5.             reportjavadoc</report
  6.         </reports
  7.         inherited</inherited
  8.         configuration
  9.             links
  10.                 http://java.sun.com/j2se/1.5.0/docs/api/</
  11.             </links
  12.         </configuration
  13.     </reportSet
  14. </reportSets
<reportSets>
	<reportSet>
		<id>sunlink</id>
		<reports>
			<report>javadoc</report>
		</reports>
		<inherited>true</inherited>
		<configuration>
			<links>
				<link>http://java.sun.com/j2se/1.5.0/docs/api/</link>
			</links>
		</configuration>
	</reportSet>
</reportSets>

posted on 2018-05-26 20:58  奚云刀  阅读(500)  评论(0编辑  收藏  举报

导航