官方指导 http://maven.apache.org/guides/plugin/guide-java-plugin-development.html

http://maven.apache.org/plugin-developers/

插件命名公约和 Apache Maven 商标

maven-<someplugin>-plugin 为官方的命名模式(Maven 的什么插件)

<yourplugin>-maven-plugin 非官方的插件命名模式(你的Maven插件)

1.根据骨架生成项目

mvn archetype:generate -DgroupId=cn.zno.plugin -DartifactId=hello-maven-plugin -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-plugin

前两个参数是设置自己的项目信息

后两个参数是指定具体的原型

点击查看所有参数

2. 遭遇问题

Plugin execution not covered by lifecycle configuration: org.apache.maven.plugins:maven-plugin-plugin:3.2:descriptor (execution: default-descriptor, phase: generate-resources)

解决办法:安装 e2m 插件。点击查看

3.新建自己的项目

E:.
│ pom.xml

└─src
└─main
└─java
└─cn
└─zno
└─plugin
GreetingMojo.java

pom.xml

<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>cn.zno.plugin</groupId>
<artifactId>hello-maven-plugin</artifactId> <version>1.0</version>
<packaging>maven-plugin</packaging> <name>hello-maven-plugin Maven Plugin</name> <!-- FIXME change it to the project's website -->
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.2</version>
<scope>provided</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.2</version>
<configuration>
<goalPrefix>hello</goalPrefix>
<skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
</configuration>
<executions>
<execution>
<id>mojo-descriptor</id>
<goals>
<goal>descriptor</goal>
</goals>
</execution>
<execution>
<id>help-goal</id>
<goals>
<goal>helpmojo</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
GreetingMojo.java
package cn.zno.plugin;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo; /**
* Says "Hi" to the user.
*
*/
@Mojo( name = "sayhi")
public class GreetingMojo extends AbstractMojo
{
public void execute() throws MojoExecutionException
{
getLog().info( "Hello, world." );
}
}

4.生成插件

进入项目所在目录执行 mvn install

E:\e\workspace\hello-maven-plugin>mvn install
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building hello-maven-plugin Maven Plugin 1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-plugin-plugin:3.2:helpmojo (help-goal) @ hello-maven-plugin ---
[INFO] Using 'UTF-8' encoding to read mojo metadata.
[INFO] Applying mojo extractor for language: java-annotations
[INFO] Mojo extractor for language: java-annotations found mojo descriptors.
[INFO] Applying mojo extractor for language: java
[INFO] Mojo extractor for language: java found mojo descriptors.
[INFO] Applying mojo extractor for language: bsh
[INFO] Mojo extractor for language: bsh found mojo descriptors.
[INFO]
[INFO] --- maven-plugin-plugin:3.2:descriptor (default-descriptor) @ hello-maven-plugin ---
[INFO] Using 'UTF-8' encoding to read mojo metadata.
[INFO] Applying mojo extractor for language: java-annotations
[INFO] Mojo extractor for language: java-annotations found mojo descriptors.
[INFO] Applying mojo extractor for language: java
[INFO] Mojo extractor for language: java found mojo descriptors.
[INFO] Applying mojo extractor for language: bsh
[INFO] Mojo extractor for language: bsh found mojo descriptors.
[INFO]
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ hello-maven-plugin ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:\e\workspace\hello-maven-plugin\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.:compile (default-compile) @ hello-maven-plugin ---
[INFO] Compiling source file to E:\e\workspace\hello-maven-plugin\target\classes
[INFO]
[INFO] --- maven-plugin-plugin:3.2:descriptor (mojo-descriptor) @ hello-maven-plugin ---
[INFO] Using 'UTF-8' encoding to read mojo metadata.
[INFO] Applying mojo extractor for language: java-annotations
[INFO] Mojo extractor for language: java-annotations found mojo descriptors.
[INFO] Applying mojo extractor for language: java
[INFO] Mojo extractor for language: java found mojo descriptors.
[INFO] Applying mojo extractor for language: bsh
[INFO] Mojo extractor for language: bsh found mojo descriptors.
[INFO]
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ hello-maven-plugin ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:\e\workspace\hello-maven-plugin\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.:testCompile (default-testCompile) @ hello-maven-plugin ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.10:test (default-test) @ hello-maven-plugin ---
[INFO] Surefire report directory: E:\e\workspace\hello-maven-plugin\target\surefire-reports -------------------------------------------------------
T E S T S
------------------------------------------------------- Results : Tests run: , Failures: , Errors: , Skipped: [INFO]
[INFO] --- maven-jar-plugin:2.3.:jar (default-jar) @ hello-maven-plugin ---
[INFO] Building jar: E:\e\workspace\hello-maven-plugin\target\hello-maven-plugin-1.0.jar
[INFO]
[INFO] --- maven-plugin-plugin:3.2:addPluginArtifactMetadata (default-addPluginArtifactMetadata) @ hello-maven-plugin --
-
[INFO]
[INFO] --- maven-install-plugin:2.3.:install (default-install) @ hello-maven-plugin ---
[INFO] Installing E:\e\workspace\hello-maven-plugin\target\hello-maven-plugin-1.0.jar to C:\Documents and Settings\Admin
istrator\.m2\repository\cn\zno\plugin\hello-maven-plugin\1.0\hello-maven-plugin-1.0.jar
[INFO] Installing E:\e\workspace\hello-maven-plugin\pom.xml to C:\Documents and Settings\Administrator\.m2\repository\cn
\zno\plugin\hello-maven-plugin\1.0\hello-maven-plugin-1.0.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: .984s
[INFO] Finished at: Sun Sep :: CST
[INFO] Final Memory: 14M/34M
[INFO] ------------------------------------------------------------------------

查看\META-INF\maven\plugin.xml

<?xml version="1.0" encoding="UTF-8"?>

<!-- Generated by maven-plugin-tools 3.2 on 2015-09-06 -->

<plugin>
<name>hello-maven-plugin Maven Plugin</name>
<description></description>
<groupId>cn.zno.plugin</groupId>
<artifactId>hello-maven-plugin</artifactId>
<version>1.0</version>
<goalPrefix>hello</goalPrefix>
<isolatedRealm>false</isolatedRealm>
<inheritedByDefault>true</inheritedByDefault>
<mojos>
<mojo>
<goal>sayhi</goal>
<description>Says &quot;Hi&quot; to the user.</description>
<requiresDirectInvocation>false</requiresDirectInvocation>
<requiresProject>true</requiresProject>
<requiresReports>false</requiresReports>
<aggregator>false</aggregator>
<requiresOnline>false</requiresOnline>
<inheritedByDefault>true</inheritedByDefault>
<implementation>cn.zno.plugin.GreetingMojo</implementation>
<language>java</language>
<instantiationStrategy>per-lookup</instantiationStrategy>
<executionStrategy>once-per-session</executionStrategy>
<threadSafe>false</threadSafe>
<parameters/>
</mojo>
<mojo>
<goal>help</goal>
<description>Display help information on hello-maven-plugin.&lt;br/&gt;
Call &lt;code&gt;mvn hello:help -Ddetail=true -Dgoal=&amp;lt;goal-name&amp;gt;&lt;/code&gt; to display parameter details.</description>
<requiresDirectInvocation>false</requiresDirectInvocation>
<requiresProject>false</requiresProject>
<requiresReports>false</requiresReports>
<aggregator>false</aggregator>
<requiresOnline>false</requiresOnline>
<inheritedByDefault>true</inheritedByDefault>
<implementation>cn.zno.plugin.HelpMojo</implementation>
<language>java</language>
<instantiationStrategy>per-lookup</instantiationStrategy>
<executionStrategy>once-per-session</executionStrategy>
<threadSafe>true</threadSafe>
<parameters>
<parameter>
<name>detail</name>
<type>boolean</type>
<required>false</required>
<editable>true</editable>
<description>If &lt;code&gt;true&lt;/code&gt;, display all settable properties for each goal.</description>
</parameter>
<parameter>
<name>goal</name>
<type>java.lang.String</type>
<required>false</required>
<editable>true</editable>
<description>The name of the goal for which to show help. If unspecified, all goals will be displayed.</description>
</parameter>
<parameter>
<name>indentSize</name>
<type>int</type>
<required>false</required>
<editable>true</editable>
<description>The number of spaces per indentation level, should be positive.</description>
</parameter>
<parameter>
<name>lineLength</name>
<type>int</type>
<required>false</required>
<editable>true</editable>
<description>The maximum length of a display line, should be positive.</description>
</parameter>
</parameters>
<configuration>
<detail implementation="boolean" default-value="false">${detail}</detail>
<goal implementation="java.lang.String">${goal}</goal>
<indentSize implementation="int" default-value="2">${indentSize}</indentSize>
<lineLength implementation="int" default-value="80">${lineLength}</lineLength>
</configuration>
</mojo>
</mojos>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<type>jar</type>
<version>2.0</version>
</dependency>
</dependencies>
</plugin>

5. 新建插件测试项目

pom.xml

<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.zno</groupId>
<artifactId>test-hello-maven-plugin</artifactId>
<version>0.0.1-SNAPSHOT</version> <build>
<plugins>
<plugin>
<groupId>cn.zno.plugin</groupId>
<artifactId>hello-maven-plugin</artifactId>
<version>1.0</version>
</plugin>
</plugins>
</build>
</project>

6. 测试

进入到测试项目根目录执行 mvn hello:sayhi

E:\e\workspace\test-hello-maven-plugin>mvn hello:sayhi
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building test-hello-maven-plugin 0.0.-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- hello-maven-plugin:1.0:sayhi (default-cli) @ test-hello-maven-plugin ---
[INFO] Hello, world.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: .203s
[INFO] Finished at: Sun Sep :: CST
[INFO] Final Memory: 4M/15M
[INFO] ------------------------------------------------------------------------

7.设置参数

修改GreetingMojo.java 文件

package cn.zno.plugin;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter; /**
* Says "Hi" to the user.
*
*/
@Mojo(name = "sayhi")
public class GreetingMojo extends AbstractMojo { @Parameter( property = "sayhi.greeting", defaultValue = "Hello World!" )
private String greeting; public void execute() throws MojoExecutionException {
getLog().info(greeting);
}
}

修改插件测试项目

<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.zno</groupId>
<artifactId>test-hello-maven-plugin</artifactId>
<version>0.0.1-SNAPSHOT</version> <build>
<plugins>
<plugin>
<groupId>cn.zno.plugin</groupId>
<artifactId>hello-maven-plugin</artifactId>
<version>1.0</version>
<configuration>
<greeting>tttttttttttttta</greeting>
</configuration>
</plugin>
</plugins>
</build>
</project>

执行mvn hello:sayhi

E:\e\workspace\test-hello-maven-plugin>mvn hello:sayhi
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building test-hello-maven-plugin 0.0.-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- hello-maven-plugin:1.0:sayhi (default-cli) @ test-hello-maven-plugin ---
[INFO] tttttttttttttta
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: .219s
[INFO] Finished at: Sun Sep :: CST
[INFO] Final Memory: 4M/15M
[INFO] ------------------------------------------------------------------------

8.通过命令行动态设置参数

只需要修改插件测试项目配置文件

<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.zno</groupId>
<artifactId>test-hello-maven-plugin</artifactId>
<version>0.0.1-SNAPSHOT</version> <build>
<plugins>
<plugin>
<groupId>cn.zno.plugin</groupId>
<artifactId>hello-maven-plugin</artifactId>
<version>1.0</version>
<configuration>
<greeting>${greeting}</greeting>
</configuration>
</plugin>
</plugins>
</build>
</project>

测试执行命令 mvn hello:sayhi -Dgreeting=22222222222

E:\e\workspace\test-hello-maven-plugin>mvn hello:sayhi -Dgreeting=
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building test-hello-maven-plugin 0.0.-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- hello-maven-plugin:1.0:sayhi (default-cli) @ test-hello-maven-plugin ---
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: .219s
[INFO] Finished at: Sun Sep :: CST
[INFO] Final Memory: 4M/15M
[INFO] ------------------------------------------------------------------------

9.备注

[ERROR] No plugin found for prefix 'jetty' in the current project and in the plugin groups [org.apache.maven.plugins, or
g.codehaus.mojo] available from the repositories [local (C:\Documents and Settings\Administrator\.m2\repository), centra
l (http://repo.maven.apache.org/maven2)] -> [Help 1]

执行插件的命令格式

You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>.

1. 在项目pom.xml 文件中配置或者

<build>
<plugins>
<plugin>
<groupId>cn.zno.plugin</groupId>
<artifactId>hello-maven-plugin</artifactId>
<version>1.0</version>
</plugin>
</plugins>
</build>

2. 在maven 的 settings.xml 文件中配置

  <pluginGroups>
<pluginGroup>cn.zno.plugin</pluginGroup>
</pluginGroups>

13) Developing Java Plugins的更多相关文章

  1. 13、Java菜单条、菜单、菜单项

    13.Java菜单条.菜单.菜单项 一般用Java做界面时,都得牵涉到菜单条.菜单.菜单项的设计.菜单项放在菜单里,菜单放在菜单条里,且其字体均可设置. 13.1.菜单条(Menubar) Frame ...

  2. macOs升级到10.13.1Beta || JAVA升级到最新版之后PhpStorm菜单栏问题

    macOs升级到10.13.1Beta || JAVA升级到最新版之后PhpStorm菜单栏会消失,估计不止出现在PhpStorm,一系列jetbrains的产品可能都会有这个问题,包括eclipis ...

  3. 第13章 Java常用类

    1.自动装箱和自动拆箱 自动装箱:基本类型就自动的封装到与它相同类型的包装中:如: 创建一个对象时:Integer i = 100;本质上是编译器编译时为我们添加了:Integer i = new I ...

  4. Java虚拟机13:Java类加载机制

    前言 我们知道我们写的程序经过编译后成为了.class文件,.class文件中描述了类的各种信息,最终都需要加载到虚拟机之后才能运行和使用.而虚拟机如何加载这些.class文件?.class文件的信息 ...

  5. 13、Java并发编程:线程池的使用

    Java并发编程:线程池的使用 在前面的文章中,我们使用线程的时候就去创建一个线程,这样实现起来非常简便,但是就会有一个问题: 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了, ...

  6. [转]JetBrains IntelliJ IDEA 13 Keygen (Java Source Code)

    转载:http://www.rover12421.com/2013/12/09/jetbrains-intellij-idea-13-keygen-java-source-code.html JetB ...

  7. 现如今,最热门的13个Java微服务框架

    曾经的 服务器领域 有许多不同的芯片架构???有哪些芯片架构???和操作系统???,经过长期发展,Java的“一次编译,到处运行”使得它在服务器领域找到一席之地,成为程序员们的最爱. 本文,我们将和大 ...

  8. 开发错误记录13:java.lang.UnsatisfiedLinkError: Couldn't load xxx.so: findLibrary returned null

    今天在导入环信开发包时,编译报如下错: java.lang.UnsatisfiedLinkError: Couldn't load hyphenate_av from loader dalvik.sy ...

  9. 13、java中的多态

    1,多态的体现 父类的引用指向了自己的子类对象. 父类的引用也可以接收自己的子类对象.2,多态的前提 必须是类与类之间有关系.要么继承,要么实现. 通常还有一个前提:存在覆盖. 3,多态的好处 多态的 ...

随机推荐

  1. pyDes库 实现python的des加密

    下载及简介地址:https://twhiteman.netfirms.com/des.html 如需要在python中使用des加密,可以直接使用pyDes库加密,该库提供了CBC和ECB两种加密方式 ...

  2. linux替换字符串的几种方法

    1. 基本替换:s/str1/str2/ 替换当前行第一个str1为str2:s/str1/str2/g 替换当前行所有str1为str2:n,$s/str1/str2/ 替换第 n 行开始到最后一行 ...

  3. 自己整理lnmp安装

    1. 操作系统   CentOS release 6.5(final)   2. 安装mysql   # yum install mysql-server   #vi /etc/my.cnf +def ...

  4. hdoj1074--Doing Homework (DP 状态压缩)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1074 思路: 看着数据很小,15,但是完成的顺序有15!情况,这么大的数据是无法实现的.上网查才知道要 ...

  5. 第六章 图(b1)邻接矩阵

  6. java POI创建Excel示例(xslx和xsl区别 )

    Java用来处理office类库有很多,其中POI就是比较出名的一个,它是apache的类库,现在版本到了3.10,也就是2014年2月8号这个版本. 在处理PPT,Excel和Word前,需要导入以 ...

  7. 对.NET中导出数据到EXCEL的几种方法探讨

    最近在做一个报表系统的时候,需要把DATASET中的数据导到EXCEL当中,于是在网上找了一遍,发现了好几种方法,本来以为应该差不多,但后来经过一一试用后,发现在性能上真的差别很大,现在就介绍一下,同 ...

  8. 简述Markdown的使用方法

    MarkdownPad Document Markdown的使用技巧 一.标题 一个”#“表示H1.“##”表示H2... 二.列表 第一点 第二点 注意1.2. -与文本之间要有一个空格 这一点 三 ...

  9. TokuMX唯一索引不支持dropDups选项

    TokuMX v1.5.0的唯一索引(unique index)不支持dropDups选项, 如果源数据包含相同目标key的文档,将无法建立唯一索引. 问题场景: 从MongoDB到TokuMX的数据 ...

  10. go实现的简易TCP的客户端和服务器

    今天介绍golang版本的通信基础:基于TCP的客户端和服务器实现,参考书籍:The Way To Go 那时学习java的时候也是做过通信的,当时是socket编程,服务器监听某一个端口,然后客户机 ...