Note of Apache Ant - Using Ant

简介:
Using Ant
 
project has three attributes:
name
default
basedir
The base directory from which all path calculations are done. This attribute might be overridden by setting the "basedir" property beforehand. When this is done, it must be omitted in the project tag. If neither the attribute nor the property have been set, the parent directory of the buildfile will be used.
 
Each project defines one or more targets. A target is a set of tasks you want to be executed. When starting Ant, you can select which target(s) you want to have executed. When no target is given, the project's default is used.
 
A target has the following attributes:
name
depends
a comma-separated list of names of targets on which this target depends.
if
the name of the property that must be set in order for this target to execute.
unless
the name of the property that must not be set in order for this target to execute.
description
a short description of this target's function.
 
 
A target can depend on other targets.
eg,
< target  name ="A" /> 
< target  name ="B"  depends ="A" /> 
< target  name ="C"  depends ="B" /> 
< target  name ="D"  depends ="C,B,A" />
Explain:
Suppose we want to execute target D. From its depends attribute, you might think that first target C, then B and then A is executed. Wrong! C depends on B, and B depends on A, so first A is executed, then B, then C, and finally D.
 
Note: Ant will only check whether the property has been set, the value doesn't matter. A property set to the empty string is still an existing property.
eg
< target  name ="build-module-A"  if ="module-A-present" /> 
< target  name ="build-own-fake-module-A"  unless ="module-A-present" /> 
In the first example, if the module-A-present property is set (to any value, e.g. false), the target will be run. In the second example, if the module-A-present property is set (again, to any value), the target will not be run.
 
If you want to check multiple conditions, you can use a dependend target for computing the result for the check:
< target  name ="myTarget"  depends ="myTarget.check"  if ="myTarget.run" > 
         < echo >Files foo.txt and bar.txt are present. </ echo > 
</ target > 

< target  name ="myTarget.check" > 
         < condition  property ="myTarget.run" > 
                 < and > 
                         < available  file ="foo.txt" /> 
                         < available  file ="bar.txt" /> 
                 </ and > 
         </ condition > 
</ target > 
 
 
A task is a piece of code that can be executed.
 
Tasks have a common structure:
< name  attribute1 ="value1"  attribute2 ="value2" ...  /> 
 
A project can have a set of properties.
Properties may be used in the value of task attributes. This is done by placing the property name between "${" and "}" in the attribute value. For example, if there is a "builddir" property with the value "build", then this could be used in an attribute like this: ${builddir}/classes. This is resolved at run-time as build/classes.
 
 
In the event you should need to include this construct literally (i.e. without property substitutions), simply "escape" the '$' character by doubling it. To continue the previous example: 
< echo >$${builddir}=${builddir} </ echo >
 
will echo this message: 
${builddir}=build/classes
 
Example Buildfile
< project  name ="MyProject"  default ="dist"  basedir ="." > 
         < description > 
                simple example build file 
         </ description > 
    <!--  set global properties for this build --> 
     < property  name ="src"  location ="src" /> 
     < property  name ="build"  location ="build" /> 
     < property  name ="dist"     location ="dist" /> 

     < target  name ="init" > 
        <!--  Create the time stamp --> 
         < tstamp /> 
        <!--  Create the build directory structure used by compile --> 
         < mkdir  dir ="${build}" /> 
     </ target > 

     < target  name ="compile"  depends ="init" 
                 description ="compile the source "  > 
        <!--  Compile the java code from ${src} into ${build} --> 
         < javac  srcdir ="${src}"  destdir ="${build}" /> 
     </ target > 

     < target  name ="dist"  depends ="compile" 
                 description ="generate the distribution"  > 
        <!--  Create the distribution directory --> 
         < mkdir  dir ="${dist}/lib" /> 

        <!--  Put everything in ${build} into the MyProject-${DSTAMP}.jar file --> 
         < jar  jarfile ="${dist}/lib/MyProject-${DSTAMP}.jar"  basedir ="${build}" /> 
     </ target > 

     < target  name ="clean" 
                 description ="clean up"  > 
        <!--  Delete the ${build} and ${dist} directory trees --> 
         < delete  dir ="${build}" /> 
         < delete  dir ="${dist}" /> 
     </ target > 
</ project > 
 
 
one or more Resource Collections can be specified as nested elements (these must consist of file-type resources only). Additionally, it should be noted that although resource collections are processed in the order encountered, certain resource collection types such as fileset, dirset and files are undefined in terms of order.
< classpath > 
             < pathelement  path ="${classpath}" /> 
             < fileset  dir ="lib" > 
                 < include  name ="**/*.jar" /> 
             </ fileset > 
             < pathelement  location ="classes" /> 
             < dirset  dir ="${build.dir}" > 
                 < include  name ="apps/**/classes" /> 
                 < exclude  name ="apps/**/*Test*" /> 
             </ dirset > 
             < filelist  refid ="third-party_jars" /> 
         </ classpath > 
 
This builds a path that holds the value of ${classpath}, followed by all jar files in the lib directory, the classes directory, all directories named classes under the apps subdirectory of ${build.dir}, except those that have the text Test in their name, and the files specified in the referenced FileList.
 
Note:
Fileset 
A FileSet is a group of files. These files can be found in a directory tree starting in a base directory and are matched by patterns taken from a number of PatternSets and Selectors.
 
A path-like structure can include a reference to another path-like structure (a path being itself a resource collection) via nested <path> elements:
< path  id ="base.path" > 
             < pathelement  path ="${classpath}" /> 
             < fileset  dir ="lib" > 
                 < include  name ="**/*.jar" /> 
             </ fileset > 
             < pathelement  location ="classes" /> 
         </ path > 

         < path  id ="tests.path" > 
             < path  refid ="base.path" /> 
             < pathelement  location ="testclasses" /> 
         </ path > 
 
The shortcuts previously mentioned for <classpath> are also valid for <path>.For example:
< path  id ="base.path" > 
             < pathelement  path ="${classpath}" /> 
         </ path >
 
can be written as:
 
< path  id ="base.path"  path ="${classpath}" /> 
 
 
Typic Builder.xml of JSF
< project  name ="JSFDemo"  basedir ="../"  default ="deploy" > 

        <!--  Project settings --> 
         < property  name ="project.distname"  value ="JSFDemo" /> 

        <!--  Local system paths --> 
         < property  file ="${basedir}/ant/build.properties" /> 
         < property  name ="webroot.dir"  value ="${basedir}/WebContent" /> 
         < property  name ="webinf.dir"  value ="${webroot.dir}/WEB-INF" /> 
         < property  name ="build.dir"  value ="build" /> 

        <!--  classpath for JSF 1.1.01 --> 
         < path  id ="compile.classpath" > 
                 < pathelement path  ="${webinf.dir}/lib/commons-beanutils.jar" /> 
                 < pathelement path  ="${webinf.dir}/lib/commons-collections.jar" /> 
                 < pathelement path  ="${webinf.dir}/lib/commons-digester.jar" /> 
                 < pathelement path  ="${webinf.dir}/lib/commons-logging.jar" /> 
                 < pathelement path  ="${webinf.dir}/lib/jsf-api.jar" /> 
                 < pathelement path  ="${webinf.dir}/lib/jsf-impl.jar" /> 
                 < pathelement path  ="${webinf.dir}/lib/jstl.jar" /> 
                 < pathelement path  ="${webinf.dir}/lib/standard.jar" /> 
                 < pathelement path  ="${webinf.dir}/classes" /> 
                 < pathelement path  ="${classpath.external}" /> 
                 < pathelement path  ="${classpath}" /> 
         </ path > 

        <!--  define your folder for deployment --> 
         < property  name ="deploy.dir"  value ="deploy" /> 


        <!--  Check timestamp on files --> 
         < target  name ="prepare" > 
                 < tstamp /> 
         </ target > 

        <!--  Copy any resource or configuration files --> 
         < target  name ="resources" > 
                 < copy  todir ="${webinf.dir}/classes"  includeEmptyDirs ="no" > 
                         < fileset  dir ="JavaSource" > 
                         < patternset > 
                                 < include  name ="**/*.conf" /> 
                                 < include  name ="**/*.properties" /> 
                                 < include  name ="**/*.xml" /> 
                         </ patternset > 
                         </ fileset > 
                 </ copy > 
         </ target > 

        <!--  Normal build of application --> 
         < target  name ="compile"  depends ="prepare,resources" > 
                 < javac  srcdir ="JavaSource"  destdir ="${webinf.dir}/classes" > 
                         < classpath  refid ="compile.classpath" /> 
                 </ javac > 
         </ target > 

        <!--  Remove classes directory for clean build --> 
         < target  name ="clean" 
             description ="Prepare for clean build" > 
             < delete  dir ="${webinf.dir}/classes" /> 
             < mkdir     dir ="${webinf.dir}/classes" /> 
         </ target > 

        <!--  Build entire project --> 
         < target  name ="build"  depends ="prepare,compile" /> 
         < target  name ="rebuild"  depends ="clean,prepare,compile" /> 

        <!--  Create binary distribution --> 
         < target  name ="war"  depends ="build" > 
             < mkdir  dir ="${build.dir}" /> 
             < war 
                 basedir ="${webroot.dir}" 
                 warfile ="${build.dir}/${project.distname}.war" 
                 webxml ="${webinf.dir}/web.xml" > 
                 < exclude  name ="WEB-INF/${build.dir}/**" /> 
                 < exclude  name ="WEB-INF/src/**" /> 
                 < exclude  name ="WEB-INF/web.xml" /> 
              </ war > 

         </ target > 

         < target  name ="deploy"  depends ="war" > 
              < delete  file ="${deploy.dir}/${project.distname}.war" /> 
              < delete  dir ="${deploy.dir}/${project.distname}" /> 
              < copy  file ="${build.dir}/${project.distname}.war"  todir ="${deploy.dir}" /> 
         </ target > 

</ project > 


    本文转自danni505 51CTO博客,原文链接:http://blog.51cto.com/danni505/172597,如需转载请自行联系原作者



相关文章
|
XML Java Apache
【Apache Ant】ANT解析以及ANT在myEclipse中的使用
【Apache Ant】ANT解析以及ANT在myEclipse中的使用
278 0
【Apache Ant】ANT解析以及ANT在myEclipse中的使用
|
Java Apache 数据格式
【Apache Ant】ANT解析以及ANT在myEclipse中的使用
转载请注明出处:http://blog.csdn.net/qq_26525215 本文源自【大学之旅_谙忆的博客】 维基百科上对Ant的介绍: Apache Ant,是一个将软件编译、测试、部署等步骤联系在一起加以自动化的一个工具,大多用于Java环境中的软件开发。
1326 0
|
Java Apache
手动利用Apache Ant构建部署自己的Java项目
虽然Eclipse从3.0开始已经继承了Ant插件,大家的项目可以自动构建 部署,此篇文章看也没多大用处,但是手动去自己使用以下 Ant来构建自己的Java项目也是一件有意义的事情,虽然现在已经没用人用Javac Java命令行的方式 来搞程序。
1021 0
|
Java Apache Maven
替代Apache Ant的最佳工具
尽管最近Apache Ant发布了最新的1.9.2版,下载地址见:http://ant.apache.org/bindownload.cgi 但是,Apache Ant毕竟是几年前的产物,它的有些方法、思想和理念已经落后于当前主流的Maven等工具。
932 0
|
Java Apache Maven
替代Apache Ant的最佳工具
版权声明:本文为博主chszs的原创文章,未经博主允许不得转载。 https://blog.csdn.net/chszs/article/details/9322389 《替代Apache Ant的最佳工具》 作者:chszs,转载需注明。
919 0
|
1月前
|
SQL Java API
Apache Flink 2.0-preview released
Apache Flink 社区正积极筹备 Flink 2.0 的发布,这是自 Flink 1.0 发布以来的首个重大更新。Flink 2.0 将引入多项激动人心的功能和改进,包括存算分离状态管理、物化表、批作业自适应执行等,同时也包含了一些不兼容的变更。目前提供的预览版旨在让用户提前尝试新功能并收集反馈,但不建议在生产环境中使用。
622 13
Apache Flink 2.0-preview released
|
1月前
|
存储 缓存 算法
分布式锁服务深度解析:以Apache Flink的Checkpointing机制为例
【10月更文挑战第7天】在分布式系统中,多个进程或节点可能需要同时访问和操作共享资源。为了确保数据的一致性和系统的稳定性,我们需要一种机制来协调这些进程或节点的访问,避免并发冲突和竞态条件。分布式锁服务正是为此而生的一种解决方案。它通过在网络环境中实现锁机制,确保同一时间只有一个进程或节点能够访问和操作共享资源。
72 3

推荐镜像

更多