我试图用以下代码使用init.groovy配置gradle:
def instance = Jenkins.getInstance()
def gradle_inst_exists = false
def gradle4 = new hudson.plugins.gradle.GradleInstallation("grade4", "/usr/share/gradle", [])
def gradleInstallationDescriptor = instance.getDescriptorByType(hudson.plugins.gradle.GradleInstallation.DescriptorImpl)
def installations = gradleInstallationDescriptor.getInstallations()
println(installations.size())
installations.each {
installation = (hudson.plugins.gradle.GradleInstallation) it
if (gradle4.getName() == installation.getName()) {
gradle_inst_exists = true
println("found installation")
}
}
if (!gradle_inst_exists) {
installations.add(gradle4)
gradleInstallationDescriptor.setInstallations(installations)
gradleInstallationDescriptor.save()
}我得到了以下错误:
groovy.lang.MissingMethodException: No signature of method: [Lhudson.plugins.gradle.GradleInstallation;.add() is applicable for argument types: (hudson.plugins.gradle.GradleInstallation) values: [GradleInstallation[grade4]]
Possible solutions: any(), any(groovy.lang.Closure), wait(), min(), last(), sum()一直在用头撞墙试图修正密码。任何帮助都将不胜感激。
发布于 2017-10-16 17:21:18
我手头没有一个Jenkins需要确认,但从这里看来,您似乎是在尝试将add转换为Array而不是List。
看一下getInstallations的源代码,它会返回一个GradleInstallation[]而不是List<GradleInstallation>,就像您所假设的那样。
Groovy的each()支持数组,这很有帮助(通常):
groovy> [1,2,3].toArray().each { print "Got $it." }
Got 1.Got 2.Got 3因此,当您试图修改数组时,您需要对数组进行修改,这通常是很可怕的。最简单的方法可能是转换为列表(使用toList )
def installations = gradleInstallationDescriptor.getInstallations().toList()然后像以前一样添加新项(或者使用<< (如果愿意),然后在调用setter之前将其转换回数组:
gradleInstallationDescriptor.setInstallations((GradleInstallation[]) installations)在Groovy- List、ArrayList和Object的区别方面有良好的背景
希望这有帮助
https://stackoverflow.com/questions/46775432
复制相似问题