管道脚本中的java

管道脚本中的java

本文介绍了管道脚本中的java.io.NotSerializableException错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码加载Groovy文件并传递参数:

I'm using below code to load a Groovy file and to pass parameter:

@NonCPS
def ld() {
   def pck = load '/tmp/Provsioning/e.groovy';
   return pck.xmlParseData("${params.hos_nam}");
}
node {
   stage ('Deploying Packages'){
      def aby = ld();
   }
}

其中${params.hos_nam}是构建参数,而installpackage常规如下所示

where ${params.hos_nam} is a build parameter and the installpackage groovy follows as below

public class ReadXMLFile {
   def xmlParseData(String g){
      installPackage(a,b,c);
      input 'proceed'
      aemRestart(b);
   }
   def installPackage(String a, String b,String c){
      //some code
   }
   def aemRestart(String a){
      //some code
   }
}

我不确定为什么会发生以下错误:

I'm not sure why the below error occurs:

an exception which occurred:
    in field val$body
    in field closures
    in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@67aecf21
    Caused: java.io.NotSerializableException:    org.codehaus.groovy.runtime.InvokerHelper$1
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:860)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854)
    at org.jboss.marshalling.river.BlockMarshaller.doWriteObject(BlockMarshaller.java:65)
    at org.jboss.marshalling.river.BlockMarshaller.writeObject(BlockMarshaller.java:56)
    at org.jboss.marshalling.MarshallerObjectOutputStream.writeObjectOverride(MarshallerObjectOutputStream.java:50)
    at org.jboss.marshalling.river.RiverObjectOutputStream.writeObjectOverride(RiverObjectOutputStream.java:179)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:344)
    at java.util.HashMap.internalWriteEntries(HashMap.java:1785)
    at java.util.HashMap.writeObject(HashMap.java:1362)
    at sun.reflect.GeneratedMethodAccessor198.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.jboss.marshalling.reflect.SerializableClass.callWriteObject(SerializableClass.java:271)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:976)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854)
    at org.jboss.marshalling.AbstractObjectOutput.writeObject(AbstractObjectOutput.java:58)
    at org.jboss.marshalling.AbstractMarshaller.writeObject(AbstractMarshaller.java:111)
    at org.jenkinsci.plugins.workflow.support.pickles.serialization.RiverWriter.writeObject(RiverWriter.java:140)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.saveProgram(CpsThreadGroup.java:458)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.saveProgram(CpsThreadGroup.java:434)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.saveProgramIfPossible(CpsThreadGroup.java:422)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:362)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$100(CpsThreadGroup.java:82)

Finished: FAILURE

推荐答案

在管道的两个步骤之间包含在变量中的对象必须是可序列化.

object that you hold in a variable between two steps of the pipeline must be Serializable.

class A{
  def f(){
    return [hello:'world']
  }
}

node{
  def a = new A()
  def b = a.f()
}

可能会抛出NotSerializableException,因为类A不可序列化

could throw NotSerializableException because class A non Serializable

要解决此问题,请将所有与不可序列化变量一起使用的代码放入@NonCPS带注释的函数中:

to solve this put all the code that works with non serializable variables into @NonCPS annotated function:

class A{
  def f(){
    return [hello:'world'] //hashmap itself is serializable
  }
}

@NonCPS
def f1(){
  def a = new A()
  return a.f()
}

node{
  def b = f1()
}

PS:我没有检查代码,只是为您提供示例.

PS: I did not check the code but just to provide you an example..

这篇关于管道脚本中的java.io.NotSerializableException错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 06:47