欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ant 使用命名空间自定义task

程序员文章站 2022-04-06 11:13:19
...
一:

首先,写三个ant 任务 (每个任务都必须继承org.apache.tools.ant.Task)

 

1:
package cn.cloud.ant;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
public class DetailTask extends Task
{
	public void execute() throws BuildException
	{
		System.out.println("detail can be execute");
	}
}
 
2:
package cn.cloud.ant;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
public class ListTask extends Task 
{
	public void execute() throws BuildException
	{
		System.out.println("this is list task");
	}
}
 
3:
package cn.cloud.ant;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
public class ShowTask extends Task
{
	public void execute() throws BuildException
	{
		System.out.println("show task");
	}
}
 
三:编写antlib.xml文件(该文件需要放在src跟目录下)
<?xml version="1.0"?>

<antlib>
   <taskdef name="mylist" classname="cn.cloud.ant.ListTask"/>
   <taskdef name="myshow" classname="cn.cloud.ant.ShowTask"/>
   <taskdef name="mydetail" classname="cn.cloud.ant.DetailTask"/>
</antlib>
 

 四:编写ant脚本build.xnl
<?xml version="1.0" encoding="UTF-8"?>

<project name="ext_task" basedir="." xmlns:my="xxx" default="message-echo">
	
	<property name="base.dir" location="target"></property>
	<property name="src.dir" location="src"></property>
	<property name="class.dir" location="${base.dir}/classes"></property>
	
	<target name="Introduction">
		<echo>this is my first ant task</echo>
		<echo>${basedir}</echo>
		<echo>this is my first ant task</echo>
	</target>
	
	<target name="clean" depends="Introduction">
		<delete dir="${base.dir}"></delete>
	</target>
	
	<target name="init" depends="clean">
		<mkdir dir="${base.dir}"></mkdir>
		<mkdir dir="${class.dir}"></mkdir>
	</target>
	
	<target name="compile" depends="init">
		<javac includeantruntime="true" destdir="${class.dir}" srcdir="${src.dir}" target="1.7"></javac>
		<copy todir="${class.dir}">
			<fileset dir="${src.dir}">
				<exclude name="**/*.java"/>
			</fileset>
		</copy>
	</target>
	
	<target name="init-task-def" depends="compile">
		<echo>define .................</echo>
		<taskdef resource="antlib.xml" uri="xxx">
			<classpath path="${class.dir}"></classpath>
		</taskdef>
	</target>

	<target name="message-echo" depends="init-task-def">
		<my:mylist />
		<my:myshow />
		<my:mydetail />
	</target>
	
</project>