Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins Groovy post-build script to evaluate file with function

I'm using the following code in post-build step of my Jenkins job:

evaluate(new File("Set-BuildBadge.groovy"));

So it runs a script successfully if it does not contain functions.

If inside the script I define a function for example:

def addSummaryWithText(Icon, Text) {
    manager.createSummary(Icon).appendText(Text, false)
}
...
addSummaryWithText("installer.gif", "Project: " + ProjectName)

then I get the following error:

FATAL: Illegal class name "Set-BuildBadge$addSummaryWithText" in class file Set-BuildBadge$addSummaryWithText java.lang.ClassFormatError: Illegal class name "Set-BuildBadge$addSummaryWithText" in class file Set-BuildBadge$addSummaryWithText at java.lang.ClassLoader.defineClass1(Native Method) ...

I'm not getting how GroovyShell.evaluate works. Can anyone help me?

like image 623
Ivan Avatar asked Dec 20 '22 16:12

Ivan


1 Answers

Looks like the JVM doesn't like class names with a hyphen in them.

By calling your script Set-BuildBadge.groovy internally it is compiled into a class that isn't allowed when you add a function to the script.

Changing the name of the script to SetBuildBadge.groovy will fix it :-)

like image 58
tim_yates Avatar answered Jan 11 '23 22:01

tim_yates