How can i convert a FilePath to a File?
Asked Answered
D

3

7

I'm writing a Jenkins plugin and i'm using build.getWorkspace() to get the path to the current workspace. The issue is that this returns a FilePath object.

How can i convert this to a File object?

Demona answered 4/11, 2013 at 23:31 Comment(2)
@Downvoter I don't understand why some people just -1 without saying why. I think we should tell the guy/girl to post some code, ask what they've tried so far, and so on. Just provide some feedback so we can all learn something from this experience.Popup
Question: why do you want a File object? FilePath is actually a pretty expressive API, and is specifically built for handling remote file operations. If you try accessing the workspace using File on a remote slave agent, you're "gonna have a bad time".Jarietta
I
6

Please use the act function and call your own FileCallable implementation if your plugin should work for master and slaves. For more information check the documentation, chapter "Using FilePath smartly" or this stackoverflow answer.

Code example (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;
    }
}
Ignacioignacius answered 6/11, 2013 at 13:32 Comment(0)
P
11

Although I haven't tried this, according to the javadoc you can obtain the URI from which you can then create a file: File myFile = new File(build.getWorkspace().toURI())

Popup answered 4/11, 2013 at 23:42 Comment(2)
If you need the path as a string, use build.workspace.getRemote() instead.Lyndialyndon
Thanks for your very constructive challenge to that answer of mine! May the upvotes be with you ;-)Pectinate
I
6

Please use the act function and call your own FileCallable implementation if your plugin should work for master and slaves. For more information check the documentation, chapter "Using FilePath smartly" or this stackoverflow answer.

Code example (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;
    }
}
Ignacioignacius answered 6/11, 2013 at 13:32 Comment(0)
B
3

This approach File myFile = new File(build.getWorkspace().toURI()) is not the correct solution. I don't know why this has been an accepted anwser till date. The approach mentioned by Sascha Vetter is correct, taking the reference from official Jenkins javadocs

,which clearly says and I quote

Unlike File, which always implies a file path on the current computer, FilePath represents a file path on a specific agent or the controller.

So being an active contributor to Jenkins community, I would reference the answer given by Sascha Vetter.

PS. Reputation point criteria makes me unable to up-vote the correct answer.

Beattie answered 13/8, 2021 at 7:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.