复制到 Windows 共享 (SMB) 的 Ant 任务 [英] Ant Task To Copy To Windows Share (SMB)

查看:30
本文介绍了复制到 Windows 共享 (SMB) 的 Ant 任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有 ant 任务(类似于 ftp 或 scp 任务)允许我将一组文件复制到 Windows (smb) 共享?

Is there an ant task (similar to ftp or scp tasks) that would allow me to copy a set of files to a windows (smb) share?

为此我必须使用 jcifs 创建一个任务.如果有人需要它,这是代码.

I had to create a task using jcifs for this. If anyone needs it, here is the code.

取决于 jcifs 和 apache ioutils.

Depends on jcifs and apache ioutils.

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import jcifs.smb.SmbFile;

import org.apache.commons.io.IOUtils;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.taskdefs.Copy;

public class SmbCopyTask extends Task
{
   private File src;
   private String tgt;

   public void execute() throws BuildException
   {
      try
      {
         recursiveCopy(src);
      }
      catch (Exception e)
      {
         throw new BuildException(e);
      }
   }

   public void recursiveCopy(File fileToCopy) throws IOException
   {

      String relativePath = src.toURI().relativize(fileToCopy.toURI()).getPath();
      SmbFile smbFile = new SmbFile(tgt, relativePath);
      if(!smbFile.exists()) 
      {
         smbFile.createNewFile();
      }
      if(!fileToCopy.isDirectory()) 
      {
         System.out.println(String.format("copying %s to %s", new Object[]{fileToCopy, smbFile}));
         IOUtils.copy(new FileInputStream(fileToCopy), smbFile.getOutputStream());
      }
      else
      {
         File[] files = fileToCopy.listFiles();
         for (int i = 0; i < files.length; i++)
         {
            recursiveCopy(files[i]);
         }
      }
   }

   public void setTgt(String tgt)
   {
      this.tgt = tgt;
   }

   public String getTgt()
   {
      return tgt;
   }

   public void setSrc(File src)
   {
      this.src = src;
   }

   public File getSrc()
   {
      return src;
   }
}

推荐答案

我不认为有一个开箱即用的 ant 任务,但您可以轻松地围绕 jcifs(Samba 库的 Java 实现).

I don't think there is an out of the box ant task for that, but you could easily build one around jcifs (a Java implementation of the Samba library).

这篇关于复制到 Windows 共享 (SMB) 的 Ant 任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆