博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Ant的实现原理
阅读量:6249 次
发布时间:2019-06-22

本文共 15752 字,大约阅读时间需要 52 分钟。

在build.xml中,我们使用一句简单的脚本就可以执行想要做的事情,比如说:

上面这句脚本将删除 d://testMKDir

很神奇,有木有?

想知道怎么实现的吗?

继续往下看....

 

将ant.jar反编译一下,我看看到如下东东:

 Ant结构

原来ant将这些操作都封装了起来。这些操作都有一个共通的基类叫Task(抽象类)。

public void execute() throws BuildException{}

其中有个空实现的execute方法,为什么提这个,后面你就知道了。

 

下面来测试一下:

/*     * 
*
* delete=org.apache.tools.ant.taskdefs.Delete * mkdir=org.apache.tools.ant.taskdefs.Mkdir */public static void execute(String obj, String path) { try { Method setMethod = null; Class classObj = Class.forName(obj); Object objectCopy = classObj.getConstructor(new Class[] {}) .newInstance(new Object[] {}); setMethod = classObj.getMethod("setDir", new Class[] { File.class }); setMethod.invoke(objectCopy, new File(path)); Task task = (Task) objectCopy; task.execute(); } catch (Exception e) { System.out.println(e); } }

调用上面的方法:

execute("org.apache.tools.ant.taskdefs.Mkdir", "d://testMKDir");execute("org.apache.tools.ant.taskdefs.Delete", "d://testMKDir");

执行之后,控制台输出:

Created dir: d:\testMKDirDeleting directory d:\testMKDir

 

由上可知,build.xml中的执行过程大致为:

以解析<mkdir dir = "d://testMKDir" />为例:

  • 解析到了mkdir,就根据属性文件defaults.properties查找到: mkdir=org.apache.tools.ant.taskdefs.Mkdir
  • 通过反射创建org.apache.tools.ant.taskdefs.Mkdir对象
  • 获取其属性比如File dir;
  • 根据属性设置方法setDir(File dir);
  • 最后用其父类调用execute()方法

defaults.properties文件在org.apache.tools.ant.types包下,内容如下:

defaults.properties
1 # Licensed to the Apache Software Foundation (ASF) under one or more  2 # contributor license agreements.  See the NOTICE file distributed with  3 # this work for additional information regarding copyright ownership.  4 # The ASF licenses this file to You under the Apache License, Version 2.0  5 # (the "License"); you may not use this file except in compliance with  6 # the License.  You may obtain a copy of the License at  7 #  8 #     http://www.apache.org/licenses/LICENSE-2.0  9 # 10 # Unless required by applicable law or agreed to in writing, software 11 # distributed under the License is distributed on an "AS IS" BASIS, 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 # See the License for the specific language governing permissions and 14 # limitations under the License. 15 # 16 # standard ant tasks 17 ant=org.apache.tools.ant.taskdefs.Ant 18 antcall=org.apache.tools.ant.taskdefs.CallTarget 19 antstructure=org.apache.tools.ant.taskdefs.AntStructure 20 antversion=org.apache.tools.ant.taskdefs.condition.AntVersion 21 apply=org.apache.tools.ant.taskdefs.Transform 22 available=org.apache.tools.ant.taskdefs.Available 23 basename=org.apache.tools.ant.taskdefs.Basename 24 buildnumber=org.apache.tools.ant.taskdefs.BuildNumber 25 bunzip2=org.apache.tools.ant.taskdefs.BUnzip2 26 bzip2=org.apache.tools.ant.taskdefs.BZip2 27 checksum=org.apache.tools.ant.taskdefs.Checksum 28 chmod=org.apache.tools.ant.taskdefs.Chmod 29 classloader=org.apache.tools.ant.taskdefs.Classloader 30 concat=org.apache.tools.ant.taskdefs.Concat 31 condition=org.apache.tools.ant.taskdefs.ConditionTask 32 copy=org.apache.tools.ant.taskdefs.Copy 33 cvs=org.apache.tools.ant.taskdefs.Cvs 34 cvschangelog=org.apache.tools.ant.taskdefs.cvslib.ChangeLogTask 35 cvspass=org.apache.tools.ant.taskdefs.CVSPass 36 cvstagdiff=org.apache.tools.ant.taskdefs.cvslib.CvsTagDiff 37 cvsversion=org.apache.tools.ant.taskdefs.cvslib.CvsVersion 38 defaultexcludes=org.apache.tools.ant.taskdefs.DefaultExcludes 39 delete=org.apache.tools.ant.taskdefs.Delete 40 dependset=org.apache.tools.ant.taskdefs.DependSet 41 diagnostics=org.apache.tools.ant.taskdefs.DiagnosticsTask 42 dirname=org.apache.tools.ant.taskdefs.Dirname 43 ear=org.apache.tools.ant.taskdefs.Ear 44 echo=org.apache.tools.ant.taskdefs.Echo 45 exec=org.apache.tools.ant.taskdefs.ExecTask 46 fail=org.apache.tools.ant.taskdefs.Exit 47 filter=org.apache.tools.ant.taskdefs.Filter 48 fixcrlf=org.apache.tools.ant.taskdefs.FixCRLF 49 genkey=org.apache.tools.ant.taskdefs.GenerateKey 50 get=org.apache.tools.ant.taskdefs.Get 51 gunzip=org.apache.tools.ant.taskdefs.GUnzip 52 gzip=org.apache.tools.ant.taskdefs.GZip 53 import=org.apache.tools.ant.taskdefs.ImportTask 54 input=org.apache.tools.ant.taskdefs.Input 55 jar=org.apache.tools.ant.taskdefs.Jar 56 java=org.apache.tools.ant.taskdefs.Java 57 javac=org.apache.tools.ant.taskdefs.Javac 58 javadoc=org.apache.tools.ant.taskdefs.Javadoc 59 length=org.apache.tools.ant.taskdefs.Length 60 loadfile=org.apache.tools.ant.taskdefs.LoadFile 61 loadproperties=org.apache.tools.ant.taskdefs.LoadProperties 62 loadresource=org.apache.tools.ant.taskdefs.LoadResource 63 macrodef=org.apache.tools.ant.taskdefs.MacroDef 64 mail=org.apache.tools.ant.taskdefs.email.EmailTask 65 manifest=org.apache.tools.ant.taskdefs.ManifestTask 66 manifestclasspath=org.apache.tools.ant.taskdefs.ManifestClassPath 67 mkdir=org.apache.tools.ant.taskdefs.Mkdir 68 move=org.apache.tools.ant.taskdefs.Move 69 nice=org.apache.tools.ant.taskdefs.Nice 70 parallel=org.apache.tools.ant.taskdefs.Parallel 71 patch=org.apache.tools.ant.taskdefs.Patch 72 pathconvert=org.apache.tools.ant.taskdefs.PathConvert 73 presetdef=org.apache.tools.ant.taskdefs.PreSetDef 74 property=org.apache.tools.ant.taskdefs.Property 75 record=org.apache.tools.ant.taskdefs.Recorder 76 replace=org.apache.tools.ant.taskdefs.Replace 77 retry=org.apache.tools.ant.taskdefs.Retry 78 rmic=org.apache.tools.ant.taskdefs.Rmic 79 sequential=org.apache.tools.ant.taskdefs.Sequential 80 signjar=org.apache.tools.ant.taskdefs.SignJar 81 sleep=org.apache.tools.ant.taskdefs.Sleep 82 sql=org.apache.tools.ant.taskdefs.SQLExec 83 subant=org.apache.tools.ant.taskdefs.SubAnt 84 sync=org.apache.tools.ant.taskdefs.Sync 85 tar=org.apache.tools.ant.taskdefs.Tar 86 taskdef=org.apache.tools.ant.taskdefs.Taskdef 87 tempfile=org.apache.tools.ant.taskdefs.TempFile 88 touch=org.apache.tools.ant.taskdefs.Touch 89 tstamp=org.apache.tools.ant.taskdefs.Tstamp 90 truncate=org.apache.tools.ant.taskdefs.Truncate 91 typedef=org.apache.tools.ant.taskdefs.Typedef 92 unjar=org.apache.tools.ant.taskdefs.Expand 93 untar=org.apache.tools.ant.taskdefs.Untar 94 unwar=org.apache.tools.ant.taskdefs.Expand 95 unzip=org.apache.tools.ant.taskdefs.Expand 96 uptodate=org.apache.tools.ant.taskdefs.UpToDate 97 waitfor=org.apache.tools.ant.taskdefs.WaitFor 98 war=org.apache.tools.ant.taskdefs.War 99 whichresource=org.apache.tools.ant.taskdefs.WhichResource100 xmlproperty=org.apache.tools.ant.taskdefs.XmlProperty101 xslt=org.apache.tools.ant.taskdefs.XSLTProcess102 zip=org.apache.tools.ant.taskdefs.Zip103 104 # optional tasks105 antlr=org.apache.tools.ant.taskdefs.optional.ANTLR106 apt=org.apache.tools.ant.taskdefs.Apt107 attrib=org.apache.tools.ant.taskdefs.optional.windows.Attrib108 blgenclient=org.apache.tools.ant.taskdefs.optional.ejb.BorlandGenerateClient109 cab=org.apache.tools.ant.taskdefs.optional.Cab110 cccheckin=org.apache.tools.ant.taskdefs.optional.clearcase.CCCheckin111 cccheckout=org.apache.tools.ant.taskdefs.optional.clearcase.CCCheckout112 cclock=org.apache.tools.ant.taskdefs.optional.clearcase.CCLock113 ccmcheckin=org.apache.tools.ant.taskdefs.optional.ccm.CCMCheckin114 ccmcheckintask=org.apache.tools.ant.taskdefs.optional.ccm.CCMCheckinDefault115 ccmcheckout=org.apache.tools.ant.taskdefs.optional.ccm.CCMCheckout116 ccmcreatetask=org.apache.tools.ant.taskdefs.optional.ccm.CCMCreateTask117 ccmkattr=org.apache.tools.ant.taskdefs.optional.clearcase.CCMkattr118 ccmkbl=org.apache.tools.ant.taskdefs.optional.clearcase.CCMkbl119 ccmkdir=org.apache.tools.ant.taskdefs.optional.clearcase.CCMkdir120 ccmkelem=org.apache.tools.ant.taskdefs.optional.clearcase.CCMkelem121 ccmklabel=org.apache.tools.ant.taskdefs.optional.clearcase.CCMklabel122 ccmklbtype=org.apache.tools.ant.taskdefs.optional.clearcase.CCMklbtype123 ccmreconfigure=org.apache.tools.ant.taskdefs.optional.ccm.CCMReconfigure124 ccrmtype=org.apache.tools.ant.taskdefs.optional.clearcase.CCRmtype125 ccuncheckout=org.apache.tools.ant.taskdefs.optional.clearcase.CCUnCheckout126 ccunlock=org.apache.tools.ant.taskdefs.optional.clearcase.CCUnlock127 ccupdate=org.apache.tools.ant.taskdefs.optional.clearcase.CCUpdate128 chgrp=org.apache.tools.ant.taskdefs.optional.unix.Chgrp129 chown=org.apache.tools.ant.taskdefs.optional.unix.Chown130 csc=org.apache.tools.ant.taskdefs.optional.dotnet.CSharp131 ddcreator=org.apache.tools.ant.taskdefs.optional.ejb.DDCreator132 depend=org.apache.tools.ant.taskdefs.optional.depend.Depend133 echoproperties=org.apache.tools.ant.taskdefs.optional.EchoProperties134 echoxml=org.apache.tools.ant.taskdefs.EchoXML135 ejbc=org.apache.tools.ant.taskdefs.optional.ejb.Ejbc136 ejbjar=org.apache.tools.ant.taskdefs.optional.ejb.EjbJar137 ftp=org.apache.tools.ant.taskdefs.optional.net.FTP138 ilasm=org.apache.tools.ant.taskdefs.optional.dotnet.Ilasm139 ildasm=org.apache.tools.ant.taskdefs.optional.dotnet.Ildasm140 image=org.apache.tools.ant.taskdefs.optional.image.Image141 importtypelib=org.apache.tools.ant.taskdefs.optional.dotnet.ImportTypelib142 iplanet-ejbc=org.apache.tools.ant.taskdefs.optional.ejb.IPlanetEjbcTask143 jarlib-available=org.apache.tools.ant.taskdefs.optional.extension.JarLibAvailableTask144 jarlib-display=org.apache.tools.ant.taskdefs.optional.extension.JarLibDisplayTask145 jarlib-manifest=org.apache.tools.ant.taskdefs.optional.extension.JarLibManifestTask146 jarlib-resolve=org.apache.tools.ant.taskdefs.optional.extension.JarLibResolveTask147 javacc=org.apache.tools.ant.taskdefs.optional.javacc.JavaCC148 javah=org.apache.tools.ant.taskdefs.optional.Javah149 jdepend=org.apache.tools.ant.taskdefs.optional.jdepend.JDependTask150 jjdoc=org.apache.tools.ant.taskdefs.optional.javacc.JJDoc151 jjtree=org.apache.tools.ant.taskdefs.optional.javacc.JJTree152 jsharpc=org.apache.tools.ant.taskdefs.optional.dotnet.JSharp153 junit=org.apache.tools.ant.taskdefs.optional.junit.JUnitTask154 junitreport=org.apache.tools.ant.taskdefs.optional.junit.XMLResultAggregator155 makeurl=org.apache.tools.ant.taskdefs.MakeUrl156 native2ascii=org.apache.tools.ant.taskdefs.optional.Native2Ascii157 netrexxc=org.apache.tools.ant.taskdefs.optional.NetRexxC158 p4add=org.apache.tools.ant.taskdefs.optional.perforce.P4Add159 p4change=org.apache.tools.ant.taskdefs.optional.perforce.P4Change160 p4counter=org.apache.tools.ant.taskdefs.optional.perforce.P4Counter161 p4delete=org.apache.tools.ant.taskdefs.optional.perforce.P4Delete162 p4edit=org.apache.tools.ant.taskdefs.optional.perforce.P4Edit163 p4fstat=org.apache.tools.ant.taskdefs.optional.perforce.P4Fstat164 p4have=org.apache.tools.ant.taskdefs.optional.perforce.P4Have165 p4integrate=org.apache.tools.ant.taskdefs.optional.perforce.P4Integrate166 p4label=org.apache.tools.ant.taskdefs.optional.perforce.P4Label167 p4labelsync=org.apache.tools.ant.taskdefs.optional.perforce.P4Labelsync168 p4reopen=org.apache.tools.ant.taskdefs.optional.perforce.P4Reopen169 p4resolve=org.apache.tools.ant.taskdefs.optional.perforce.P4Resolve170 p4revert=org.apache.tools.ant.taskdefs.optional.perforce.P4Revert171 p4submit=org.apache.tools.ant.taskdefs.optional.perforce.P4Submit172 p4sync=org.apache.tools.ant.taskdefs.optional.perforce.P4Sync173 propertyfile=org.apache.tools.ant.taskdefs.optional.PropertyFile174 pvcs=org.apache.tools.ant.taskdefs.optional.pvcs.Pvcs175 replaceregexp=org.apache.tools.ant.taskdefs.optional.ReplaceRegExp176 resourcecount=org.apache.tools.ant.taskdefs.ResourceCount177 rexec=org.apache.tools.ant.taskdefs.optional.net.RExecTask178 rpm=org.apache.tools.ant.taskdefs.optional.Rpm179 schemavalidate=org.apache.tools.ant.taskdefs.optional.SchemaValidate180 scp=org.apache.tools.ant.taskdefs.optional.ssh.Scp181 script=org.apache.tools.ant.taskdefs.optional.Script182 scriptdef=org.apache.tools.ant.taskdefs.optional.script.ScriptDef183 serverdeploy=org.apache.tools.ant.taskdefs.optional.j2ee.ServerDeploy184 setproxy=org.apache.tools.ant.taskdefs.optional.net.SetProxy185 soscheckin=org.apache.tools.ant.taskdefs.optional.sos.SOSCheckin186 soscheckout=org.apache.tools.ant.taskdefs.optional.sos.SOSCheckout187 sosget=org.apache.tools.ant.taskdefs.optional.sos.SOSGet188 soslabel=org.apache.tools.ant.taskdefs.optional.sos.SOSLabel189 sound=org.apache.tools.ant.taskdefs.optional.sound.SoundTask190 splash=org.apache.tools.ant.taskdefs.optional.splash.SplashTask191 sshexec=org.apache.tools.ant.taskdefs.optional.ssh.SSHExec192 stcheckin=org.apache.tools.ant.taskdefs.optional.starteam.StarTeamCheckin193 stcheckout=org.apache.tools.ant.taskdefs.optional.starteam.StarTeamCheckout194 stlabel=org.apache.tools.ant.taskdefs.optional.starteam.StarTeamLabel195 stlist=org.apache.tools.ant.taskdefs.optional.starteam.StarTeamList196 stylebook=org.apache.tools.ant.taskdefs.optional.StyleBook197 symlink=org.apache.tools.ant.taskdefs.optional.unix.Symlink198 telnet=org.apache.tools.ant.taskdefs.optional.net.TelnetTask199 translate=org.apache.tools.ant.taskdefs.optional.i18n.Translate200 vbc=org.apache.tools.ant.taskdefs.optional.dotnet.VisualBasicCompile201 verifyjar=org.apache.tools.ant.taskdefs.VerifyJar202 vssadd=org.apache.tools.ant.taskdefs.optional.vss.MSVSSADD203 vsscheckin=org.apache.tools.ant.taskdefs.optional.vss.MSVSSCHECKIN204 vsscheckout=org.apache.tools.ant.taskdefs.optional.vss.MSVSSCHECKOUT205 vsscp=org.apache.tools.ant.taskdefs.optional.vss.MSVSSCP206 vsscreate=org.apache.tools.ant.taskdefs.optional.vss.MSVSSCREATE207 vssget=org.apache.tools.ant.taskdefs.optional.vss.MSVSSGET208 vsshistory=org.apache.tools.ant.taskdefs.optional.vss.MSVSSHISTORY209 vsslabel=org.apache.tools.ant.taskdefs.optional.vss.MSVSSLABEL210 wljspc=org.apache.tools.ant.taskdefs.optional.jsp.WLJspc211 wlrun=org.apache.tools.ant.taskdefs.optional.ejb.WLRun212 wlstop=org.apache.tools.ant.taskdefs.optional.ejb.WLStop213 wsdltodotnet=org.apache.tools.ant.taskdefs.optional.dotnet.WsdlToDotnet214 xmlvalidate=org.apache.tools.ant.taskdefs.optional.XMLValidateTask215 216 217 # deprecated ant tasks (kept for back compatibility)218 copydir=org.apache.tools.ant.taskdefs.Copydir219 copyfile=org.apache.tools.ant.taskdefs.Copyfile220 copypath=org.apache.tools.ant.taskdefs.CopyPath221 deltree=org.apache.tools.ant.taskdefs.Deltree222 execon=org.apache.tools.ant.taskdefs.ExecuteOn223 javadoc2=org.apache.tools.ant.taskdefs.Javadoc224 jlink=org.apache.tools.ant.taskdefs.optional.jlink.JlinkTask225 jspc=org.apache.tools.ant.taskdefs.optional.jsp.JspC226 mimemail=org.apache.tools.ant.taskdefs.optional.net.MimeMail227 rename=org.apache.tools.ant.taskdefs.Rename228 renameext=org.apache.tools.ant.taskdefs.optional.RenameExtensions229 starteam=org.apache.tools.ant.taskdefs.optional.scm.AntStarTeamCheckOut230 style=org.apache.tools.ant.taskdefs.XSLTProcess231 WsdlToDotnet=org.apache.tools.ant.taskdefs.optional.dotnet.WsdlToDotnet

类似的,测试一下copy方法如下:

public static void execute_Copy(String obj, String path,String destPath) {        try {            Method setMethod = null;            Method setDestMethod = null;            Class classObj = Class.forName(obj);            Object objectCopy = classObj.getConstructor(new Class[] {})            .newInstance(new Object[] {});                        setMethod = classObj.getMethod("setFile", new Class[] { File.class });            setMethod.invoke(objectCopy, new File(path));                        setDestMethod = classObj.getMethod("setTofile", new Class[] { File.class });            setDestMethod.invoke(objectCopy, new File(destPath));                                    Task task = (Task) objectCopy;            task.execute();        } catch (Exception e) {            System.out.println(e);        }    }

 

 

以上测试基于ant 1.7版,其他版本应该也差不多..

 

 

 

 

 

 

转载于:https://www.cnblogs.com/yejg1212/archive/2013/03/14/2959689.html

你可能感兴趣的文章
angularJS
查看>>
微软可穿戴设备新专利公布
查看>>
web应用安全的现状是怎样的
查看>>
QuikNode -Infura高配版
查看>>
JVM学习记录——类加载的过程
查看>>
iOS 仿看了吗应用、指南针测网速等常用工具、自定义弹出视图框架、图片裁剪、内容扩展等源码...
查看>>
分布式系统中ID的需求
查看>>
引入计算属性、action、动态内容
查看>>
linux shell 命令下批量添加文件的后缀 和批量删除 拥有某后缀的文件
查看>>
OSChina 周三乱弹 ——京中有善口技者
查看>>
MyEclipse2014 Update Progress弹窗频繁出现的问题解决
查看>>
异或运算
查看>>
python 爬虫抓取心得分享
查看>>
教你ABBYY FineReader 12添加图像的技巧
查看>>
Zabbix服务端部署
查看>>
mysql 查询(详细版)
查看>>
天使汇
查看>>
Spark Streaming和Kafka整合之路(最新版本)
查看>>
第四阶段 :安全与监控 Linux监控和安全运维
查看>>
fbterm keymap 键盘问题
查看>>