Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the syntax to create a Serializable Groovy Class for Jenkins Workflow/Pipeline

Working with Jenkins Workflow Groovy, I'm running into Serialization errors when trying to create objects for a dead simple class. What kind of syntactic sugar is required to pass the serializable test? The following code is failing. Adding the @Serializable line fails with "class java.io.Serializable is not an annotation in @java.io.Serializable"

//@Serializable 
class TestClass { 
    def hello() { 
        println "halloooo" 
    } 
} 
def myobj = TestClass.newInstance() 
node () { 
    myobj.hello() 
}
like image 801
Jeremy Woodland Avatar asked Dec 10 '22 18:12

Jeremy Woodland


2 Answers

Credit to izzekil for answering this.

class TestClass implements Serializable {}
like image 85
Jeremy Woodland Avatar answered Apr 09 '23 13:04

Jeremy Woodland


It's worth mentioning that Groovy classes don't require implementing Serializable interface explicitly - any Groovy class implements the following two interfaces: Serializable and GroovyObject.

However, there was an issue like the one mentioned in the question in Groovy CPS and Jenkins Workflow CPS libraries. It got fixed in groovy-cps:1.20 and workflow-cps:2.41.

On the other hand, using Class.newInstance() is not the recommended way of initializing objects in the Jenkins Pipeline. This method is blacklisted by default and when you try to approve it, Jenkins warns you that approving this signature may introduce a security vulnerability.

enter image description here

If you take a look at Class.newInstance() method implementation you will see that it uses reflection like crazy. It's just better to instantiate objects with new TestClass() instead of TestClass.newInstance() in this case.

like image 45
Szymon Stepniak Avatar answered Apr 09 '23 13:04

Szymon Stepniak