自定义maven插件中重写默认的生命周期

现在已能够编写简单的maven插件,不论是maven已有的插件,还是自己定义pluign,其默认的生命周期(lifecycle)有三个,即Clean Lifecycle、Default Lifecycle和Clean Lifecycle Bindings。而当我们有需要创建自己prallel的生命周期时,需要在自定义的maven项目中创建两个xml文件,chieve our goal:

首先,需要在默认的resource目录下建立两个文件夹,META-INF-maven 和 META-INF/nexus .这两个目录下面分别建立名为ifecycle.xml和components.xml 的文件。前者是用来override 默认的lifecycle,而后者将定义的lifecycle与phase进行映射。因为maven的默认生命周期分别就是在这两个文件中定义并引用执行的

    下面是lifecyclcle.xml的配置:

<lifecycles>
    <lifecycle>
        <id>zipcycle</id>
        <phases>
            <phase>
                <id>package</id>
                <executions>
                    <execution>
                        <goals>
                            <goal>zip</goal>
                        </goals>
                    </execution>
                </executions>
            </phase>
        </phases>
    </lifecycle>
</lifecycles>


这里我们在将定义的goal(zip)绑定到package阶段。



components.xml的配置如下:

<component-set>
    <components>
        <component>
            <role>org.apache.maven.lifecycle.mapping.LifecycleMapping</role>
            <role-hint>zip</role-hint>
            <implementation>
                org.apache.maven.lifecycle.mapping.DefaultLifecycleMapping
            </implementation>
            <configuration>
                <phases>
                    <process-resources>
                        org.apache.maven.plugins:maven-resources-plugin:resources
                    </process-resources>
                    <compile>
                        org.apache.maven.plugins:maven-compiler-plugin:compile
                    </compile>
                    <package>org.sonatype.mavenbook.plugins:maven-zip-plugin:zip</package>
                </phases>
            </configuration>
        </component>
    </components>
</component-set>




这里在package阶段绑定了自定义的zip,覆盖了maven默认的lifecycle。

当然,还需要实现extends AbstractMojo类并实现exetute()方法

<pre class="screen" style="padding: 9.5px; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 13px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; margin-top: 0px; margin-bottom: 10px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; border: 1px solid rgba(0, 0, 0, 0.14902); background-color: rgb(245, 245, 245);">/**
* @goal zip-fork
* @execute lifecycle="zipcycle" phase="package"
*/
public class ZipForkMojo extends AbstractMojo
{
public void execute()
throws MojoExecutionException
{
getLog().info( "**********************$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" );
}
}
 
</pre><pre class="null" name="code" style="color: rgb(61, 61, 61); font-family: Cabin; box-sizing: content-box; white-space: pre-wrap; margin-top: 0px; margin-bottom: 0px; padding: 0px; direction: ltr; border: 0px; vertical-align: baseline; overflow-x: auto; line-height: 19.5px; background-color: rgb(255, 255, 255);">
 
在项目中引用时,pom.xml文件中 配置如下:
<pre class="screen" style="padding: 9.5px; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 13px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; margin-top: 0px; margin-bottom: 10px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; border: 1px solid rgba(0, 0, 0, 0.14902); background-color: rgb(245, 245, 245);"><project>
    ...
    <build>
        ...
        <plugins>
            <plugin>
                <groupId>com.training.plugins</groupId>
                <artifactId>maven-zip-plugin</artifactId>
                <extensions>true</extensions>
            </plugin>
        </plugins>
    </build>
</project>
这一块,一定要加上 <extensions>true</extensions>,否则插件定义的周期不被识别。另外,需要定义多个goal与phase时,待续
 
 
 
 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值