0

I'm trying to configure gradle using init.groovy with the following code:

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()
}

And I get the following error:

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()

Been beating my head against the wall trying to fix the code. Any help would be greatly appreciated.

BurritoBoy
  • 45
  • 1
  • 6

1 Answers1

0

I haven't got a Jenkins to hand to confirm, but from here it looks like you're trying to add to an Array not a List.

Looking at what seems to be the source for getInstallations, it returns a GradleInstallation[] rather than List<GradleInstallation> as perhaps you're assuming.

Groovy's each() supports Arrays, helpfully (usually):

groovy> [1,2,3].toArray().each { print "Got $it." }
Got 1.Got 2.Got 3

So when you try to modify the array, you'll need to do this with arrays, which is usually horrible. Easiest is probably convert to a list (with toList:

def installations = gradleInstallationDescriptor.getInstallations().toList()

then add your new item as before (or use << if you prefer), then convert back to an array before calling the setter:

gradleInstallationDescriptor.setInstallations((GradleInstall‌​ation[])  installations)

Some good background in Groovy- Difference between List, ArrayList and Object Array

Hope that helps

declension
  • 4,110
  • 22
  • 25
  • I made the changes you suggested but now the script errors on the `setInstallations`. `groovy.lang.MissingMethodException: No signature of method: hudson.plugins.gradle.GradleInstallation$DescriptorImpl.setInstallations() is applicable for argument types: ([Ljava.lang.Object;) values: [[GradleInstallation[grade4]]] Possible solutions: setInstallations([Lhudson.tools.ToolInstallation;), setInstallations([Lhudson.plugins.gradle.GradleInstallation;), getInstallations(), getInstallations()` – BurritoBoy Oct 16 '17 at 19:01
  • I guess you'll need to cast it to a `GradleInstallation[]`, or alternatively define the list explicitly as a `List` – declension Oct 16 '17 at 20:07
  • Solved! Changed `def installations = gradleInstallationDescriptor.getInstallations().toList()` and added cast ` gradleInstallationDescriptor.setInstallations((GradleInstallation[]) installations)` Can you fix your response so I can mark it answered? – BurritoBoy Oct 16 '17 at 20:39
  • And one doesn't need the `toArray()` – BurritoBoy Oct 16 '17 at 20:39