
官方指导 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>