我正在寫一個Jenkins插件,我正在使用build.getWorkspace()
來獲取當前工作空間的路徑。問題是這返回一個FilePath對象。如何將FilePath轉換爲文件?
如何將其轉換爲File對象?
我正在寫一個Jenkins插件,我正在使用build.getWorkspace()
來獲取當前工作空間的路徑。問題是這返回一個FilePath對象。如何將FilePath轉換爲文件?
如何將其轉換爲File對象?
雖然我沒有試過,根據javadoc可以獲取URI,從中可以再創建一個文件:File myFile = new File(build.getWorkspace().toURI())
是的。似乎工作。謝謝。 – daniels
如果您需要將路徑作爲字符串使用,請改爲使用'build.workspace.getRemote()'。 – sschuberth
請使用行爲功能及調用自己的FileCallable實施如果你的插件應該爲主人和奴隸工作。欲瞭解更多信息,請檢查documentation, chapter "Using FilePath smartly"或stackoverflow answer。
代碼示例(source):
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;
}
}
@Downvoter我不明白爲什麼有些人就是-1不說爲什麼。我認爲我們應該告訴那個男孩/女孩張貼一些代碼,詢問他們到目前爲止所嘗試的內容,等等。只需提供一些反饋,以便我們都可以從這種體驗中學到一些東西 – Morfic
**問題:**爲什麼你想要一個'File'對象? 'FilePath'實際上是一個非常富有表現力的API,專門用於處理遠程文件操作。如果您嘗試使用遠程奴隸代理上的「File」訪問工作區,則「會有一段糟糕的時間」。 –