SamBeran
SamBeran

Reputation: 1934

Ant Task To Copy To Windows Share (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?

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

Upvotes: 4

Views: 8123

Answers (3)

AmenAlan
AmenAlan

Reputation: 51

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>

Upvotes: 3

crowne
crowne

Reputation: 8534

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

Upvotes: 2

jarnbjo
jarnbjo

Reputation: 34313

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).

Upvotes: 3

Related Questions