Jenkinsfile中的共享类返回错误java.io.FileNotFoundException:(没有这样的文件或目录)

Jenkinsfile中的共享类返回错误java.io.FileNotFoundException:(没有这样的文件或目录),jenkins,groovy,jenkins-plugins,jenkins-groovy,jenkins-job-dsl,Jenkins,Groovy,Jenkins Plugins,Jenkins Groovy,Jenkins Job Dsl,几周前,我在詹金斯创建了管道 在mainjenkinsfile中,我想在share类中使用相同的方法,以便重用这个类和其他Jenkinsfile Jenkinsfile stage('Git clone') { checkout scm script { def hosts = load "Prepare.groovy" print "Result " + hosts.PrepareHosts();

几周前,我在詹金斯创建了管道 在mainjenkinsfile中,我想在share类中使用相同的方法,以便重用这个类和其他Jenkinsfile

Jenkinsfile

stage('Git clone') {
    checkout scm
    script {
        def hosts = load "Prepare.groovy"
        print "Result " + hosts.PrepareHosts();
    //    writeFile file: "hosts", text: "[MyHosts]\n"+hosts.PrepareHosts().join('\n')+"\n"
    }
} 
共享类-Prepare.groovy

print("Loaded class MyClass.groovy")

public class Prepare {

    private List service1 = ["dev-app-0001","dev-app-0002","dev-app-0003"];
    private List service2 = ["dev-app-0004","dev-app-0005","dev-app-0006","dev-app-0007"]

    Object GetHosts() {
        return this.service1+this.service2;
    }

}

String PrepareHosts() {
    Prepare pr = new Prepare();
//    pr.setPath(Path)
    pr.GetHosts();

    Object hosts = new File("/data/jenkins/workspace/example1/update_sshkeys/hosts");
//    hosts.createNewFile();
    hosts.write(pr.GetHosts())
    return pr.GetHosts();
}

return this
如果尝试使用hosts.write(pr.GetHosts()),则返回错误

java.io.FileNotFoundException: /data/jenkins/workspace/example1/update_sshkeys/hosts (No such file or directory)
at java.io.FileOutputStream.open0(Native Method)
如果在主文件中使用-此错误不存在

如何解决这个问题