maven插件开发(二)

接着上一篇文章:  

maven插件开发(一)


这里主要讲解如何配置各种类型的变量值

插件代码为:

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  即可看到配置的变量都已经输出来了


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值