博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Note of Apache Ant - Using Ant
阅读量:6350 次
发布时间:2019-06-22

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

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,如需转载请自行联系原作者
你可能感兴趣的文章
Oracle随机函数—dbms_random
查看>>
pvr 批量转换
查看>>
linux命令basename使用方法
查看>>
windows下开发库路径解决方案
查看>>
linux迁移mysql数据目录
查看>>
脚本源码安装LNMP
查看>>
Percona Server安装
查看>>
函数为左边表达式
查看>>
2015.06.04 工作任务与心得
查看>>
icinga2使用587端口发邮件
查看>>
hpasmcli查看HP服务器内存状态
查看>>
极客工具
查看>>
【14】Python100例基础练习(1)
查看>>
boost bind使用指南
查看>>
使用ntpdate更新系统时间
查看>>
Android M 特性 Doze and App Standby模式详解
查看>>
IE FF(火狐) line-height兼容详解
查看>>
谷歌Pixel 3吸引三星用户, 但未动摇iPhone地位
查看>>
VUE中使用vuex,cookie,全局变量(少代码示例)
查看>>
grep -w 的解析_学习笔记
查看>>