Maven的pom.xml文件详解------Build Settings
- projectxmlns"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
<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
- build
- defaultGoalinstall</defaultGoal
- directory${basedir}/target</directory
- finalName${artifactId}-${version}</finalName
- filters
- filterfilters/filter1.properties</filter
- </filters
- </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(通常)不是代码,他们不被编译,但是被绑定在你的项目或者用于其它什么原因,例如代码生成。
- build
- resources
- resource
- targetPathMETA-INF/plexus</targetPath
- filteringfalse</filtering
- directory${basedir}/src/main/plexus</directory
- includes
- includeconfiguration.xml</include
- </includes
- excludes
- exclude**/*.properties</exclude
- </excludes
- </resource
- </resources
- testResources
- </testResources
- </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
- build
- plugins
- plugin
- groupIdorg.apache.maven.plugins</groupId
- artifactIdmaven-jar-plugin</artifactId
- version</version
- extensionsfalse</extensions
- inherited</inherited
- configuration
- classifier</classifier
- </configuration
- dependencies</dependencies
- executions</executions
- </plugin
- </plugins
- </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:
- plugin
- groupIdmy.group</groupId
- artifactIdmy-plugin</artifactId
- configuration
- items
- parent-1</
- parent-2</
- </items
- properties
- parentKeyparent</parentKey
- </properties
- </configuration
- </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中做如下配置:
- plugin
- groupIdmy.group</groupId
- artifactIdmy-plugin</artifactId
- configuration
- items
- child-1</
- </items
- properties
- childKeychild</childKey
- </properties
- </configuration
- </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中将得到:
- plugin
- groupIdmy.group</groupId
- artifactIdmy-plugin</artifactId
- configuration
- items
- child-1</
- </items
- properties
- childKeychild</childKey
- parentKeyparent</parentKey
- </properties
- </configuration
- </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:
- configuration
- itemscombine.children"append"
- <!-- combine.children="merge" is the default -->
- child-1</
- </items
- propertiescombine.self"override"
- <!-- combine.self="merge" is the default -->
- childKeychild</childKey
- </properties
- </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>则,现在合并后的效果如下:
- configuration
- itemscombine.children"append"
- parent-1</
- parent-2</
- child-1</
- </items
- propertiescombine.self"override"
- childKeychild</childKey
- </properties
- </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:
- build
- plugins
- plugin
- artifactIdmaven-antrun-plugin</artifactId
- version</version
- executions
- execution
- echodir</
- goals
- </
- </goals
- phaseverify</phase
- inheritedfalse</inherited
- configuration
- tasks
- Build Dir: ${project.build.directory}</
- </tasks
- </configuration
- </execution
- </executions
- </plugin
- </plugins
- </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中做如下配置:
- build
- pluginManagement
- plugins
- plugin
- groupIdorg.apache.maven.plugins</groupId
- artifactIdmaven-jar-plugin</artifactId
- version</version
- executions
- execution
- pre-process-classes</
- phasecompile</phase
- goals
- </
- </goals
- configuration
- classifierpre-process</classifier
- </configuration
- </execution
- </executions
- </plugin
- </plugins
- </pluginManagement
- </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中,我们只需要配置:
- build
- plugins
- plugin
- groupIdorg.apache.maven.plugins</groupId
- artifactIdmaven-jar-plugin</artifactId
- </plugin
- </plugins
- </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报告。
- reporting
- outputDirectory${basedir}/target/site</outputDirectory
- plugins
- plugin
- artifactIdmaven-project-info-reports-plugin</artifactId
- version2.0.1</version
- reportSets
- reportSet</reportSet
- </reportSets
- </plugin
- </plugins
- </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:
- reportSets
- reportSet
- sunlink</
- reports
- reportjavadoc</report
- </reports
- inherited</inherited
- configuration
- links
- http://java.sun.com/j2se/1.5.0/docs/api/</
- </links
- </configuration
- </reportSet
- </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>