如何在groovy退出时删除临时文件夹? [英] How do I remove a temporary folder on exit in groovy?

查看:214
本文介绍了如何在groovy退出时删除临时文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种解决方案,当我的(Jenkins)Groovy脚本结束时,它将递归地移除使用 Files.createTempDirectory()创建的文件夹。


$ b

如果您阅读文档,createTempDirectory()不会删除文件夹,即使您尝试使用delete-on-exit,如果文件夹内有其他文件,它也会失败。



请注意,我正在寻找一种无需在Groovy脚本末尾添加额外代码或添加try / catch方法的解决方案。这是因为这些Groovy代码是从多个可重用部分编译而成的。



工作解决方案不需要在脚本末尾添加额外的代码,可能使用挂钩机制来注册目录删除操作。

  import java.nio.file.Files 
x = Files.createTempDirectory()
//< - 添加一些魔术钩来告诉在退出
时递归地移除'x'文件夹//很多代码我无法触及



参考文献




解决方案



<$>下面的代码将递归地从临时目录中删除所有文件和文件夹。 p $ p $ mydir = Files.createTempDirectory()
addShutdownHook {
mydir.deleteDir()
}
pre>

这段代码适用于正常的Groovy执行,但它在基于Jenkins Grooby的管道上失败,因为:

 发生的异常:字段委托中的
e
字段中的对象封装
org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@782f5796
导致:java.io.NotSerializableException:sun.nio.fs.UnixPath
在org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:860)

我仍在致力于解决CPS方面的问题,第一次尝试失败,出现相同的错误:

  @NonCPS 
$ m
mydir = Files.createTempDirectory(cp-)
addShutdownHook {
mydir.deleteDir()
println已清理
}
mydir.toString()
}

节点{
mkdtemp('xxx')
}


I am looking for a solution that would recursively remove a folder created with Files.createTempDirectory() when my (Jenkins) Groovy script ends.

If you read the documentation createTempDirectory() does not remove folder and even if you try to use the delete-on-exit, it will fail if the folder has other files inside.

Please note that I am looking for a solution that would not have to add extra code at the end of the Groovy script or to add try/catch methods. That's because these Groovy codes are compiled from multiple re-usable parts.

A working solution should not need to add extra code at the end of the script, probably using a hooking mechanism to register the directory removal operation.

import java.nio.file.Files
x = Files.createTempDirectory()
// <-- add some magic hook to tell to remove 'x' folder recursively on exit
// a lot of code I cannot touch

References

解决方案

The code below will remove recursively all files and folders from the temp directory on exit.

mydir = Files.createTempDirectory()
addShutdownHook {
    mydir.deleteDir()
}

This code works for normal Groovy execution but it fails on Jenkins Grooby based pipelines because:

an exception which occurred:
    in field delegate
    in field closures
    in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@782f5796
Caused: java.io.NotSerializableException: sun.nio.fs.UnixPath
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:860)

So, I am still working on addressing the "CPS" aspect, the first attempts failed with the same errors:

@NonCPS
def mkdtemp(String s) {
    mydir = Files.createTempDirectory("cp-")
        addShutdownHook {
            mydir.deleteDir()
            println "cleaned"
        }
    mydir.toString()
}

node {
   mkdtemp('xxx')
}

这篇关于如何在groovy退出时删除临时文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆