-
Bug
-
Resolution: Not A Defect
-
Minor
-
Job DSL Plugin 1.48
Jenkins 2.7.4
I am using the "configure" command to directly modify the XML since the Template Plugin is not supported directly.
This is simple groovy code that produces (in my opinion) a false ArrayIndexOutOfBoundsException:
job("Template Output Job") { String[] templates = ["Template1", "Template2"] for (int i = 0; i < templates.length; i++) { configure { it / "builders" << "hudson.plugins.templateproject.ProxyBuilder" { "projectName"(templates[i]); } } } }
I can easily fix this by using a temporary variable:
job("Template Output Job") { String[] templates = ["Template1", "Template2"] for (int i = 0; i < templates.length; i++) { String template = templates[i]; configure { it / "builders" << "hudson.plugins.templateproject.ProxyBuilder" { "projectName"(template); } } } }
There may be a good reason for this, but it is very strange from an end user point of view...
This is actually a feature. The configure block takes a closure argument (the block with the curly braces). That block is not evaluated immediately but when the config XML generated after the script has been processed.
When a Groovy closure is defined, it captures the variables but not their values. In your case it captures it i variable. And since the value of i is 2 after the loop (when the closure is actually called), you get an ArrayIndexOutOfBoundsException.
You can test this behavior by running this script in the Jenkins Script Console:
The output is
To avoid this problem you could iterate through the array using each (which is also Groovyier):