0

I need to read data from 2 xml files. I have the following pipeline script:

            def targetServerConfigFileContent = readFile file: 'apiproxy/targets/default.xml'               
            def xmlObject1 = new XmlSlurper().parseText(targetServerConfigFileContent);

            echo "-------------"
            existingTargetServerName = xmlObject1.HTTPTargetConnection.LoadBalancer.Server.'@name';

            echo "existingTargetServerName::${existingTargetServerName}"

            targetServerConfigFileContent = null;
            xmlObject1 = null;

            def proxyConfigFileContent = readFile file: 'apiproxy/'+ proxyName + '.xml'
            def xmlObject = new XmlSlurper().parseText(proxyConfigFileContent);

            existingProxyName = xmlObject.'@name'
            existingBasePath = xmlObject.Basepaths              
            existingProxyDesc = xmlObject.Description

            echo "existingProxyName::${existingProxyName}"
            echo "existingProxyDesc::${existingProxyDesc}"
            echo "existingBasePath::${existingBasePath}"

            proxyConfigFileContent = null;
            xmlObject = null;

The moment, I start reading the second file with code, it started throwing the exception.

def proxyConfigFileContent = readFile file: 'apiproxy/'+ proxyName + '.xml'

Exception:

java.lang.IllegalArgumentException: Could not instantiate {message=java.io.NotSerializableException: groovy.util.slurpersupport.Attributes} for EchoStep(message: String): java.lang.ClassCastException: org.jenkinsci.plugins.workflow.steps.EchoStep.message expects class java.lang.String but received class java.io.NotSerializableException

Please let me know what was the wrong with the code.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Prasad
  • 53
  • 1
  • 8
  • is this scripted or declarative? – Matthew Schuchard Oct 29 '18 at 19:24
  • It is scripted. – Prasad Oct 29 '18 at 19:36
  • I got it resolved by having the file read operations one after another. **def targetServerConfigFileContent = readFile file: 'apiproxy/targets/default.xml' def proxyConfigFileContent = readFile file: 'apiproxy/'+ proxyName + '.xml'** – Prasad Oct 29 '18 at 19:42
  • 1
    See: https://stackoverflow.com/a/50146898/4279361 . If if it works right now it will fail to work in the future. You cannot safetly use any object inside Jenkins Pipeline CPS code which is not serializable - and `XmlSlurper` isn't.. – Joerg S Oct 30 '18 at 05:38
  • Joerg S Thank you for the suggestion. I will change the logic as per the link shared. In fact, I tried @NonCPS even before posting my problem here. I will check again and let you know. – Prasad Oct 31 '18 at 13:37

0 Answers0