代码之家  ›  专栏  ›  技术社区  ›  SamBeran

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

  •  5
  • SamBeran  · 技术社区  · 16 年前

    是否存在允许我将一组文件复制到Windows(SMB)共享的Ant任务(类似于ftp或scp任务)?

    编辑:为此,我必须使用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;
       }
    }
    
    3 回复  |  直到 11 年前
        1
  •  3
  •   jarnbjo    16 年前

    我不认为有现成的蚂蚁任务,但你可以很容易地在周围建立一个 jcifs (SAMBA库的Java实现)。

        2
  •  2
  •   martin clayton egrunin    15 年前

    你应该能够使用 copy 任务,只要您的共享被装载。

        3
  •  1
  •   AmenAlan    15 年前

    我正在使用与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>