13) Developing Java Plugins
官方指导 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 "Hi" 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.<br/>
Call <code>mvn hello:help -Ddetail=true -Dgoal=&lt;goal-name&gt;</code> 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 <code>true</code>, 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的更多相关文章
- 13、Java菜单条、菜单、菜单项
13.Java菜单条.菜单.菜单项 一般用Java做界面时,都得牵涉到菜单条.菜单.菜单项的设计.菜单项放在菜单里,菜单放在菜单条里,且其字体均可设置. 13.1.菜单条(Menubar) Frame ...
- macOs升级到10.13.1Beta || JAVA升级到最新版之后PhpStorm菜单栏问题
macOs升级到10.13.1Beta || JAVA升级到最新版之后PhpStorm菜单栏会消失,估计不止出现在PhpStorm,一系列jetbrains的产品可能都会有这个问题,包括eclipis ...
- 第13章 Java常用类
1.自动装箱和自动拆箱 自动装箱:基本类型就自动的封装到与它相同类型的包装中:如: 创建一个对象时:Integer i = 100;本质上是编译器编译时为我们添加了:Integer i = new I ...
- Java虚拟机13:Java类加载机制
前言 我们知道我们写的程序经过编译后成为了.class文件,.class文件中描述了类的各种信息,最终都需要加载到虚拟机之后才能运行和使用.而虚拟机如何加载这些.class文件?.class文件的信息 ...
- 13、Java并发编程:线程池的使用
Java并发编程:线程池的使用 在前面的文章中,我们使用线程的时候就去创建一个线程,这样实现起来非常简便,但是就会有一个问题: 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了, ...
- [转]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 ...
- 现如今,最热门的13个Java微服务框架
曾经的 服务器领域 有许多不同的芯片架构???有哪些芯片架构???和操作系统???,经过长期发展,Java的“一次编译,到处运行”使得它在服务器领域找到一席之地,成为程序员们的最爱. 本文,我们将和大 ...
- 开发错误记录13:java.lang.UnsatisfiedLinkError: Couldn't load xxx.so: findLibrary returned null
今天在导入环信开发包时,编译报如下错: java.lang.UnsatisfiedLinkError: Couldn't load hyphenate_av from loader dalvik.sy ...
- 13、java中的多态
1,多态的体现 父类的引用指向了自己的子类对象. 父类的引用也可以接收自己的子类对象.2,多态的前提 必须是类与类之间有关系.要么继承,要么实现. 通常还有一个前提:存在覆盖. 3,多态的好处 多态的 ...
随机推荐
- ubuntu16.04安装tensorflow-gpu和cuda8.0加速训练
转载请注明出处:http://www.cnblogs.com/buxizhizhoum/p/8086230.html 环境: 系统:ubuntu 16.04 cpu:i5 gpu:gt920m mem ...
- find命令之时间戳使用示例
查看当前目录以及子目录下哪些文件占用的空间最大: find ./ -type f -exec du -m {} \; | sort -nr | head find ./ -type f ...
- FileOperator 文件(夹)操作类
public class FileOperator { /** * 复制文件目录 * @param srcDir 要复制的源目录 eg:/mnt/sdcard/DB * @param destDir ...
- Linux iptables 备忘
iptables主要通过存储在linux内核中的一个个表来控制IP包的.可以想象成excel表格.你可以自定义所需的iptables表.不过已经内置了三张队列表. filter 这是默认的表,包含了内 ...
- 把AVI存在资源中用TAnimate播放
Animate1.RESName := 'About'; Animate1.Active := True;
- scala sparseVetor, SprseMatrix 实现
def rand(seed:Int):Double={ val rand=new Random(seed) rand.nextDouble()} def rand2(size:Int,seed:Int ...
- mongodb根据子项中的指标查找最小或最大值
假设students集合中有这样的数据: { "_id" : 1, "name" : "Aurelia Menendez", "s ...
- android笔记:Service
服务:在后台运行,没有界面的组件. 服务生命周期如下: 两种启动方式: 1.startService(): onCreate()-->onStartCommand()-->onDestro ...
- C & C++ 宏与const
1.宏定义函数: 例:#define do{exp} while(0)与#define exp有什么不同,好处在哪里? 定义复杂代码,防止分号,或是括号不匹配等错误.比如: 定义: #define s ...
- 几种常见的Web服务器
Apache与Tomcat的区别 ,几种常见的web/应用服务器 APACHE是一个web服务器环境程序 启用他可以作为web服务器使用 不过只支持静态网页 如(asp,php,cgi,jsp)等动态 ...