编写插件
<?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>