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,多态的好处 多态的 ...
随机推荐
- pycharm ideavimrc设置备忘
文件存放位置 windows下 C:\Users\你的用户名\.ideavimrc 注:如果要映射pycharm 中的一些命令可以 在pycharm 中 edit->Macros->Sta ...
- scrollTop实现图像循环滚动(实例1)
<html><head><title>scrollTop实现图像循环滚动(实例1)</title><meta http-equiv="C ...
- PHP与apache配置
在apache 的安装路径中找到 \conf\httpd.conf文件 在 LoadModule最后面添加如下代码: PHPIniDir "D:\PHP"LoadModule ph ...
- zset
zset sorted set,有序集合 元素为string类型 元素具有唯一性,不重复 每个元素都会关联一个double类型的score,表示权重,通过权重将元素从小到大排序 说明:没有修改操作 增 ...
- memory management
1. 高端内存: 内存的物理寻址范围比虚拟寻址范围大的多,有一些内存页不能永久的映射到内核地址空间. 2. 高端内存和低端内存是内核对内存物理页的划分. 参考:http://ilinuxkernel. ...
- 使用git提交代码到GitHub
0.下载Git Bash,在Windows系统可以用Git Bash通过简单的命令将代码提交到GitHub1.打开项目所在的文件夹,右键,"Git Bash Here"2.初次使用 ...
- 超薄二维Mo2C晶体
记者今天从中国科学院金属研究所获悉,该所沈阳材料科学国家(联合)实验室先进炭材料研究部任文才研究组在大尺寸高质量二维过渡族金属碳化物晶体的制备与物性研究方面取得了重要突破.相关成果日前在<自然— ...
- 警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:Weixin' did not find a matching property.
警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclips ...
- Bom对象介绍
1.windows对象 1.windows对象 1_1.alert:显示一段消息和确认按钮的弹出的警告框 我们平时用的alert的全称就是 window.alert("hahah" ...
- SQL时间格式化 转载备用~
Sel1 取值后格式化{0:d}小型:如2005-5-6{0:D}大型:如2005年5月6日{0:f}完整型 2 当前时间获取 DateTime.Now.ToShortDateString 3 取值中 ...