如何将FilePath转换为文件?

dan*_*els 7 hudson hudson-plugins jenkins jenkins-plugins

我正在写一个Jenkins插件,我正在使用它build.getWorkspace()来获取当前工作空间的路径.问题是这会返回一个FilePath对象.

我怎样才能将其转换为File对象?

Mor*_*fic 11

虽然我没有尝试过,但根据javadoc,您可以获取URI,然后您可以从中创建文件:File myFile = new File(build.getWorkspace().toURI())


Sas*_*ter 5

如果您的插件适用于 master 和 slaves,请使用act函数并调用您自己的FileCallable实现。有关更多信息,请查看文档、“巧妙地使用 FilePath”一章或此stackoverflow 答案

代码示例(来源):

void someMethod(FilePath file) {
    // make 'file' a fresh empty directory.
    file.act(new Freshen());
}
// if 'file' is on a different node, this FileCallable will
// be transferred to that node and executed there.
private static final class Freshen implements FileCallable<Void> {
    private static final long serialVersionUID = 1;
    @Override public Void invoke(File f, VirtualChannel channel) {
        // f and file represent the same thing
        f.deleteContents();
        f.mkdirs();
        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)