maven pom进阶教程 - 插件与继承(plugin & inherite)

插件是maven的核心,全部的功能都要经过插件来完成。 #插件配置的基本格式 本例是以maven-resources-plugin为例apache

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-resources-plugin</artifactId>
	<version>3.0.2</version>
	<inherited>true</inherited>
	<configuration><!--有的插件识别这个位置,有的不识别,有的都识别-->
		<skip>false</skip><!--只有部分插件支持这个功能-->
	</configuration>
	<executions>
		<execution>
			<id>default-resources</id>
			<phase>process-resources</phase>
			<goals>
				<goal>resources</goal>
			</goals>
			<configuration><!--有的插件不识别这个位置的configuration-->
				<skip>true</skip><!--只有部分插件支持这个功能-->
			</configuration>
		</execution>
		<execution>
			<id>copy2</id>
			<phase>compile</phase>
			<goals>
				<goal>copy-resources</goal>
			</goals>
			<configuration>
				<skip>false</skip><!--只有部分插件支持这个功能-->
			</configuration>
		</execution>
	</executions>
</plugin>

inherited 插件是容许被继承。好比child使用parent标签继承其它的pom.xml, parent的pom.xml能够将插件的inherited设置为false, 从而避免child继承某个插件。app

configuration 从示例能够看出,configuration的位置有两处:   1. 处于plugin结点   2. 处于execution结点 有的插件只识别位置1,好比exec-maven-plugin;
有的插件两个位置都识别,并采用继承的规则(见[插件的继承与覆盖]),好比maven-resources-plugin。
所以,最好是按照插件的官方例子来写, 避免没必要要的调试。eclipse

skip 是否跳过execution, 只有部分插件支持这个功能,具体信息须要查看插件的参数。maven

executions,execution, 定义任务列表ide

id, 定义任务的id, 至关于任务名称,能够随便写,若是不写,则默认值为default(实践得出来的)。同个插件内的execution的id, 不能重复, 因此,包含多个任务时,须要设置为不一样的id。maven的phase绑定的默认的插件的execution id的格式为default-<Goal>
执行mvn相关命令时,console上会打印被执行的execution的id, 例如:
[INFO] --- maven-compiler-plugin:3.6.0:compile (default-compile) @ my-child2 ---

[INFO] --- 插件的artifactId:插件的version:插件的goal (execution的id) @ 项目的artifactId --- 工具

phase, 设置execution在哪一个阶段执行。phase乱写,将致使execution不被执行。开发工具

goals,goal, 须要执行插件的哪些goal, 插件有哪些goal, 能够去官网上查。若是乱写,mvn命令将失败ui

#插件的继承与覆盖 若是pom继承了其它的pom.xml, plugin也会产生继承关系。parent中的plugin能够设置成没法被继承(彷佛没什么意义)。默认状况下,maven会将parent和child的配置合并(若是元素存在,则覆盖)。以maven-resources-plugin来举例,parent的配置以下插件

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-resources-plugin</artifactId>
	<version>3.0.2</version>
	<configuration>
		<outputDirectory>${project.basedir}/target/</outputDirectory>
		<resources>
			<resource>
				<directory>src/main/resources</directory>
			</resource>
		</resources>
		<encoding>UTF-8</encoding>
	</configuration>
</plugin>

child配置以下调试

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-resources-plugin</artifactId>
	<configuration>
		<resources>
			<resource>
				<directory>src/main/resources2</directory>
			</resource>
		</resources>
	</configuration>
</plugin>

child会继承version, outputDirectory, encoding这些属性,可是会覆盖resouces属性, 最终只拷贝了resource2中的资源
child能够在给元素加入 combine.children="append",将child的此元素属性与parent的属性拼接到一块儿, 例如:

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-resources-plugin</artifactId>
	<configuration>
		<resources combine.children="append">
			<resource>
				<directory>src/main/resources2</directory>
			</resource>
		</resources>
	</configuration>
</plugin>

这样配置后,child将同时拷贝resouce和resource2中的资源. child也可使用 combine.self="override",彻底覆盖parent的属性
在开发工具eclipse中,编辑pom.xml时,可切换成Effective POM视图,查看最终生成的pom配置
#pluginManagement 在<build>结点下,plugins能够处于两个地方
1 pluginManagement结点下

<build>
		<pluginManagement>
			<plugins></plugins>
		</pluginManagement>
	</build>
    ```  
2  <build>根结点下
<build>
	<plugins></plugins>
</build>
这两者的惟一区别是,只有当child的pom.xml使用了某个插件,pluginManagement下的对应插件才会生效.  
**!可是,须要注意的是,有些插件会被自动调用,好比将resources插件写在pluginManagement,不管child有没有使用resources插件,它都会生效,因process-resources阶段已经绑定了它,确定会被调用**
相关文章
相关标签/搜索