1、下载ant软件包,本次下载的是apache-ant-1.9.6-bin.tar.gz
2、传到Linux服务器上,我传到/usr/local/下
3、解压缩,并创建软连接
[[email protected] local]# tar xf apache-ant-1.9.6-bin.tar.gz
[[email protected] local]# ln -sv apache-ant-1.9.6 ant196
4、配置Java环境变量
[[email protected] profile.d]# vim java.sh
JAVA_HOME=/root/tomcat/jdk1.7.0_71
ANT_HOME=/usr/local/ant196
PATH=$JAVA_HOME/bin:$ANT_HOME/bin:$PATH
export JAVA_HOME ANT_HOME PATH
~
5、查看是否成功安装
[[email protected] bin]# ant -version
Apache Ant(TM) version 1.9.6 compiled on June 29 2015
[[email protected] bin]#
6、实例
实验环境Linux系统,两个节点node1:IP 192.168.164.129和node2:IP 192.168.164.128
实验需求:
ant安装在node1节点,安装之后,在此实现功能是在node1节点上到node2节点执行创建/tmp/test01,然后再在/tmp/test01目录下创建文件test001.txt,还有把本地的/tmp/下的test.jar包传到node2节点上的/tmp/下。
在node1上操作
[[email protected] xml]# pwd
/usr/local/xml
[[email protected] xml]# ls
ant01.sh build.properties build.xml deploy01.xml
[[email protected] xml]# cat ant01.sh
#JAVA_HOME=/root/tomcat/jdk1.7.0_71
#ANT_HOME=/usr/local/ant196
#PATH=$JAVA_HOME/bin:$ANT_HOME/bin:$PATH
#export JAVA_HOME ANT_HOME PATH
echo "==============`date +%Y%m%d_%H:%M:%S`===============" > /usr/local/logs/ant01.log
nohup /usr/local/ant196/bin/ant -f /usr/local/xml/build.xml test01 >> /usr/local/logs/ant01.log 2>&1 &
[[email protected] xml]# cat build.xml
<?xml version="1.0" encoding="UTF-8" ?>
<project name="test01" basedir=".">
<property file="build.properties"/>
<target name="test01" description="test01">
<ant antfile="/usr/local/xml/deploy01.xml" dir="." target="deploy-test"/>
</target>
</project>
[[email protected] xml]#
[[email protected] xml]# cat deploy01.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="deploy-test" default="deploy-test" basedir=".">
<target name="deploy-test" description="deploy-test">
<parallel threadCount="1">
<sequential>
<sshexec host="${node1}" username="${name1}" password="${passwd1}" trust="true" command="mkdir /tmp/test01;touch /tmp/test01/test001.txt"/>
<scp file="${src}/test01.jar" todir="${name1}@${node1}:/tmp/" password="${passwd1}" trust="true"/>
</sequential>
</parallel>
</target>
</project>
[[email protected] xml]#
[[email protected] xml]# cat build.properties
src=/tmp
node1=192.168.164.128
name1=root
passwd1=changyu
[[email protected] xml]#
在192.168.164.128端查看
[[email protected] tmp]# ls
test01 test01.jar
[[email protected] tmp]# cd test01
[[email protected] test01]# ls
test001.txt
[[email protected] test01]#