You may already use theApache Ant的Java-based build tool to build projects with a presupplied build.xml file. But what if you want to do something more complicated? Knowing how Ant works under the hood will let you make the most of its capabilities. Let's dissect an Ant buildfile and examine its components.
蚂蚁比其他命令以不同的方式运作-line build tools. Instead of using shell commands to extend your tool, you extend Ant by writing Java classes to run particular tasks – although Ant comes with a huge list of available tasks. Instead of constructing a shell command to run a particular task configuration, you configure Ant in XML files. The XML creates a tree of tasks to be executed to build particular defined targets, and each task is run by its own object. The crucial implication of this is that Ant is entirely cross-platform, unlikemakeand its ilk.
I'll assume that you already have Ant installed for this tutorial; if not, get it from its website or your package manager.
基本蚂蚁建设
如果你只需要输入anton the command line in an empty directory, you'll get an error message telling you that there's no build.xml file. build.xml is the file Ant needs in order to know what it's supposed to do. Let's look at the structure of a basic buildfile:
<?XML版本= “1.0” 编码= “UTF-8”?> <项目名称= “HelloAnt” 默认= “编译” 的basedir = “ ”> <属性名=“ src.dir的” 值= “”/> <属性名= “build.dir” 值= “构建/”/> <目标名称= “干净”> <回波>清理$ {build.dir} 回波> <删除DIR =“$ {构建。DIR} “/> 目标> <目标名称=” 初始化”取决于= “干净”> <回波>创建构建目录$ {build.dir} 回波> 目标> Compiling source from ${src.dir} to ${build.dir}
该root element for an Ant buildfile isproject:此元素的文件形式剩下的部分。该project元件本身有一个名称,缺省目标,和基本目录,需要其中没有。构建文件然后定义源和编译目录的属性,它是指在文件中稍后。这意味着,如果你改变你的目录结构,你只需要更改文件在一个地方。干净(这将删除编译目录),初始化(这再现了build目录),并编译:实际工作是由三个目标进行。
每个目标包括多个任务。蚂蚁有huge list of built-in tasks available,and you can define more of your own by writing a Java file, though here we're just using built-in ones.回声,delete,MKDIR,andjavac的所有做你所期望的东西;你可以检查出文档的详细信息。注意,javac的如果目标目录不存在任务将失败,因此需要在创建它在里面target.
您还会注意到要看attribute on two of the tasks. This sets up a chain of tasks; if you tell Ant to execute Task 1 which is dependent on Task 2, Ant will run Task 2 for you first. You can also chain multiple dependencies, as here, wherecompile依赖于取决于在里面这取决于clean。该y'll all be executed in the correct order. Tasks can also have multiple dependencies, and Ant will sort out everything for you.
为了测试这个构建文件,创建一个基本HelloWorld.javafile in your base directory, then typeant。Since our default target iscompile,it should clean, init, and compile your program.
Properties
Instead of having all the properties at the start of build.xml, you can instead keep them in a properties file. This is usually called build.properties, and ours would look like this:
src.dir=. build.dir=build test.dir=${build.dir}/test
正如你所看到的,你可以参考在文件中较早的性质。那么您在此的build.xml build.properties文件:
<项目名称= “HelloAndroid” 默认为 “帮助”> <属性文件= “build.properties”/> ... 项目>
当你有属性的显著数量,所以它的好,养成使用它的习惯,这种模块化的结构更易于维护。
您还可以覆盖在命令行上的属性。试试这个命令(注意缺少的标志和值之间的空间):
ant -Dbuild.dir=test
您的项目将被改造成测试/。请注意,在这种情况下,旧的类文件在建/意志notbe deleted, because thecleantarget will be run on test/ instead.
More on targets
Ant lets you create "hidden" targets. For example, let's add a couple of targets:
<项目名称= “HelloAnt” 的basedir = “”默认= “编译”> ... <属性名= “jar.dir” 值= “JAR /”/> <属性名= “jar.jarfile” 值= “HelloAnt.jar”/> ... <目标名称= “ - jarinit”> <格式属性= “TODAY_UK” 图案= “yyyy-MM-dd_HH.mm” 区域= “EN_GB”/> TSTAMP> 目标> <目标名称= “罐子” 依赖= “ - jarinit” 描述= “创建二值分布。”> <罐BASEDIR = “$ {build.dir}” destfile =“${jar.dir} / $ {TODAY_UK} / $ {jar.jarfile}” /> 目标> 项目>
目标-jarinitcan't be called from the command line, which makes sense, as all it does is to set up a property,TODAY_UK,which is a timestamp with a particular format. However, the target罐,which can be called from the command line, depends on-jarinit,其中规定了jar文件将被保存在目录中。如果删除要看="-jarinit"attribute, the task will still run, but it'll use the directory ${TODAY_UK} rather than substituting in the property. (This is one property that has to stay in build.xml rather than build.properties.)
事实上,罐really should depend oncompileas well as罐在里面,因为你不能从一个不存在的目录下创建一个jar文件,并且不希望创建一个从旧的build目录!更改要看property to read要看="-jarinit,compile"并运行蚂蚁罐子再次。你会看到蚂蚁运行-jarinit,then traverse the tree of tasks to runclean,在里面,compile, 然后罐。
Help!
现在尝试打字ant -projecthelp。You should get output like this:
Buildfile: /home/juliet/coding/ant/build.xml Main targets: jar Creates the binary distribution. Default target: compile
只有我们的任务显示了一个!这是因为罐task is the only one with adescriptionproperty. Add descriptions to the other tasks, and tryant -projecthelpagain:
Buildfile: /Users/juliet/coding/ant/build.xml Main targets: -jarinit Creates a timestamp. clean Cleans up old build directory. compile Compiles source. init Creates build directory. jar Creates the binary distribution. Default target: compile
请注意,即使隐藏的任务将显示,如果你给它一个描述。
It's also good practice to create a救命target – something like this:
<目标名称=“帮助”取决于=“用法” /> <目标名称=“用法”描述=“显示使用的信息。”> <回声消息=“执行‘蚂蚁-projecthelp’的构建文件的帮助。”/> <回声消息= “为Ant帮助执行 '蚂蚁-help'”。/> 目标>
这说明的Ant目标的另一个功能:你可以有一个“目标别名”,意思是一个目标,只有调用另一个目标。在这里,所有救命does is invoke用法。This means that the user will get help from eitherant helpor蚂蚁用法。你也可以别名build至compile, 例如。这也显示了使用的另一种方式回声task.
It's good practice to change your default to救命or to something else that doesn't do much; if you make the default becompile当蚂蚁吹走旧的生成文件,或需要年龄为大项目运行的用户可能会不高兴。
本教程应该给你足够的信息来开始,如果你想调整你建立了一个小编辑Ant build.xml文件。一旦你觉得舒服,你也可以尝试设置为您定期做其他任务的Ant文件;有没有必要限制自己Java编译!