官方指导 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. python, Django 的安装

    yum install zlib yum install zlib-devel yum install openssl-devel 否则导致安装setuptools出错,还需要重新编译python 如 ...

  2. ArcMap导入图层出现General function failure问题

    问题描述: 使用ArcMap的FeatureClassToFeatureClass命令导入图层,出现如下图的错误提示: 解决方法: 参考http://forums.esri.com/thread.as ...

  3. python的读写和写读操作

    # 读写操作 (读写操作是正常的)f = open('log',mode='r+',encoding='utf-8') # log是文件名 print(f.read()) f.write(" ...

  4. MySQL语句相关

    一.增加 1.基本 2.集合 3.组合 二.删除 1.基本 2.集合 3.组合 1.一个表的查询结果作为另一个表的插入字段之一 <insert id="方法" paramet ...

  5. mongo嗅探器mongosniff

    mongo嗅探器 在更高版本被mongoreplay取代. 安装: 在Ubuntu直接apt-get install mongodb即包含有. 使用方法 直接--help查看使用方法,一般使用: mo ...

  6. 最小重组缓冲区和路径MTU发现

    概括: 主要来源于unp,可参考:http://blog.csdn.net/ysu108/article/details/7764461 疑惑: 1. 最小重组缓冲区大小: ipv4为576,ipv6 ...

  7. SSH框架整合jar包时的注意事项

    SSH框架整合jar包时的注意事项: 在将三个框架所需的jar整合到一起后,要看一下有没有相同类型但是版本不同的jar包,如果有的话,需要把低版本的jar包删除掉,否则会报错.我这里整合的时候java ...

  8. DataTable的序列化和反序列化

    /// <summary> /// DataTable序列化 /// </summary> /// <param name="dt"></ ...

  9. .NET资源文件实现多语言切换

    1.创建对应的资源文件 lang.en.resx  英文 lang.resx   中文,默认 lang.zh-tw.resx  繁体 首先说明,这三个文件前面部分名称需要一样,只是 点 后面的语言代号 ...

  10. jps 命令详解

    jps 命令详解 jps 是 jdk 提供的一个查看当前 java 进程的小工具, 可以看做是 JavaVirtual Machine Process Status Tool 的缩写.非常简单实用. ...