文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>ant学习

ant学习

时间:2006-06-24  来源:hanyh

1,简介
ANT是用JAVA写的类似make的跨平台工具
2,安装
  • 安装JDK。设置环境变量
  • 我使用ubuntu,直接apt-get install ant即成功
3,使用
ant的buildfile用XML文件写成,每个build.xml文件至少有一个project一个默认的target.Targets包含许多task元素,每个task元素可以有一个id属性用来必引用

Projects
一个project有3个属性
name,工程属性 default默认的target,basedir目录,如果忽略,使用build.xml文件所在的目录

Targets
target 可以依赖其他的target,当是并不保证其他的target一定必执行,target依赖关键字depends on,执行顺序传左到右
例如:
<target name="A"/>
<target name="B" depends="A"/>
<target name="C" depends="B"/>
<target name="D" depends="C,B,A"/>
这里如果执行D,根据责任链,执行顺序是ABCD

target可以根据系统的state而有不同的执行行为,通过使用if关键字来实现
<target name="build-module-A" if="module-A-present"/>
<target name="build-own-fake-module-A" unless="module-A-present"/>
这里要注意target的if判断属性只要存在就行,而不是要判断值。上面第一个如果module
-A-present属性存在(即使值为空)也就会执行
如果 no if and no unless必设置,则target一定会被执行
target有4个主要属性:name ,depends ,if ,unless, description(用来设置帮助信息)
把tstamp tasks项目的初始化target是一个好的实践.名为init的target

Tasks
task是一块必执行的代码,有一个通过的结构:
<name attribute1="value1" attribute2="value2" ... />
task可以有一个ID的属性
<taskname id="taskId" ... />
在脚本中访问其他的tasks

可以在另外一个task中如此访问:
project.getReference("task1")
Properties
一个project有一个属性集合,这可以通过property task设置或者在ant外面设置。ant有很多内部预定义属性:如${os.name}

一个例子:
<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>
在init任务里面创建了我们要的build目录,我们添加src,和dist目录,写一个
// The most basic Java program
public class HelloWorld
{
public static void main(String args[]) throws Exception
{
System.out.println("Hello World!");
}
}
HelloWorld.java弄在src目录,然后在当前目录下面执行ant, 看看,是不是自动的给我们
编译了?并打包为现在时间的.jar文件。很简单不是?
相关阅读 更多 +
排行榜 更多 +
点赞小姐姐

点赞小姐姐

休闲益智 下载
火车向前冲

火车向前冲

休闲益智 下载
人狠话不多

人狠话不多

休闲益智 下载