自动部署工具ant
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自动部署工具ant相关的知识,希望对你有一定的参考价值。
学习ant所做的笔记,根据官方网站所举事例
<?xml version="1.0" ?>
<project name="ant-project" default="print-dir">
<property name="name" value="jojo" />
<target name="print-dir">
<echo message="name: ${name} project: ${ant.project.name} "/>
</target>
</project>
运行结果如下:
[[email protected]_test apache-ant-1.9.7]# bin/ant -f /home/huang/test/build.xml
Buildfile: /home/huang/test/build.xml
print-dir:
[echo] name: jojo project: ant-project -----》${ant.project.name},系统变量
BUILD SUCCESSFUL
Total time: 0 seconds
上例中用户设置了名为name的属性,这个属性设置后,在下文中可以通过 ${name}取得这两个属性值。
unzip参数:
[[email protected]_test test]# cat unzip.xml
<project name="unzip_name" default="init">
<property name="src.dir" value="/home/huang/test/src"/>
<property name="dest.dir" value="/home/huang/test/dest"/>
<target name="init" description="compress the Task">
<unzip src="${src.dir}/build.zip" dest="${dest.dir}" />
</target>
</project>
mkdir创建一个src目录:http://ant.apache.org/manual/Tasks/mkdir.html
[[email protected]_test test]# cat file.xml
<project name="create" default="number" basedir="/home/huang/test">
<property name="src.dir" value="src"/>
<target name="number">
<mkdir dir="${src.dir}"/>
</target>
</project>
运行结果:
[[email protected]_test apache-ant-1.9.7]# bin/ant -f /home/huang/test/file.xml
Buildfile: /home/huang/test/file.xml
number:
[mkdir] Created dir: /home/huang/test/src
BUILD SUCCESSFUL
Total time: 0 seconds
delete删除一个目录或者文件:http://ant.apache.org/manual/Tasks/delete.html
[[email protected]_test test]# cat file.xml
<project name="create" default="number" basedir="/home/huang/test">
<property name="src.dir" value="src"/>
<target name="number">
<delete dir="${src.dir}"/>
</target>
</project>
运行结果:
[[email protected]_test apache-ant-1.9.7]# bin/ant -f /home/huang/test/file.xml
Buildfile: /home/huang/test/file.xml
number:
[delete] Deleting directory /home/huang/test/src ---》删除src目录及其目录下的所有文件
BUILD SUCCESSFUL
Total time: 0 seconds
删除目录中带有.bak结尾的文件
<delete>
<fileset dir="." includes="**/*.bak"/> ----》删除当前目录下面到字目录中有.bak的文件
</delete>
copy复制一个文件:http://ant.apache.org/manual/Tasks/copy.html
[[email protected]_test test]# cat file.xml
<project name="create" default="number" basedir="/home/huang/test">
<property name="src.dir" value="src"/>
<target name="number">
<copy file="unzip.xml" tofile="unzip.xml_copy"/> ----》复制一个单文件unzip.xml,unzip.xml_copy
</target>
</project>
运行结果:
[[email protected]_test apache-ant-1.9.7]# bin/ant -f /home/huang/test/file.xml
Buildfile: /home/huang/test/file.xml
number:
[copy] Copying 1 file to /home/huang/test
BUILD SUCCESSFUL
Total time: 0 seconds
<copy file="unzip.xml" todir="${src.dir}"/> 复制单文件到目录下
[[email protected]_test test]# ll src/
total 4
-rw-r--r-- 1 root root 293 Sep 19 14:53 unzip.xml
copy复制一个目录下的所有文件除了某特定的文件:http://ant.apache.org/manual/Tasks/copy.html
[[email protected]_test test]# cat file.xml
<project name="create" default="number" basedir="/home/huang/test">
<property name="src.dir" value="src"/>
<property name="dest.dir" value="dest"/>
<target name="number">
<mkdir dir="${dest.dir}"/>
<copy todir="${dest.dir}">
<fileset dir="${src.dir}" excludes="**/*.xml"/> ----》excludes:排除某个特定文件
</copy>
</target>
</project>
运行如下:
[[email protected]_test apache-ant-1.9.7]$ bin/ant -f /home/huang/test/file.xml
Buildfile: /home/huang/test/file.xml
number:
[copy] Copying 1 file to /home/huang/test/dest
BUILD SUCCESSFUL
Total time: 0 seconds
copy将某目录下面的所有文件全部copy为.bak模式
[[email protected]_test test]$ cat file.xml
<project name="create" default="number" basedir="/home/huang/test">
<property name="src.dir" value="src"/>
<property name="dest.dir" value="dest"/>
<target name="number">
<copy todir="${dest.dir}">
<fileset dir="${src.dir}"/>
<globmapper from="*" to="*.bak"/>
</copy>
</target>
</project>
运行效果如下:
[[email protected]_test apache-ant-1.9.7]$ bin/ant -f /home/huang/test/file.xml
Buildfile: /home/huang/test/file.xml
number:
[copy] Copying 2 files to /home/huang/test/dest
[copy] Copied 1 empty directory to 1 empty directory under /home/huang/test/dest
BUILD SUCCESSFUL
Total time: 0 seconds
[[email protected]_test test]$ ll dest/
total 4
-rw-rw-r-- 1 huang huang 0 Sep 19 15:40 test.txt.bak
-rw-rw-r-- 1 huang huang 293 Sep 19 15:40 unzip.xml.bak
一个jar程序的编译运行过程:
<project name="HelloWorld" basedir="/home/huang/test/" default="main">
<property name="src.dir" value="src"/>
<property name="build.dir" value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir" value="${build.dir}/jar"/>
<property name="main-class" value="oata.HelloWorld"/>
<target name="clean">
<delete dir="${build.dir}"/>
</target>
<target name="compile">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}"/>
</target>
<target name="jar" depends="compile">
<mkdir dir="${jar.dir}"/>
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
#######${ant.project.name}----->HelloWorld(<project name="HelloWorld")
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
</manifest>
</jar>
</target>
<target name="run" depends="jar">
<java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>
</target>
<target name="clean-build" depends="clean,jar"/>
<target name="main" depends="clean,run"/>
</project>
Now it‘s easier, just do a ant and you will get
Buildfile: build.xml
clean:
compile:
[mkdir] Created dir: C:\...\build\classes
[javac] Compiling 1 source file to C:\...\build\classes
jar:
[mkdir] Created dir: C:\...\build\jar
[jar] Building jar: C:\...\build\jar\HelloWorld.jar
run:
[java] Hello World
main:
BUILD SUCCESSFUL
以上是关于自动部署工具ant的主要内容,如果未能解决你的问题,请参考以下文章