今天在导入以前写的maven项目时,遇到了一个问题,下面是pom.xml中的代码片段:
<plugin> <groupId>com.google.code.maven-replacer-plugin</groupId> <artifactId>replacer</artifactId> <version>1.5.2</version> <executions> <!-- Replace path token in mybatisGeneratorConfig.xml --> <execution> <id>Replace the path token in mybatisGeneratorConfig.xml</id> <phase>initialize</phase> <goals> <goal>replace</goal> </goals> <configuration> <file>${project.basedir}/src/main/resources/mybatisGeneratorConfig.xml</file> <replacements> <replacement> <token>solution.basedir</token> <value>${solution.basedir}</value> </replacement> </replacements> </configuration> </execution> <executions> <plugin>
在<execution>节点上老是报错:Plugin execution not covered by lifecycle configuration,如图:
为什么会出现上面的结果呢?就是因为我是在Eclipse中使用的maven,即m2eclipse。这里有eclipse官网的描述:https://www.eclipse.org/m2e/documentation/m2e-execution-not-covered.html
图中也给出了两种方式来解决,但这种直接在该Module中Ignore这个错误是不合适的,而且对于一个开发来说,不知道为什么要这样做也是不能满足的。于是经过一番搜索和折腾,发现使用<pluginManagement>元素将<plugins>标签包裹一下,也可以解决该问题(请参见:http://stackoverflow.com/questions/10483180/maven-what-is-pluginmanagement):
<build> <pluginManagement> <plugins> <plugin> ... </plugin> <plugin> ... </plugin> .... </plugins> </pluginManagement> </build>
那么,<pluginManagement>元素有什么作用呢?怀着好奇心,我接着去折腾,以下是答案:
From Maven documentation:
pluginManagement: is an element that is seen along side plugins. Plugin Management contains plugin elements in much the same way, except that rather than configuring plugin information for this particular project build, it is intended to configure project builds that inherit from this one. However, this only configures plugins that are actually referenced within the plugins element in the children. The children have every right to override pluginManagement definitions.
它和<plugin>元素有什么区别呢?
<pluginManagement/>
defines the settings for plugins that will be inherited by multiple modules in your build. This is great for cases where you have a parent pom file.
<plugins/>
is an actual invocation of the plugin. It may or may not be inherited from a <pluginManagement/>
.
需要注意的是:
You don't need to have a <pluginManagement/>
in your project, if it's not a parent POM. However, if it's a parent pom, then in the child's pom, you need to have a declaration like:
<plugins> <plugin> <groupId>com.foo</groupId> <artifactId>bar-plugin</artifactId> </plugin> </plugins>
Notice how you aren't defining any configuration. You can inherit it from the parent, unless you need to further adjust your invocation as per the child project's needs.
正好我出错的pom是一个child module,于是我就在parent中定义了以下内容:
<pluginManagement> <plugins> <!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself. --> <plugin> <groupId>org.eclipse.m2e</groupId> <artifactId>lifecycle-mapping</artifactId> <version>1.0.0</version> <configuration> <lifecycleMappingMetadata> <pluginExecutions> <!-- 在Parent Module的pom.xml中忽略 --> <pluginExecution> <pluginExecutionFilter> <groupId>com.google.code.maven-replacer-plugin</groupId> <artifactId>replacer</artifactId> <!-- 这个范围是指,版本大于1.5的都忽略 --> <versionRange>[1.5,)</versionRange> <goals> <!-- 这里的goal就是在原来child module中plugin定义的goal --> <goal>replace</goal> </goals> </pluginExecutionFilter> <action> <!-- 记住这个ignore元素 --> <ignore/> </action> </pluginExecution> </pluginExecutions> </lifecycleMappingMetadata> </configuration> </plugin> </plugins> </pluginManagement>
以下是在Parent Module中处理之后的截图:
以同样的方式,我们就可以在Parent Module中同时管理多个plugin的execution过程了。如果其他的maven plugin也报Plugin execution not covered by lifecycle configuration的错,将这些execution的信息都加入到Parent Module的<pluginManagement>元素中即可。
Stackoverflow上也有人使用以下方式,经本人测试可行,于是写在这里:
1. 在Eclipse的Workspace下找到以下文件(没有就创建):Workspace/.metadata/.plugins/org.eclipse.m2e.core/lifecycle-mapping-metadata.xml并打开;
2. 在<pluginExecutions>节点下粘贴刚刚在Parent Module中添加的内容,若没有该文件,创建一个,使得修改后的完整内容为:
<?xml version="1.0" encoding="UTF-8"?> <lifecycleMappingMetadata> <pluginExecutions> <pluginExecution> <pluginExecutionFilter> <groupId>com.google.code.maven-replacer-plugin</groupId> <artifactId>replacer</artifactId> <versionRange>[1.5,)</versionRange> <goals> <goal>replace</goal> </goals> </pluginExecutionFilter> <action> <ignore/> </action> </pluginExecution> </pluginExecutions> </lifecycleMappingMetadata>
当然,以后其他plugin execution需要ignore的,也可以在这里声明。
3. 修改文件之后,记得在Eclipse中:Windows -> Preferences -> Maven -> Lifecycle mappings,点击Reload workspace lifecycle mappings metadata使之生效即可。如图:
4. 如果还没生效,在project上 右键 -> Maven -> Update Project... 将更新应用到project中。
对于这两种方式,我个人觉得还是第一种更好使,因为第一种方式不依赖于Eclipse环境,在pom.xml中配置好后,可以在任何的eclipse中导入均不会有错。
参考网址:http://stackoverflow.com/questions/6352208/how-to-solve-plugin-execution-not-covered-by-lifecycle-configuration-for-sprin