首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Jenkins -使用groovy配置gradle安装

Jenkins -使用groovy配置gradle安装
EN

Stack Overflow用户
提问于 2017-10-16 17:00:25
回答 1查看 580关注 0票数 0

我试图用以下代码使用init.groovy配置gradle:

代码语言:javascript
复制
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()
}

我得到了以下错误:

代码语言:javascript
复制
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()

一直在用头撞墙试图修正密码。任何帮助都将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-16 17:21:18

我手头没有一个Jenkins需要确认,但从这里看来,您似乎是在尝试将add转换为Array而不是List

看一下getInstallations的源代码,它会返回一个GradleInstallation[]而不是List<GradleInstallation>,就像您所假设的那样。

Groovy的each()支持数组,这很有帮助(通常):

代码语言:javascript
复制
groovy> [1,2,3].toArray().each { print "Got $it." }
Got 1.Got 2.Got 3

因此,当您试图修改数组时,您需要对数组进行修改,这通常是很可怕的。最简单的方法可能是转换为列表(使用toList )

代码语言:javascript
复制
def installations = gradleInstallationDescriptor.getInstallations().toList()

然后像以前一样添加新项(或者使用<< (如果愿意),然后在调用setter之前将其转换回数组:

代码语言:javascript
复制
gradleInstallationDescriptor.setInstallations((GradleInstall‌​ation[])  installations)

Groovy- List、ArrayList和Object的区别方面有良好的背景

希望这有帮助

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46775432

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档