ant实例

<?xml version="1.0" encoding="UTF-8" ?> 
<project name="javaTest" default="sshexec8081" basedir="."> 
    <property name="server" value="192.168.0.112"/>
    <property name="user" value="root"/>
    <property name="password" value="root"/> 
    <property name="command" value="java -jar /home/shutong/helloworld.jar"/>     
  <target name="clean">     
  <delete dir="build"/>    
  </target>
  <target name="compile" depends="clean"> 
    <mkdir dir="build/classes"/>    
    <javac srcdir="src" destdir="build/classes"/>  
  </target>
  <target name="run" depends="compile">     
    <java classname="HelloWorld">         
    <classpath><pathelement path="build/classes"/></classpath>    
    </java>  
  </target>
  <target name="jar" depends="run"> 
  <jar destfile="helloworld.jar" basedir="build/classes">           
    <manifest>               
      <attribute name="Main-class" value="HelloWorld"/>      
    </manifest>       
  </jar>
  </target>
    <property name="name" value="amigo"/>
    <property name="age" value="25"/>
  <target name="sshexecdelete"  description="delete">
    <sshexec host="${server}"  
         username="${user}"  
         password="${password}"  
         port="22"  
         trust="true"  
         command="rm -rf /home/shutong/helloworld.jar"
         />  
         <echo message="删除目标jar包成功" />  
  </target>
  <target name="myscp" depends="jar,sshexecdelete">
    <scp file="./helloworld.jar" todir="${user}:${password}@${server}:/home/shutong/" trust="true"/>
    <echo message="连接上传${server}服务器成功" />  
  </target>  
  <target name="sshexec8081" depends="myscp" description="8081">
    <sshexec host="${server}"  
         username="${user}"  
         password="${password}"  
         port="22"  
         trust="true"  
         command="${command}"
         />  
         <echo message="程序执行成功" />  
  </target> 
</project>

 

版本2:

<?xml version="1.0"?> 
<project name="projectStudy" default="sshexec8081">
        <property name="server" value="192.168.11.43"/>
        <property name="user" value="root"/>
        <property name="password" value="root"/> 
        <property name="java_home" value="C:/Program Files/Java/jre1.8.0_65/lib"/> 
        <property name="server_dir" value="/home/shutong"/> 
        <property name="command" value="/usr/local/jdk/bin/java -jar ${server_dir}/helloworld.jar"/> 
        <property name="rmjar" value="rm -rf ${server_dir}/helloworld.jar"/>
    <target name="sayBaseDir">            
          <echo message="The base dir is: ${basedir}"/>  
          <echo message="Start - - -"/> 
    </target> 
    <target name="clean">     
        <delete file="./bin/com/jim/Hbase.class"/>  
        <echo message="删除class文件" /> 
    </target>
    <target name="compile" depends="clean"> 
            <echo message="开始编译..." />  
            <javac srcdir="./src/com/jim" destdir="./bin"/>  
    </target>
    <target name="run" depends="compile">     
            <java classname="com.jim.Hbase">  
            <sysproperty key="file.encoding" value="UTF-8" />  
            <!--类文件路径,不能加包名-->  
            <classpath><pathelement path="./bin"/></classpath> 
            <classpath>  
             <!--依赖包,多个同理--> 
             <!--<fileset dir="C:\\Program Files\\Java\\jar" includes="jsoup-1.8.1.jar"></fileset>--> 
             <!--<fileset dir="lib" includes="**/*.jar"></fileset>可用通配符-->  
             </classpath> 
            </java>  
     </target>
    <target name="jar" depends="run"> 
      <jar destfile="helloworld.jar" basedir="./bin">           
        <manifest>               
          <attribute name="Main-class" value="com.jim.Hbase"/>      
        </manifest>       
      </jar>
    </target>
    <target name="sshexec8081delete" depends="jar">
            <sshexec host="${server}"  
                 username="${user}"  
                 password="${password}"  
                 port="22"  
                 trust="true"  
                 command="${rmjar}"
                 />  
                 <echo message="程序执行成功" />  
          </target> 
     <target name="myscp" depends="sshexec8081delete">
        <scp file="./helloworld.jar" todir="${user}:${password}@${server}:${server_dir}" trust="true"/>
        <echo message="连接上传${server}服务器成功" />  
      </target> 
    <target name="sshexec8081" depends="myscp" description="8081">
        <sshexec host="${server}"  
             username="${user}"  
             password="${password}"  
             port="22"  
             trust="true"  
             command="${command}"
             />  
             <echo message="程序执行成功" />  
      </target>  
</project>

 

posted @ 2017-12-14 22:38  Blue眼泪2016  阅读(249)  评论(0编辑  收藏  举报