Ant的实现原理

本文深入探讨了Apache Ant的任务执行原理,展示了如何通过解析build.xml文件中的标签,利用反射机制创建并执行相应的任务类。此外,还提供了一个具体示例,演示了mkdir和delete任务的实现方式。
摘要由CSDN通过智能技术生成

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

<delete dir="d://testMKDir" />

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

很神奇,有木有?

想知道怎么实现的吗?

继续往下看....

 

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

 Ant结构

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

public void execute() throws BuildException{

}

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

 

下面来测试一下:

    /*
     * <delete dir="d://testMKDir" /> 
     * <mkdir dir="d://testMKDir" />
     * 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:\testMKDir
Deleting 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.WhichResource
100 xmlproperty=org.apache.tools.ant.taskdefs.XmlProperty
101 xslt=org.apache.tools.ant.taskdefs.XSLTProcess
102 zip=org.apache.tools.ant.taskdefs.Zip
103 
104 # optional tasks
105 antlr=org.apache.tools.ant.taskdefs.optional.ANTLR
106 apt=org.apache.tools.ant.taskdefs.Apt
107 attrib=org.apache.tools.ant.taskdefs.optional.windows.Attrib
108 blgenclient=org.apache.tools.ant.taskdefs.optional.ejb.BorlandGenerateClient
109 cab=org.apache.tools.ant.taskdefs.optional.Cab
110 cccheckin=org.apache.tools.ant.taskdefs.optional.clearcase.CCCheckin
111 cccheckout=org.apache.tools.ant.taskdefs.optional.clearcase.CCCheckout
112 cclock=org.apache.tools.ant.taskdefs.optional.clearcase.CCLock
113 ccmcheckin=org.apache.tools.ant.taskdefs.optional.ccm.CCMCheckin
114 ccmcheckintask=org.apache.tools.ant.taskdefs.optional.ccm.CCMCheckinDefault
115 ccmcheckout=org.apache.tools.ant.taskdefs.optional.ccm.CCMCheckout
116 ccmcreatetask=org.apache.tools.ant.taskdefs.optional.ccm.CCMCreateTask
117 ccmkattr=org.apache.tools.ant.taskdefs.optional.clearcase.CCMkattr
118 ccmkbl=org.apache.tools.ant.taskdefs.optional.clearcase.CCMkbl
119 ccmkdir=org.apache.tools.ant.taskdefs.optional.clearcase.CCMkdir
120 ccmkelem=org.apache.tools.ant.taskdefs.optional.clearcase.CCMkelem
121 ccmklabel=org.apache.tools.ant.taskdefs.optional.clearcase.CCMklabel
122 ccmklbtype=org.apache.tools.ant.taskdefs.optional.clearcase.CCMklbtype
123 ccmreconfigure=org.apache.tools.ant.taskdefs.optional.ccm.CCMReconfigure
124 ccrmtype=org.apache.tools.ant.taskdefs.optional.clearcase.CCRmtype
125 ccuncheckout=org.apache.tools.ant.taskdefs.optional.clearcase.CCUnCheckout
126 ccunlock=org.apache.tools.ant.taskdefs.optional.clearcase.CCUnlock
127 ccupdate=org.apache.tools.ant.taskdefs.optional.clearcase.CCUpdate
128 chgrp=org.apache.tools.ant.taskdefs.optional.unix.Chgrp
129 chown=org.apache.tools.ant.taskdefs.optional.unix.Chown
130 csc=org.apache.tools.ant.taskdefs.optional.dotnet.CSharp
131 ddcreator=org.apache.tools.ant.taskdefs.optional.ejb.DDCreator
132 depend=org.apache.tools.ant.taskdefs.optional.depend.Depend
133 echoproperties=org.apache.tools.ant.taskdefs.optional.EchoProperties
134 echoxml=org.apache.tools.ant.taskdefs.EchoXML
135 ejbc=org.apache.tools.ant.taskdefs.optional.ejb.Ejbc
136 ejbjar=org.apache.tools.ant.taskdefs.optional.ejb.EjbJar
137 ftp=org.apache.tools.ant.taskdefs.optional.net.FTP
138 ilasm=org.apache.tools.ant.taskdefs.optional.dotnet.Ilasm
139 ildasm=org.apache.tools.ant.taskdefs.optional.dotnet.Ildasm
140 image=org.apache.tools.ant.taskdefs.optional.image.Image
141 importtypelib=org.apache.tools.ant.taskdefs.optional.dotnet.ImportTypelib
142 iplanet-ejbc=org.apache.tools.ant.taskdefs.optional.ejb.IPlanetEjbcTask
143 jarlib-available=org.apache.tools.ant.taskdefs.optional.extension.JarLibAvailableTask
144 jarlib-display=org.apache.tools.ant.taskdefs.optional.extension.JarLibDisplayTask
145 jarlib-manifest=org.apache.tools.ant.taskdefs.optional.extension.JarLibManifestTask
146 jarlib-resolve=org.apache.tools.ant.taskdefs.optional.extension.JarLibResolveTask
147 javacc=org.apache.tools.ant.taskdefs.optional.javacc.JavaCC
148 javah=org.apache.tools.ant.taskdefs.optional.Javah
149 jdepend=org.apache.tools.ant.taskdefs.optional.jdepend.JDependTask
150 jjdoc=org.apache.tools.ant.taskdefs.optional.javacc.JJDoc
151 jjtree=org.apache.tools.ant.taskdefs.optional.javacc.JJTree
152 jsharpc=org.apache.tools.ant.taskdefs.optional.dotnet.JSharp
153 junit=org.apache.tools.ant.taskdefs.optional.junit.JUnitTask
154 junitreport=org.apache.tools.ant.taskdefs.optional.junit.XMLResultAggregator
155 makeurl=org.apache.tools.ant.taskdefs.MakeUrl
156 native2ascii=org.apache.tools.ant.taskdefs.optional.Native2Ascii
157 netrexxc=org.apache.tools.ant.taskdefs.optional.NetRexxC
158 p4add=org.apache.tools.ant.taskdefs.optional.perforce.P4Add
159 p4change=org.apache.tools.ant.taskdefs.optional.perforce.P4Change
160 p4counter=org.apache.tools.ant.taskdefs.optional.perforce.P4Counter
161 p4delete=org.apache.tools.ant.taskdefs.optional.perforce.P4Delete
162 p4edit=org.apache.tools.ant.taskdefs.optional.perforce.P4Edit
163 p4fstat=org.apache.tools.ant.taskdefs.optional.perforce.P4Fstat
164 p4have=org.apache.tools.ant.taskdefs.optional.perforce.P4Have
165 p4integrate=org.apache.tools.ant.taskdefs.optional.perforce.P4Integrate
166 p4label=org.apache.tools.ant.taskdefs.optional.perforce.P4Label
167 p4labelsync=org.apache.tools.ant.taskdefs.optional.perforce.P4Labelsync
168 p4reopen=org.apache.tools.ant.taskdefs.optional.perforce.P4Reopen
169 p4resolve=org.apache.tools.ant.taskdefs.optional.perforce.P4Resolve
170 p4revert=org.apache.tools.ant.taskdefs.optional.perforce.P4Revert
171 p4submit=org.apache.tools.ant.taskdefs.optional.perforce.P4Submit
172 p4sync=org.apache.tools.ant.taskdefs.optional.perforce.P4Sync
173 propertyfile=org.apache.tools.ant.taskdefs.optional.PropertyFile
174 pvcs=org.apache.tools.ant.taskdefs.optional.pvcs.Pvcs
175 replaceregexp=org.apache.tools.ant.taskdefs.optional.ReplaceRegExp
176 resourcecount=org.apache.tools.ant.taskdefs.ResourceCount
177 rexec=org.apache.tools.ant.taskdefs.optional.net.RExecTask
178 rpm=org.apache.tools.ant.taskdefs.optional.Rpm
179 schemavalidate=org.apache.tools.ant.taskdefs.optional.SchemaValidate
180 scp=org.apache.tools.ant.taskdefs.optional.ssh.Scp
181 script=org.apache.tools.ant.taskdefs.optional.Script
182 scriptdef=org.apache.tools.ant.taskdefs.optional.script.ScriptDef
183 serverdeploy=org.apache.tools.ant.taskdefs.optional.j2ee.ServerDeploy
184 setproxy=org.apache.tools.ant.taskdefs.optional.net.SetProxy
185 soscheckin=org.apache.tools.ant.taskdefs.optional.sos.SOSCheckin
186 soscheckout=org.apache.tools.ant.taskdefs.optional.sos.SOSCheckout
187 sosget=org.apache.tools.ant.taskdefs.optional.sos.SOSGet
188 soslabel=org.apache.tools.ant.taskdefs.optional.sos.SOSLabel
189 sound=org.apache.tools.ant.taskdefs.optional.sound.SoundTask
190 splash=org.apache.tools.ant.taskdefs.optional.splash.SplashTask
191 sshexec=org.apache.tools.ant.taskdefs.optional.ssh.SSHExec
192 stcheckin=org.apache.tools.ant.taskdefs.optional.starteam.StarTeamCheckin
193 stcheckout=org.apache.tools.ant.taskdefs.optional.starteam.StarTeamCheckout
194 stlabel=org.apache.tools.ant.taskdefs.optional.starteam.StarTeamLabel
195 stlist=org.apache.tools.ant.taskdefs.optional.starteam.StarTeamList
196 stylebook=org.apache.tools.ant.taskdefs.optional.StyleBook
197 symlink=org.apache.tools.ant.taskdefs.optional.unix.Symlink
198 telnet=org.apache.tools.ant.taskdefs.optional.net.TelnetTask
199 translate=org.apache.tools.ant.taskdefs.optional.i18n.Translate
200 vbc=org.apache.tools.ant.taskdefs.optional.dotnet.VisualBasicCompile
201 verifyjar=org.apache.tools.ant.taskdefs.VerifyJar
202 vssadd=org.apache.tools.ant.taskdefs.optional.vss.MSVSSADD
203 vsscheckin=org.apache.tools.ant.taskdefs.optional.vss.MSVSSCHECKIN
204 vsscheckout=org.apache.tools.ant.taskdefs.optional.vss.MSVSSCHECKOUT
205 vsscp=org.apache.tools.ant.taskdefs.optional.vss.MSVSSCP
206 vsscreate=org.apache.tools.ant.taskdefs.optional.vss.MSVSSCREATE
207 vssget=org.apache.tools.ant.taskdefs.optional.vss.MSVSSGET
208 vsshistory=org.apache.tools.ant.taskdefs.optional.vss.MSVSSHISTORY
209 vsslabel=org.apache.tools.ant.taskdefs.optional.vss.MSVSSLABEL
210 wljspc=org.apache.tools.ant.taskdefs.optional.jsp.WLJspc
211 wlrun=org.apache.tools.ant.taskdefs.optional.ejb.WLRun
212 wlstop=org.apache.tools.ant.taskdefs.optional.ejb.WLStop
213 wsdltodotnet=org.apache.tools.ant.taskdefs.optional.dotnet.WsdlToDotnet
214 xmlvalidate=org.apache.tools.ant.taskdefs.optional.XMLValidateTask
215 
216 
217 # deprecated ant tasks (kept for back compatibility)
218 copydir=org.apache.tools.ant.taskdefs.Copydir
219 copyfile=org.apache.tools.ant.taskdefs.Copyfile
220 copypath=org.apache.tools.ant.taskdefs.CopyPath
221 deltree=org.apache.tools.ant.taskdefs.Deltree
222 execon=org.apache.tools.ant.taskdefs.ExecuteOn
223 javadoc2=org.apache.tools.ant.taskdefs.Javadoc
224 jlink=org.apache.tools.ant.taskdefs.optional.jlink.JlinkTask
225 jspc=org.apache.tools.ant.taskdefs.optional.jsp.JspC
226 mimemail=org.apache.tools.ant.taskdefs.optional.net.MimeMail
227 rename=org.apache.tools.ant.taskdefs.Rename
228 renameext=org.apache.tools.ant.taskdefs.optional.RenameExtensions
229 starteam=org.apache.tools.ant.taskdefs.optional.scm.AntStarTeamCheckOut
230 style=org.apache.tools.ant.taskdefs.XSLTProcess
231 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值