查看所有已安装的插件
Script Console
// def plugins = jenkins.model.Jenkins.instance.getPluginManager().getPlugins()
// plugins.each {println "${it.getShortName()}: ${it.getVersion()}"}
查看插件是否安装
#1 metaClass.getMetaMethod()
我们通过检查插件提供的 Step 是否存在来间接判断插件是否安装:
In groovy, is there a way to check if an object has a given method? – Stack Overflow
this.pipeline.metaClass.getMetaMethod("ansiColor")
# 10/22/2024 该方法无效。
#2 try catch Throwable
Jenkins cps groovy does not catch NoSuchMethodError exceptions – Stack Overflow
private Boolean assertPluginInstalled() {
try {
this.pipeline.ansiColor('')
return true
} catch (java.lang.Throwable e) {
return false
}
}