Use the Java program with Aapche Ant to back up remote server files

Source: Internet
Author: User
Tags echo message secure copy

Many articles have been circulated on the Internet about Java calling Ant API, and Ant is also used to download remote server files and upload local files to the server. Unfortunately, google hasn't found a build File for ant use for a long time. Most of them are ant extensions. Later, I thought about using ant without relying on build files in Java, so I don't lose ant flexibility. Is it configurable ?!

However, sometimes the problem occurs, and the program must automatically control the Build execution.

For more information about Ant, see Baidu or http://aiilive.blog.51cto.com/1925756/1346266.

The following problem is: local file operations on the remote server.

Ant provides the SCP task tool, namely the Secure Copy command. By viewing the Task Dependencise information, you can know that Ant executes SCP to depend on jsch. jar, ant-jsch.jar; the jar we rely on, such as ant-*. jar, jar, Ant provides.

For SCP see: http://www.vpser.net/manage/scp.html

Ant uses scp and sshexec tasks in combination to complete local file upload server, local directory upload server, Server File Download local, Server Directory download local work, the following is the Build configuration.

<? Xml version = "1.0" encoding = "UTF-8"?> <Project name = "test" basedir = ". "default =" r2l_download_dir2dir "> <property name =" username "value =" root "> </property> <property name =" password "value =" root @ 123 "> </property> <property name = "server. user "value =" $ {username }:$ {password} "> </property> <property name =" server. host "value =" 192.168.88.138 "> </property> <property name =" scp: deploy "value =" $ {server. user }@$ {server. host} "> </property> <property Name = "server. dir "value ="/root/Softwares/Tomcat6_linux/Tomcat6/webapps/ROOT/upload/** "> </property> <property name =" server. file "value ="/root/learning/ftp. pl "> </property> <property name =" client. file "value =" ROOT. war "> </property> <property name =" client. dir "value =" E: \ antTask "> </property> <target name =" init "description =" create backup file directory "> <echo message =" Back Up Files on a remote server "/> <mkdir dir = "$ {client. dir} "/> <! -- <Sshexec host = "$ {server. host} "username =" $ {username} "password =" $ {password} "command =" mkdir $ {server. dir} "trust =" true "/> --> </target> <target name =" clean "description =" Clear files on the server "> <delete dir =" $ {client. dir} "includeemptydirs =" yes "verbose =" true "> </delete> <! -- <Sshexec host = "$ {server. host} "username =" $ {username} "password =" $ {password} "command =" rm-rf $ {server. dir} "trust =" true "/> --> </target> <target name =" l2r_upload_file2dir "description =" upload files locally to the server directory "depends =" init "> <echo message = "upload files locally to the server directory"/> <scp localFile = "$ {client. file} "remoteTodir =" $ {scp: deploy }:: {server. dir} "trust =" true "> </scp> </target> <target name =" l2r_upload_dir2dir "description =" Local upload directory to server directory "> <echo message =" upload the local directory to the server directory "/> <scp localFile =" $ {client. dir} "remoteTodir =" $ {scp: deploy }:: {server. dir} "trust =" true "> </scp> </target> <target name =" r2l_download_file2file "description =" download server files to the local directory "depends =" init "> <echo message = "Download server File to local directory"/> <scp remoteFile = "$ {server. user }@$ {server. host }:$ {server. file} "localTofile =" $ {client. dir} "trust =" true "> </scp> </target> <target name =" r2l_download_dir2dir "description =" download server directory to local directory "depends =" init "> <echo message = "server Directory downloaded to local directory"/> <scp remoteFile = "$ {server. user }@$ {server. host }:$ {server. dir} "localTodir =" $ {client. dir} "trust =" true "> </scp> </target> </project>

Use the sshexec task to remotely call Linux commands, and use the scp task to transfer files.

There are many ways to make the Build File executable controllable. In order to control its execution in the program, Java tasks can be used to control it.

The following is the Java operation code for the Build definition execution:

Import java. io. file; import java. util. timer; import java. util. timerTask; import org. apache. tools. ant. defaultLogger; import org. apache. tools. ant. project; import org. apache. tools. ant. helper. projectHelper2; public class TestTask {public static void main (String [] args) {Timer timer = new Timer (); System. out. println (System. currentTimeMillis (); Schedule schedule = new Schedule (); timer. schedule (schedule, 10000,100 00); System. out. println ("... "); System. out. println (schedule. scheduledExecutionTime ();} class Schedule extends TimerTask {@ Override public void run () {Project p = new Project (); p. init (); File build = new File (System. getProperty ("user. dir ") + File. separator + "deploy. xml "); // deploy. xml build File ProjectHelper2 ph = new ProjectHelper2 (); ph. parse (p, build); DefaultLogger dl = new DefaultLogger (); dl. setErrorPrintStream (System. err); dl. setOutputPrintStream (System. out); dl. setMessageOutputLevel (Project. MSG_VERBOSE); p. addBuildListener (dl); p.exe cuteTarget (p. getDefaultTarget ());}}

Note: The Ant version used here is 1.9. The ProjectHelper2 class is used to parse the build file. The ProjectHelper class is the helper class used to parse the build file in the old version.

Ant tasks can be used to complete some specific functions. You can use Build configuration or Java and Ant APIs to implement some functions, an example of a simple Java operation on Ant's Mkdir task:

public void test3() {        Project p = new Project();        Mkdir mkdir = new Mkdir();        File f = new File(System.getProperty("user.dir") + File.separator                + "mkdir");        mkdir.setDir(f);        mkdir.setProject(p);        mkdir.execute();        Assert.assertEquals(true, f.exists());    }

Finally, for Java to back up files on the remote server through Ant, the basic process is to write the desired Ant task configuration information, Java program encapsulates Ant execution, timing and other control settings.

This article is from the "wild horse red dust" blog and will not be reposted!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.