您的位置:首页 > 其它

Ant学习总结5(配合Ant视频8,9)

2013-10-16 23:40 225 查看
重点:

Ant的属性介绍:
<property name="build.dir" value="build"/>
注意:一般对于常量都会定义成为属性。

<property name="build.classes" value="${build.dir}/class" />

注意:对于/ \的问题,在widows和liunx中对于斜杠的支持是不一样的。
windows 中为 \ linux中为:\

对于value而言,是不会根据操作系统来自己变动的,你写成为:
build/class 则会去这样找找build/class
build\class 则会去这样找build\class

第二点:value是相对路径,即使基于当前文件的路径来继续。

可以测试,用echo
<echo>${build.class}</echo>

输出为:build\class

此时,可以用location 这个属性,自动会更具操作系统来转化识别。

<property name="build.classes" location="${build.dir}/class" />

使用属性定义相应的路径的时候,一定要使用location,而不要使用value.

location :是绝对路径。

总结:1)值用value,路径用location
2)路径不建议在外部文件中定义,因为在外部文件中,就不会设置location的值了,相当于value了。
3)环境变量引入方法:
<property enviroment="env" />
然后使用env

<?xml version="1.0" encoding="UTF-8"?>
<project default="execute">

<property name="build.dir" location="build"></property>
<!--使用属性定义相应的路径时,一定使用location而不要使用value-->
<property name="build.classes" location="${build.dir}\classes"></property>
<property name="build.src" location="${build.dir}/src"></property>
<property name="build.lib.dir" location="${build.dir}/dist"></property>
<!--<property name="execute.class" value="ant.zttc.edu.cn.HelloWorld"/>
<property name="jar.name" value="hello.jar"></property>
-->

<!--如果属性太多,可以将属性放置到一个外部文件中定义,之后进行引用
特别注意:如果是路径不建议在外部文件中定义,因为此时就不会设置location的值-->
<property file="build.properties"></property>

<!--把环境变量中的参数到处到env这个变量中-->
<property environment="env"></property>

<target name="test">
<echo>${ant.home}</echo>
<echo>${ant.version}</echo>
<echo>${env.CATALINA_HOME}</echo>
<echo>${env.OS}</echo>
</target>

<!--fileset可以设定一组文件集来进行操作,dir指明文件集要进行选择的路径,
通过id可以指定这个文件的名称,在使用的时候进行直接的引入
include和exclude可以设定包含返回和排除范围**/*.*所有目录中的所有文件
-->
<fileset id="src.path" dir="src">
<include name="**/*.*"/>
<!--<exclude name="**/*.java"/>-->
</fileset>

<target name="init">
<delete dir="${build.dir}"></delete>
<mkdir dir="${build.dir}"/>
<mkdir dir="${build.src}"/>
<mkdir dir="${build.classes}"/>
<mkdir dir="${build.lib.dir}"/>
</target>

<target name="copySrc" depends="init">
<copy todir="${build.src}">
<fileset refid="src.path"></fileset>
</copy>
</target>

<target name="compile" depends="init">
<javac destdir="${build.classes}" srcdir="src"></javac>
</target>

<target name="jar" depends="compile">
<jar destfile="${build.lib.dir}/${jar.name}" basedir="${build.classes}">
<manifest>
<attribute name="Main-Class" value="${execute.class}"/>
<attribute name="Build-By" value="Konghao"/>
</manifest>
</jar>
</target>

<target name="execute" depends="jar,copySrc">

<echo>基于类路径的classname来完成执行</echo>
<java classname="${execute.class}" classpath="${build.classes}">
<arg value="张三"/>
<arg value="李四"/>
<arg value="王五"/>
</java>

<echo>基于jar文件执行</echo>
<java jar="${build.lib.dir}/${jar.name}" fork="true">
<arg value="张三"/>
<arg value="李四"/>
<arg value="王五"/>
</java>
</target>
</project>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: