maven 3 插件开发

原文地址:http://my.oschina.net/caiyuan/blog/41850

参考地址:http://suhuanzheng7784877.iteye.com/category/138308

编写插件

01<?xmlversion="1.0"encoding="UTF-8"?>
02<projectxmlns="http://maven.apache.org/POM/4.0.0"
03         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
04         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
05    <modelVersion>4.0.0</modelVersion>
06 
07    <groupId>org.caiyuan.maven</groupId>
08    <artifactId>test-maven-plugin</artifactId>
09    <version>1.0</version>
10    <packaging>maven-plugin</packaging>
11 
12    <dependencies>
13        <dependency>
14            <groupId>org.apache.maven</groupId>
15            <artifactId>maven-plugin-api</artifactId>
16            <version>3.0.3</version>
17        </dependency>
18    </dependencies>
19 
20</project>

01package org.caiyuan.maven.plugins;
02 
03import org.apache.maven.plugin.AbstractMojo;
04import org.apache.maven.plugin.MojoExecutionException;
05import org.apache.maven.plugin.MojoFailureException;
06import org.caiyuan.maven.v.V;
07 
08import java.io.File;
09import java.net.URL;
10import java.util.Arrays;
11import java.util.List;
12import java.util.Map;
13import java.util.Properties;
14 
15/**
16* User: caiyuan
17* Date: 12-2-27 11:55
18*
19* @goal compile
20* @requiresProject false
21*/
22public class TestMojo extendsAbstractMojo {
23 
24    /**
25     * @parameter property="str"
26     */
27    privateString str;
28 
29    /**
30     * @parameter
31     */
32    privateString lable;
33 
34    /**
35     * @parameter
36     */
37    privatefloat number;
38 
39    /**
40     * @parameter
41     */
42    privateFile file;
43 
44    /**
45     * @parameter
46     */
47    privateURL url;
48 
49    /**
50     * @parameter
51     */
52    privateString[] array;
53 
54    /**
55     * @parameter
56     */
57    privateList<String> items;
58 
59    /**
60     * @parameter
61     */
62    privateMap<String, String> map;
63 
64    /**
65     * @parameter
66     */
67    privateProperties properties;
68 
69    /**
70     * @parameter
71     */
72    privateV v;
73 
74    @Override
75    publicvoid execute() throws MojoExecutionException, MojoFailureException {
76        getLog().info("String is : str = "+ str);
77        getLog().info("String is : lable = "+ lable);
78        getLog().info("String is : number = "+ number);
79        getLog().info("String is : file = "+ file);
80        getLog().info("String is : url = "+ url);
81        getLog().info("String is : array = "+ Arrays.toString(array));
82        getLog().info("String is : items = "+ items);
83        getLog().info("String is : map = "+ map);
84        getLog().info("String is : properties = "+ properties);
85        getLog().info("String is : v = "+ v);
86    }
87 
88}

01package org.caiyuan.maven.v;
02 
03/**
04* User: caiyuan
05* Date: 12-2-27 17:34
06*/
07public class V {
08 
09    privateString firstName;
10    privateString lastName;
11 
12    publicString getFirstName() {
13        returnfirstName;
14    }
15 
16    publicvoid setFirstName(String firstName) {
17        this.firstName = firstName;
18    }
19 
20    publicString getLastName() {
21        returnlastName;
22    }
23 
24    publicvoid setLastName(String lastName) {
25        this.lastName = lastName;
26    }
27 
28    @Override
29    publicString toString() {
30        return"V{ firstName='" + firstName +"', lastName='" + lastName + "'}";
31    }
32}

测试插件

01<?xml version="1.0"encoding="UTF-8"?>
02<project xmlns="http://maven.apache.org/POM/4.0.0"
03         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
04         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
05    <modelVersion>4.0.0</modelVersion>
06 
07    <groupId>org.caiyuan.maven</groupId>
08    <artifactId>mvn-test</artifactId>
09    <version>1.0</version>
10 
11    <build>
12        <plugins>
13            <plugin>
14                <groupId>org.caiyuan.maven</groupId>
15                <artifactId>test-maven-plugin</artifactId>
16                <version>1.0</version>
17                <configuration>
18                    <str>test</str>
19                    <lable>123</lable>
20                    <number>3.15</number>
21                    <file>D:\\</file>
22                    <url>http://www.google.com</url>
23                    <array>
24                        <item>a</item>
25                        <item>b</item>
26                        <item>c</item>
27                    </array>
28                    <items>
29                        <item>a</item>
30                        <item>b</item>
31                        <item>c</item>
32                    </items>
33                    <map>
34                        <k1>v1</k1>
35                        <k2>v2</k2>
36                        <k3>v3</k3>
37                    </map>
38                    <properties>
39                        <property>
40                            <name>name1</name>
41                            <value>value1</value>
42                        </property>
43                        <property>
44                            <name>name2</name>
45                            <value>value2</value>
46                        </property>
47                        <property>
48                            <name>name3</name>
49                            <value>value3</value>
50                        </property>
51                    </properties>
52                    <v>
53                        <firstName>Jason</firstName>
54                        <lastName>van Zyl</lastName>
55                    </v>
56                </configuration>
57            </plugin>
58        </plugins>
59    </build>
60 
61</project

 另一篇文章

1.  自定义插件介绍

在一些情况下,Maven现有的一些插件可能满足不了我们的一些“特殊服务”。这个时候有三种选择,第一,问问有经验的Maven开发者,是否有相应的。第二,从网上找找搜搜,看看有没有别的组织开发的Maven插件可以使用。第三,以上两条路都没走通,看来只能自己开发Maven的插件了,谁让咱们的需求比较“特殊”呢!“特殊服务”就得付出点代价不是~对于插件,大家可能接触最深的就是IDE插件了,像Eclipse这种IDE就开创了插件时代,后来的NetBeans、IntelliJ都是内核比较少,插件加在一起显得比较重量级。

2.  编写简单Maven插件

编写一个Maven插件其实不难,关键是编写优秀的Maven插件确实不容易。相对于Eclipse Plugin开发来说,Maven插件开发显得简单又清晰。只需要注意两点就可以开发出一个简单的插件,第一,编写插件pom.xml文件;第二,编写真正做事情的插件Java类。

咱先看pom.xml文件内容啊

Java代码 复制代码  收藏代码
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">   
  3.     <modelVersion>4.0.0</modelVersion>   
  4.   
  5.     <groupId>com.liuyan.account</groupId>   
  6.     <artifactId>MavenAccount-Plugin</artifactId>   
  7.     <version>1.0</version>   
  8.     <packaging>maven-plugin</packaging>   
  9.     <name>MavenAccount-Plugin</name>   
  10.     <url>http://maven.apache.org</url>   
  11.   
  12.     <properties>   
  13.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>   
  14.     </properties>   
  15.   
  16.     <dependencies>   
  17.         <dependency>   
  18.             <groupId>junit</groupId>   
  19.             <artifactId>junit</artifactId>   
  20.             <version>3.8.1</version>   
  21.             <scope>test</scope>   
  22.         </dependency>   
  23.         <dependency>   
  24.             <groupId>org.apache.maven</groupId>   
  25.             <artifactId>maven-plugin-api</artifactId>   
  26.             <version>3.0.3</version>   
  27.         </dependency>   
  28.     </dependencies>   
  29. </project>  
<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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.liuyan.account</groupId>
	<artifactId>MavenAccount-Plugin</artifactId>
	<version>1.0</version>
	<packaging>maven-plugin</packaging>
	<name>MavenAccount-Plugin</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.maven</groupId>
			<artifactId>maven-plugin-api</artifactId>
			<version>3.0.3</version>
		</dependency>
	</dependencies>
</project>
  这个pom.xml文件主要注意两点,一个就是<packaging>的类型,应该是maven-plugin

,还有一个就是Maven插件项目必须依赖一个插件maven-plugin-api。

之后咱们再来看真正为客户端提供“特殊”Maven“服务”的类插件类必须继承自类org.apache.maven.plugin.AbstractMojo。这里面的一些注解比较有意思,大家别当它是普通的注释信息,Maven编译的时候不会忽略这些注释信息的,这些注释信息暗含了该插件的执行信息。

Java代码 复制代码  收藏代码
  1. package com.liuyan.account.MavenAccount_Plugin;   
  2.   
  3. import org.apache.maven.plugin.AbstractMojo;   
  4. import org.apache.maven.plugin.MojoExecutionException;   
  5. import org.apache.maven.plugin.MojoFailureException;   
  6.   
  7. /**  
  8.  * @goal info  
  9.  * @phase compile  
  10.  * @requiresProject false  
  11.  */  
  12. public class HelloWorldMojo extends AbstractMojo {   
  13.   
  14.     /**  
  15.      * @parameter expression="${name}"  
  16.      * @required  
  17.      */  
  18.     String name;   
  19.   
  20.     /**  
  21.      * @parameter expression="${age}"  
  22.      * @required  
  23.      */  
  24.     int age;   
  25.   
  26.     /**  
  27.      * @parameter expression="${isOk}"  
  28.      * @required  
  29.      */  
  30.     boolean isOk;   
  31.   
  32.     public void execute() throws MojoExecutionException, MojoFailureException {   
  33.         getLog().info(this.toString());   
  34.     }   
  35.   
  36.     public String toString() {   
  37.         return "String is : \"" + name + "\"" + "int is : \"" + age + "\""  
  38.                 + "boolean is : \"" + isOk + "\"";   
  39.     }   
  40.   
  41. }  
package com.liuyan.account.MavenAccount_Plugin;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;

/**
 * @goal info
 * @phase compile
 * @requiresProject false
 */
public class HelloWorldMojo extends AbstractMojo {

	/**
	 * @parameter expression="${name}"
	 * @required
	 */
	String name;

	/**
	 * @parameter expression="${age}"
	 * @required
	 */
	int age;

	/**
	 * @parameter expression="${isOk}"
	 * @required
	 */
	boolean isOk;

	public void execute() throws MojoExecutionException, MojoFailureException {
		getLog().info(this.toString());
	}

	public String toString() {
		return "String is : \"" + name + "\"" + "int is : \"" + age + "\""
				+ "boolean is : \"" + isOk + "\"";
	}

}

 

@goal info:表示该插件的服务目标

@phase compile:表示该插件的生效周期阶段

@requiresProject false:表示是否依托于一个项目才能运行该插件

@parameter expression="${name}":表示插件参数,使用插件的时候会用得到

@required:代表该参数不能省略

3.  使用自定义插件

使用插件之前先进行安装install。

之后在命令行输入如下命令

Java代码 复制代码  收藏代码
  1. com.liuyan.account:MavenAccount-Plugin:info -Dname=liuyan -Dage=25 -DisOk=true  
com.liuyan.account:MavenAccount-Plugin:info -Dname=liuyan -Dage=25 -DisOk=true

 输出如下效果

Java代码 复制代码  收藏代码
  1. [INFO] --- MavenAccount-Plugin:1.0:info (default-cli) @ MavenAccount-Plugin ---   
  2. [INFO] String is : "liuyan"int is : "25"boolean is : "true"  
  3. [INFO] ------------------------------------------------------------------------   
  4. [INFO] BUILD SUCCESS   
  5. [INFO] ------------------------------------------------------------------------   
  6. [INFO] Total time: 0.609s   
  7. [INFO] Finished at: Thu Jun 23 23:17:45 CST 2011  
  8. [INFO] Final Memory: 2M/15M   
  9. [INFO] ------------------------------------------------------------------------  
[INFO] --- MavenAccount-Plugin:1.0:info (default-cli) @ MavenAccount-Plugin ---
[INFO] String is : "liuyan"int is : "25"boolean is : "true"
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.609s
[INFO] Finished at: Thu Jun 23 23:17:45 CST 2011
[INFO] Final Memory: 2M/15M
[INFO] ------------------------------------------------------------------------

 插件输出了相关信息。您可以为您的服务提供更特殊的定制化“服务”。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值