在本章主要介绍Build Setting,涉及到
build 声明项目目录结构和管理插件
reporting 反映报告目的的构建要素
Build主要使用在POM project顶级元素下和profiles元素下。
可以表示为”project build” and “profile build”,如下:
"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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
...
...
...
The BaseBuild Element Set
构建Build包括基本通用元素、
先介绍构建Build元素一些基础元素、Resources、Plugins、Plugin Management。
Base Element
<build>
<defaultGoal>installdefaultGoal>
<directory>${basedir}/targetdirectory>
<finalName>${artifactId}-${version}finalName>
<filters>
<filter>filters/filter1.propertiesfilter>
filters>
...
build>
directory
build target目标的目录,默认为${basedir}/target。
fileName
build目标文件的文件名称,默认为${artifactId}-${version}
filters
指定filter属性的位置,定义包含适用于接受其设置的资源的属性列表的* .properties文件。 换句话说,过滤器文件中定义的”name=value”对在构建资源中替换$ {name}字符串。 上面的示例定义了filter /目录下的filter1.properties文件。 Maven的默认过滤器目录是$ {basedir}/src/main/filters/
Resources
Resources指定加载项目中已存在的资源。
"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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
...
META-INF/plexus
false
${basedir}/src/main/plexus
configuration.xml
**/*.properties
...
...
resources
resource元素列表,每个资源元素都描述与此项目关联的文件和何处包含的文件。
targetPath
指定从构建中放置资源集的目录结构。目标路径默认为基本目录。将要包装在JAR中的资源的通常指定的目标路径是META-INF
filtering
值为true或false,表示十分要启动资源过滤。如果过滤所有properties(* .properties)不必定义。resources默认情况下使用POM文件中定义的属性(如${project.version})。
directory
resources目录路径,默认为${basedir}/src/main/resources
includes
指定要作为资源包含在该指定目录下的文件,使用*作为通配符
excludes
排除资源文件,目录结构与includes相同,如果当include和exclude冲突,优先执行exclude。
testResources
测试资源,目录结构与resources相同,但是只能在测试阶段使用。其中一个区别就是项目的默认测试资源目录是${basedir}/src/test/resources。测试资源不部署。
Plugins
"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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
...
org.apache.maven.plugins
maven-jar-plugin
2.6
false
true
test
...
...
extensions
值为true或false,默认为false。决定是否加载插件扩展。
inherited
值为true或false,默认为true。是否可以继承插件POM属性。
configuration
用于个人不开源的plugin,无法深入了解plugin内部工作原理,但满足项目正常使用plugin。
配置元素中的值不是由POM模式显式要求的,但插件目标有权要求配置值。
如果POM声明父类,那么将从父类的build/plugin或者pluginManagement继承plugin配置。
如果使用插件继承关系,首先父类POM存在plugin代码
<plugin>
<groupId>my.groupgroupId>
<artifactId>my-pluginartifactId>
<configuration>
<items>
<item>parent-1item>
<item>parent-2item>
items>
<properties>
<parentKey>parentparentKey>
properties>
configuration>
plugin>
那么子类中POM文件需要添加如下:
<plugin>
<groupId>my.groupgroupId>
<artifactId>my-pluginartifactId>
<configuration>
<items>
<item>child-1item>
items>
<properties>
<childKey>childchildKey>
properties>
configuration>
父类和子类都有相同的groupId.artifactId plugin,Maven会合并名称相同配置元素。如果子类POM有一个属性,则该属性有效。如果子类没有属性,但父类存在,则会使用父类的属性。
根据这些规则,上面例子在Maven中将得到:
<plugin>
<groupId>my.groupgroupId>
<artifactId>my-pluginartifactId>
<configuration>
<items>
<item>child-1item>
items>
<properties>
<childKey>childchildKey>
<parentKey>parentparentKey>
properties>
configuration>
关于继承配置方法有没有其他方式嗯?
我们可以通过在子类POM中添加combine.children和combine.self属性完成子类继承父类配置。子类POM中使用这些属性来控制Maven如何将父窗口中的插件配置与子类POM显示配置相结合。
以下是两个属性插图的子配置:
<configuration>
<items combine.children="append">
<item>child-1item>
items>
<properties combine.self="override">
<childKey>childchildKey>
properties>
configuration>
最终得到的配置方式:
<configuration>
<items combine.children="append">
<item>parent-1item>
<item>parent-2item>
<item>child-1item>
items>
<properties combine.self="override">
<childKey>childchildKey>
properties>
configuration>
combine.children=”append”按照顺序结合父元素和子元素。 combine.self=”override”,覆盖父元素属性,使用子元素属性。 不能在一个元素上同时使用combine.self=”override”和combine.children=”append”,如果同时存在,将遵守”override”规则。
这些属性仅适用于它们在其上声明的配置元素,并且不会传播到嵌套元素。 也就是说,如果来自子POM的项目元素的内容是复杂的结构而不是文本,则其子元素仍然会受到默认合并策略的约束,除非它们本身被标记为属性。
combine.*属性从父POM继承。将这些属性添加到父POM时,可能会影响子POM。
dependencies
依赖关系,在POM经常使用,在plugins元素块作为一个元素存在。依赖关系具有与该基础构建相同的结构和功能。主要区别在与它们不再适用于项目的依赖关系,而是作为所属插件的依赖关系。
executions
plugin支持多个目标。每个目标可能有一个单独的配置,甚至可能将插件的目标绑定到不同的阶段。
在executions中配置execution。
如果你要绑定antrun:run目标的verif阶段,在此过程中我们系统输出构建目录,并且通过将inherited设置为false,避免将此配置传递给其子节点(假设它是父项),配置如下:
<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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
...
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-pluginartifactId>
<version>1.1version>
<executions>
<execution>
<id>echodirid>
<goals>
<goal>rungoal>
goals>
<phase>verifyphase>
<inherited>falseinherited>
<configuration>
<tasks>
<echo>Build Dir: ${project.build.directory}echo>
tasks>
configuration>
execution>
executions>
plugin>
plugins>
build>
project>
id
id,主键标识。用于区分其他execution。当这段被执行时,会以[plugin:goal execution: id]形式展现。上面例子展示为[antrun:run execution:echodir]
goals
plugin的execution的目标列表
phase
执行目标列表的阶段。
inherited
是否可以继承父POM属性
configuration
将配置限制在此特定目标列表中,而不是插件下的所有目标。
Plugin Management
pluginManagement元素配置方式与plugins配置使相同的。pluginManagement集成配置,子类需要。
在多模块化项目中,子模块项目会用到很多相同的jar、plugin,每个子模块POM文件都配置,造成冗余jar及不便管理。那么我们所有子模块pom.xml集成父pom.xml,父POM不会直接到repository中解析你定义的依赖不会下载依赖,而是在子POM dependencyManagement定义需要的dependency,子pom.xml只需要配置dependency的groupId.artifactId,Maven会依赖查找,而且版本已经在父POM定义不需要重复设置。
pluginManagement的作用类似于denpendencyManagement,只是denpendencyManagement是用于管理项目jar包依赖,pluginManagement是用于管理plugin。与pom build里的plugins区别是,这里的plugin是列出来,然后让子pom来决定是否引用。
如父pom.xml
<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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
...
<build>
...
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-jar-pluginartifactId>
<version>2.6version>
<executions>
<execution>
<id>pre-process-classesid>
<phase>compilephase>
<goals>
<goal>jargoal>
goals>
<configuration>
<classifier>pre-processclassifier>
configuration>
execution>
executions>
plugin>
plugins>
pluginManagement>
...
build>
project>
子pom.xml,子POM中决定引入哪个plugin
"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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
...
...
org.apache.maven.plugins
maven-jar-plugin
...
The Build Element Set
Build主要使用在POM project顶级元素下和profiles元素下。
可以表示为”project build” and “profile build”,上面介绍的元素都可以在两种方式下使用,还有两种方式是在profile build单独使用。
directories
目录元素集合存在于父构建元素中,它为整个POM设置了各种目录结构。 由于它们在配置文件构建中不存在,所以这些不能由配置文件更改。
"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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
...
${basedir}/src/main/java
${basedir}/src/main/scripts
${basedir}/src/test/java
${basedir}/target/classes
${basedir}/target/test-classes
...
当使用directory时,路径一般都设置为绝对路径。如果要使用相对路径,那么路径也是相对于${basedir}
Extensions
扩展是在此构建中使用的工件的列表。 它们将被包含在运行构建的类路径中。 它们可以启用对构建过程的扩展(例如为Wagon传输机制添加一个ftp提供程序),并使活动的插件能够对构建生命周期进行更改。 简而言之,扩展是在构建期间激活的工件。 扩展不需要实际执行任何操作,也不包含Mojo。 因此,扩展对于指定普通插件接口的多个实现中的一个是非常好的。
"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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
...
...
org.apache.maven.wagon
wagon-ftp
1.0-alpha-3
...
Reporting包含site生成阶段的一些元素。特定的Maven插件能产生定义和配置在reporting元素下的报告,例如:产生Javadoc报告。
Maven官网关于Reporting描述
Reporting contains the elements that correspond specifically for the site generation phase. Certain Maven plugins can generate reports defined and configured under the reporting element, for example: generating Javadoc reports. Much like the build element’s ability to configure plugins, reporting commands the same ability. The glaring difference is that rather than fine-grained control of plug-in goals within the executions block, reporting configures goals within reportSet elements. And the subtler difference is that a plugin configuration under the reporting element works as build plugin configuration, although the opposite is not true (a build plugin configuration does not affect a reporting plugin).
Possibly the only item under the reporting element that would not be familiar to someone who understood the build element is the Boolean excludeDefaults element. This element signifies to the site generator to exclude reports normally generated by default. When a site is generated via the site build cycle, a Project Info section is placed in the left-hand menu, chock full of reports, such as the Project Team report or Dependencies list report. These report goals are generated by maven-project-info-reports-plugin. Being a plugin like any other, it may also be suppressed in the following, more verbose, way, which effectively turns off project-info reports.
"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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
...
${basedir}/target/site
maven-project-info-reports-plugin
2.0.1
...
还有一个区别是输出路径默认为${basedir}/target/site
Report Sets
Report Sets设置多个目标。每个目标有不同的配置。
"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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
...
...
sunlink
javadoc
true
http://java.sun.com/j2se/1.5.0/docs/api/
...