Maven自定义plugin

  Maven作为一款流行的项目管理及构建工具,因为其使用方便,众多出色的功能以及丰富的插件而受到欢迎。尽管如此众多的插件为我们提供了几乎所需要的所有功能,但我还是想进一步了解如何编写一个自定义插件呢。那么按照下面的步骤我们就可以完成一个自定义的插件了。

  先来看看我的环境吧。

  Environment

 

  如果你熟悉Maven的话,那你应该知道Maven为我们提供了简单的命令来构建各种项目的原型框架。现在我们需要创建一个Archetype为maven-archetype-mojo的项目。你可以在命令行中输入mvn archetype:generate,选择archetype类型来创建我们需要的项目原型,或者利用IDE来快速创建。

 

  来看看我的pom文件

 

<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 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.sayhi</groupId>
    <artifactId>sayhi</artifactId>
    <packaging>maven-plugin</packaging>
    <version>1.0</version>
    <name>SayHelloPlugin Maven Mojo</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-plugin-api</artifactId>
            <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

 

 

  再来看看我的类吧

 

package SayHelloPlugin;

/*
 * Copyright 2001-2005 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;

/**
 * Goal which touches a timestamp file.
 *
 * @goal sayhello
 */
public class MyMojo extends AbstractMojo {

    public void execute() throws MojoExecutionException {
        Log log = this.getLog();
        log.info("\n");
        log.info("=============================");
        log.info("This is a maven plugin test.");
        log.info("=============================");
        log.info("\n");
    }

}

 

  当然,这个类的作用仅仅是为了说明我们的插件实现必须继承AbstractMojo这个类而已,除此之外没有其他作用。

 

  如果你实现了你自己的插件类后。那么下面我们就需要在命令行进入我们的项目目录,输入mvn:install命令了,这样可以帮我们把项目打包并部署到我们本地的repository中去。

 

  如果想要调用我们的插件呢,你需要在命令行中输入如下命令:mvn groupid:artifactId:goal。

 

  如果你想要将你的插件部署到服务器上,那么你就需要mvn:deploy命令。

 

  详情请参见附件。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值