要复制到 Windows 共享 (SMB) 的 Ant 任务

发布于 2024-08-18 11:15:33 字数 1674 浏览 3 评论 0原文

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

编辑:我必须为此使用 jcifs 创建一个任务。如果有人需要,这里是代码。

取决于 jcifs 和 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;
   }
}

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?

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

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;
   }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

童话里做英雄 2024-08-25 11:15:33

我不认为有一个开箱即用的 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).

硪扪都還晓 2024-08-25 11:15:33

我正在使用与 Eclipse(Windows) 捆绑在一起的 ant 库,并且可以使用复制任务将文件复制到网络共享。我敢打赌,从命令行使用 ANT 也能实现同样的效果。

<copy todir="//server_name/share_name" overwrite="true" verbose="true">
<fileset dir="./WebContent">
    <patternset refid="sources"/>
    <different targetdir="//server_name/share_name" ignoreFileTimes="true"/>
</fileset></copy>

I'm using the ant libraries bundled with Eclipse(Windows) and I can use the copy task to copy files to a network share. I bet the same works with ANT from the command line as well.

<copy todir="//server_name/share_name" overwrite="true" verbose="true">
<fileset dir="./WebContent">
    <patternset refid="sources"/>
    <different targetdir="//server_name/share_name" ignoreFileTimes="true"/>
</fileset></copy>
鼻尖触碰 2024-08-25 11:15:33

您应该能够只使用 copy 任务,只要您共享已安装。

You should be able to just use the copy task, as long as your share is mounted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文