Ant手册的内容介绍
访问以下网址可以获取几乎所有的信息.
http://ant.apache.org/
一个范例build.xml
<?xml version="1.0"?>
<project default="dist" name="First Ant Project">
<description>A ant test project.</description>
<property name="message" value="Now show the simplest ant project..."/>
<property name="srcDir" location="src"/>
<property name="distDir" location="dist"/>
<property name="buildDir" location="build"/>
<property name="libDir" location="lib"/>
<property name="jardir" location="jardir"/>
<target name="init">
<echo message="${message}"/>
<tstamp/>
<mkdir dir="${distDir}"/>
<mkdir dir="${buildDir}"/>
<mkdir dir="${jardir}"/>
</target>
<target name="compile" depends="init">
<javac srcdir="${srcDir}" destdir="${buildDir}">
<classpath>
<fileset dir="${libDir}">
<include name="*.jar"/>
</fileset>
</classpath>
</javac>
</target>
<target name="dist" depends="compile">
<jar jarfile="${distDir}/package-${DSTAMP}.jar" basedir="${buildDir}">
<manifest>
<attribute name="Built-By" value="${user.name}"/>
<attribute name="Main-Class" value="package.Main"/>
</manifest>
</jar>
</target>
<target name="zip" depends="dist">
<zip zipfile="${distDir}/output.zip" basedir="${buildDir}"/>
</target>
<target name="clean" depends="dist">
<delete dir="${buildDir}"/>
</target>
<target name="copy" depends="dist">
<copy todir="jardir">
<fileset dir="${distDir}">
<include name="*.jar"/>
</fileset>
</copy>
</target>
</project>
echo 任务 —— 简单地输出给定的消息。
<echo message="Message=${message}"/>
复制文件
<copy file="src/Test.java" tofile="src/TestCopy.java"/>
<copy file="src/Test.java" todir="archive"/>
移动文件
<move file="src/Test.java" tofile="src/TestCopy.java"/>
<move file="src/Test.java" todir="archive"/>
压缩 zip 及 tar 文件
<zip destfile="output.zip" basedir="output"/><!--在1.4中,应当把destfile改成zipfile.-->
tar与此相同
<gzip src="output.tar" zipfile="output.tar.gz"/>
解压缩
<unzip src="output.tar.gz" dest="extractDir"/>
还可以包括 overwrite 属性来控制覆盖行为。默认设置是覆盖与正在被提取的归档文件中的条目相匹配的所有现有文件。相关的任务名称是 untar、unjar、gunzip 和 bunzip2
替换文件中的标记
replace 任务,它执行文件中的查找和替换操作。token 属性指定要查找的字符串,value 属性指定一个新的字符串,查找到的标记字符串的所有实例都被替换为这个新的字符串。
<replace file="input.txt" token="old" value="new"/>
替换操作将在文件本身之内的适当位置进行。为了提供更详细的输出,可把 summary 属性设置为 true。这将导致该任务输出找到和替换的标记字符串实例的数目。
CVS
<?xml version="1.0"?>
<project name="CVS Extract" default="extract" basedir=".">
<property name="cvsRoot" value=":pserver:username:password@192.9.150.11:/cvsroot"/>
<target name="extract">
<cvs cvsRoot="${cvsRoot}" package="cmbc" dest="${basedir}"/>
</target>
</project>
cvs 任务的主要属性是 cvsRoot,它是对 CVS 知识库的完整引用,包括连接方法和用户详细信息。这个参数的格式如下:
[:method:][[user][:password]@]hostname[:[port]]/path/to/repository
package是respository的名字,也就是项目的名称.
当然,首先需要设置cvs命令的路径,在环境变量中更改path.
<?xml version="1.0"?>
<project name="ant mail" default="mail" basedir=".">
<target name="mail">
<mail mailhost="192.9.200.16" encoding="plain" mailport="25" subject="Can u get it?" from="gelh@icss.com.cn" tolist="gelh@icss.com.cn">
</mail>
</target>
</project>
需要mail.jar和activation.jar
注意一定要设置encoding属性
自定义任务
<taskdef name="filesorter"
classname="FileSorter
classpath="."/>
Cvs用法:(待收录)
首先login
cvs -d :pserver:cvsroot@192.9.150.11:/cvsroot login
然后checkout
cvs :pserver:cvsroot@192.9.150.11:/cvsroot checkout cmbc
Creating executable JARs
Creating an executable JAR is easy. You begin by placing all your application code in a single directory. Let's say the main class in your application is com.mycompany.myapp.Sample. You want to create a JAR file that contains the application code and identifies the main class. To do this, create a file called manifest somewhere (not in your application directory), and add the following line to it:
Main-Class: com.mycompany.myapp.Sample |
Then, create the JAR file like this:
jar cmf manifest ExecutableJar.jar application-dir |
That's all there is to it -- now the JAR file ExecutableJar.jar can be executed using java -jar.
An executable JAR must reference all the other dependent JARs it requires through the Class-Path header of the manifest file. The environment variable CLASSPATH and any class path specified on the command line is ignored by the JVM if the -jar option is used.
Launching executable JARs
Now that we've packaged our application into an executable JAR called ExecutableJar.jar, we can launch the application directly from the file using the following command:
java -jar ExecutableJar.jar |
本文地址:http://www.45fan.com/a/question/70299.html