要复制到 Windows 共享 (SMB) 的 Ant 任务
是否有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不认为有一个开箱即用的 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).
我正在使用与 Eclipse(Windows) 捆绑在一起的 ant 库,并且可以使用复制任务将文件复制到网络共享。我敢打赌,从命令行使用 ANT 也能实现同样的效果。
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 任务,只要您共享已安装。
You should be able to just use the copy task, as long as your share is mounted.