接着上一篇文章:
这里主要讲解如何配置各种类型的变量值
插件代码为:
package com.lala.maven.plugin;
import java.io.File;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
/**
* 这个例子主要演示了,如何配置各种类型的变量值
*/
@Mojo(name = "parameter")
public class TestMojo extends AbstractMojo
{
@Parameter(property = "greeting", defaultValue = "this is my test maven plugin")
private String greeting;
//引用变量
@Parameter(defaultValue = "${pom.groupId}")
private String groupId;
@Parameter
private File myFile;
@Parameter
private URL myURL;
@Parameter
private String[] myArray;
@Parameter
private List<String> myList;
@Parameter
private Map<String, String> myMap;
@Parameter
private Properties myProperties;
public void execute() throws MojoExecutionException, MojoFailureException
{
info(greeting);
info(groupId);
if(myFile != null)
{
info(myFile.exists());
info(myFile.getAbsolutePath());
}
if(myURL != null)
{
info(myURL.toString());
}
if(myArray != null)
{
for(String arr : myArray)
{
info(arr);
}
}
if(myList != null)
{
for(String arr : myList)
{
info(arr);
}
}
if(myMap != null)
{
for(String key : myMap.keySet())
{
info(key + "=" + myMap.get(key));
}
}
if(myProperties != null)
{
for(Object key : myProperties.keySet())
{
info(key.toString() + "=" + myProperties.getProperty(key.toString()));
}
}
}
private void info(Object str)
{
this.getLog().info(str.toString());
}
}
注意,上面的private变量,不一定需要get,set方法
<plugin>
<groupId>com.lala</groupId>
<artifactId>sys-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<greeting>**********************</greeting>
<myFile>D:\\code\\settings.xml</myFile>
<myURL>http://www.baidu.com</myURL>
<myArray>
<param>aaa</param>
<param>bbb</param>
<param>ccc</param>
</myArray>
<myList>
<param>111</param>
<param>222</param>
<param>333</param>
</myList>
<myMap>
<id>10086</id>
<name>CMCC</name>
<age>32</age>
</myMap>
<myProperties>
<property>
<name>className</name>
<value>com.mysql.jdbc.Driver</value>
</property>
<property>
<name>url</name>
<value>jdbc:mysql://127.0.0.1:3306:db_cloud</value>
</property>
</myProperties>
</configuration>
</plugin>
最后,执行:mvn sys:parameter 即可看到配置的变量都已经输出来了