开发自己的Maven插件之一:hello world

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

一直在使用Maven,用了各种各样的插件,但是有时候没有的话,还是需要自己写点。写一个插件并不难,会写插件的另一个好处就是了解了更多的Maven工作机制的内幕。对更好的使用Maven有帮助。

首先创建一个Maven项目,名叫plugin-example1。

这里要理解一个术语:mojo,就是Maven Plain Old Java Object,也就是一个普通的Java类。
我们需要mojo的api库,所以在pom.xml中添加一个依赖:

  <dependencies>    <dependency>      <groupId>org.apache.maven</groupId>      <artifactId>maven-plugin-api</artifactId>      <version>2.0</version>    </dependency>  </dependencies>
创建一个Example类,继承于AbstractMojo,实现execute方法。
代码很简单:
public class Example extends AbstractMojo{    public void execute() throws MojoExecutionException, MojoFailureException {        getLog().info("Hello world");    }    }
getLog()获取的是AbstractMojo内部的log,类型是:org.apache.maven.plugin.logging.Log;
至少在Mojo的开发中,不要使用其他的Log基础设施。


现在修改一下工程的描述信息:

  <groupId>org.freebird</groupId>  <artifactId>plugin-example1</artifactId>  <version>1.0-SNAPSHOT</version>  <packaging>maven-plugin</packaging>  <name>plugin-example1</name>  <url>http://maven.apache.org</url>  <properties>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  </properties>

注意,packaging的值是maven-plugin

现在编译吧,很快就遇到错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-plugin-plugin:2.9:descriptor (default-descriptor) on project plugin-example1: Error extracting plugin descriptor: 'No mojo definitions were found for plugin: org.freebird:plugin-example1.' -> [Help 1]org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-plugin-plugin:2.9:descriptor (default-descriptor) on project plugin-example1: Error extracting plugin descriptor: 'No mojo definitions were found for plugin: org.freebird:plugin-example1.'

需要加入一个maven-plugin-plugin来生成descriptor。不知道Maven的官方文档中为什么不提。

  <build>    <plugins>      <plugin>        <groupId>org.apache.maven.plugins</groupId>        <artifactId>maven-plugin-plugin</artifactId>        <version>3.0</version>        <executions>        </executions>        <configuration>          <!-- Needed for Java 5 annotation based configuration, for some reason. -->          <skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>        </configuration>      </plugin>    </plugins>  </build>

这样编译就通过了。或者在类的注视上添加一个descriptor:

/** * * @goal sayhi */public class Example extends AbstractMojo{


为了将Maven部署在私服上,需要加上如下配置:

  <distributionManagement>    <snapshotRepository>      <id>snapshots</id>      <url>http://your_server:8080/nexus/content/repositories/snapshots</url>    </snapshotRepository>  </distributionManagement>

然后运行mvn clean package deploy

部署成功。






           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值