maven 3 插件开发

编写插件

<?xml version="1.0" encoding="UTF-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>org.caiyuan.maven</groupId>
    <artifactId>test-maven-plugin</artifactId>
    <version>1.0</version>
    <packaging>maven-plugin</packaging>

    <dependencies>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-plugin-api</artifactId>
            <version>3.0.3</version>
        </dependency>
    </dependencies>

</project>

package org.caiyuan.maven.plugins;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.caiyuan.maven.v.V;

import java.io.File;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * User: caiyuan
 * Date: 12-2-27 11:55
 *
 * @goal compile
 * @requiresProject false
 */
public class TestMojo extends AbstractMojo {

    /**
     * @parameter property="str"
     */
    private String str;

    /**
     * @parameter
     */
    private String lable;

    /**
     * @parameter
     */
    private float number;

    /**
     * @parameter
     */
    private File file;

    /**
     * @parameter
     */
    private URL url;

    /**
     * @parameter
     */
    private String[] array;

    /**
     * @parameter
     */
    private List<String> items;

    /**
     * @parameter
     */
    private Map<String, String> map;

    /**
     * @parameter
     */
    private Properties properties;

    /**
     * @parameter
     */
    private V v;

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        getLog().info("String is : str = " + str);
        getLog().info("String is : lable = " + lable);
        getLog().info("String is : number = " + number);
        getLog().info("String is : file = " + file);
        getLog().info("String is : url = " + url);
        getLog().info("String is : array = " + Arrays.toString(array));
        getLog().info("String is : items = " + items);
        getLog().info("String is : map = " + map);
        getLog().info("String is : properties = " + properties);
        getLog().info("String is : v = " + v);
    }

}

package org.caiyuan.maven.v;

/**
 * User: caiyuan
 * Date: 12-2-27 17:34
 */
public class V {

    private String firstName;
    private String lastName;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        return "V{ firstName='" + firstName + "', lastName='" + lastName + "'}";
    }
}

测试插件

<?xml version="1.0" encoding="UTF-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>org.caiyuan.maven</groupId>
    <artifactId>mvn-test</artifactId>
    <version>1.0</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.caiyuan.maven</groupId>
                <artifactId>test-maven-plugin</artifactId>
                <version>1.0</version>
                <configuration>
                    <str>test</str>
                    <lable>123</lable>
                    <number>3.15</number>
                    <file>D:\\</file>
                    <url>http://www.google.com</url>
                    <array>
                        <item>a</item>
                        <item>b</item>
                        <item>c</item>
                    </array>
                    <items>
                        <item>a</item>
                        <item>b</item>
                        <item>c</item>
                    </items>
                    <map>
                        <k1>v1</k1>
                        <k2>v2</k2>
                        <k3>v3</k3>
                    </map>
                    <properties>
                        <property>
                            <name>name1</name>
                            <value>value1</value>
                        </property>
                        <property>
                            <name>name2</name>
                            <value>value2</value>
                        </property>
                        <property>
                            <name>name3</name>
                            <value>value3</value>
                        </property>
                    </properties>
                    <v>
                        <firstName>Jason</firstName>
                        <lastName>van Zyl</lastName>
                    </v>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

转载于:https://my.oschina.net/caiyuan/blog/41850

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值