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()
}
Credit to izzekil for answering this.
class TestClass implements Serializable {}
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With