Eden

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

在eclipse中安装了MAVEN插件M2Eclipse之后,每当修改了maven project中的任何一个文件,都会触发自动build,console输出如下:Maven Builder: AUTO_BUILD

这背后到底执行了哪些动作,如何才能控制这一过程,加入其他插件的执行? 我个人发现有2种便捷的方式:

使用自定义lifecycle-mapping插件去覆盖M2E插件的默认行为

 <plugin>
  <groupId>org.maven.ide.eclipse</groupId>
  <artifactId>lifecycle-mapping</artifactId>
  <version>0.10.0</version>
  <configuration>
   <mappingId>customizable</mappingId>
   <configurators>
    <configurator id='org.maven.ide.eclipse.jdt.javaConfigurator' />
   </configurators>
   <mojoExecutions>
    <!-- groupId:artifactId:[version]:[goal[,goal]*] -->
    <!-- version and goal(s) parts are optional, while groupId, artifactId
     and colons are not. If version is omitted, the mask will match any version.
     Likewise, if goal is omitted, the mask will match any goal -->
    <mojoExecution>org.apache.maven.plugins:maven-resources-plugin::</mojoExecution>
    <mojoExecution>org.apache.maven.plugins:maven-websources-plugin::</mojoExecution>
   </mojoExecutions>
  </configuration>
</plugin>

注意红色字体涉及到三种lifecycle mapping strategy,自定义lifecycle时使用了customizable(另外2个是"generic"和"Noop")

褐色部分是要使用的configurators,如果不知道要使用哪些,可以在没有使用lifecycle-mapping插件的情况下,参考project -> properties -> Maven -> Lifecycle Mapping中Project Configurators中显示的id

蓝色部分则是配置要执行哪些插件的哪个GOAL,格式为:groupId:artifactId:[version]:[goal[,goal]*] ,其中 version和goal如果为空,则匹配任务版本的所有GOAL.在这里,我们就可以加入一些自定义的插件.这样在每次项目中任一文件修改好,都会被触发执行.

通过m2e插件的Lifecycle Mapping进行设置

 第一种会让项目依赖一个与eclipse相关的插件,让人感觉不清爽.以下这种方式是利用M2E和MAVEN已经提供的功能,进行一些组合,以达到同样的效果:

  1. 首先在project -> properties -> Maven -> Lifecycle Mapping中可以看到Generic Lifecycle Mapping有2个Goals设置,其中一个是当resources发生变化时执行的goals.这里的resources对应了maven pom.xml中的 <build><resources><resource> ,这里<resource>可以设置多个.
  2. 将项目里的想要被监视的文件夹设置为<resource>,则当此文件夹下的文件发生变化时,会激活配置的goals以及所属阶段之前的所有默认goals和POM中通过<executions><execution><goals><goal>指定的goals
  3. 举个例子:

 

代码
<build>
<finalName>ebiz-support</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/webapp</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-websources-plugin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<executions>
<execution>
<goals>
<goal>webSources</goal>
</goals>
</execution>
</executions>
</plugin>

 

 

这里,我将src/main/webapp设置为了resource,这样,这个文件夹下的文件一旦变化,将激活"process-resources resources:testResources"(默认的goal),而maven-websources-plugin是我自定义的插件,绑定的是@phase process-resources ,跟上述配置的phase吻合,所以就会被调用.

通过这种方式,我们变相的自定义了M2E的的生命周期.

posted on 2010-11-02 10:55  Johney  阅读(15179)  评论(2编辑  收藏  举报