- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在构建期间执行的 Jenkinsfile 中运行以下 groovy 脚本:
import java.util.stream.Collectors
import java.util.stream.Stream
import groovy.transform.Field
@Field
Map<String, String> sampleMap = [
'SampleKey1': 'SampleValue1',
'SampleKey2': 'SampleValue2'
]
//example param value: "C/SampleKey1/someFile, C/SampleKey2/someFile2"
private Collection<String> getValues(String param) {
Stream.of(param.split(','))
.map { getValueFromOnePath(it) }
.filter { !it.isEmpty() }
.distinct()
.collect(Collectors.toList())
}
private String getValueFromOnePath(String path) {
String[] pathParts = path.split('/')
if (pathParts.size() < 2) {
return ''
}
return sampleMap[pathParts[1]] ?: ''
}
上述代码对参数值的预期结果为:
"C/SampleKey1/someFile, C/SampleKey2/someFile2"
是:
[SampleValue1, SampleValue2]
但是,当在 Jenkins 上执行此代码时,我收到一个我无法理解的奇怪错误(为什么会发生):
hudson.remoting.ProxyException: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'SampleValue1' with class 'java.lang.String' to class 'java.util.Collection' at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnSAM(DefaultTypeTransformation.java:405) at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnNumber(DefaultTypeTransformation.java:319) at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnCollection(DefaultTypeTransformation.java:267) at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:219) at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.castToType(ScriptBytecodeAdapter.java:603) at Unknown.Unknown(Unknown) at cps.transform(Native Method) at com.cloudbees.groovy.cps.impl.LocalVariableBlock$LocalVariable.set(LocalVariableBlock.java:45) at com.cloudbees.groovy.cps.impl.AssignmentBlock$ContinuationImpl.assignAndDone(AssignmentBlock.java:70) at sun.reflect.GeneratedMethodAccessor303.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72) at com.cloudbees.groovy.cps.impl.LocalVariableBlock$LocalVariable.get(LocalVariableBlock.java:39) at com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30) at com.cloudbees.groovy.cps.impl.LocalVariableBlock.evalLValue(LocalVariableBlock.java:28) at com.cloudbees.groovy.cps.LValueBlock$BlockImpl.eval(LValueBlock.java:55) at com.cloudbees.groovy.cps.LValueBlock.eval(LValueBlock.java:16) at com.cloudbees.groovy.cps.Next.step(Next.java:83) at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:174) at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:163) at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:122) at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:261) at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163) at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:19) at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:35) at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:32) at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:108) at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:32) at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:174) at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:331) at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$100(CpsThreadGroup.java:82) at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:243) at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:231) at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:64) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:112) at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748)
有什么想法吗?
最佳答案
您可以尝试使用@NonCPS 注解来注解getValues。还要尽量避免在将要在 Jenkins 沙箱中执行的代码中使用 Streams,这通常是导致错误的原因。希望对您有所帮助。
关于java - Groovy 和 StreamAPI 出现意外的 GroovyCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48348285/
我有课 public class CenterReportDto { ZonedDateTime date; int centerId; int amount;
我在构建期间执行的 Jenkinsfile 中运行以下 groovy 脚本: import java.util.stream.Collectors import java.util.stream.St
List entities = ... Map> entitiesByIsTest = entities.stream() .collect(Colle
我是一名优秀的程序员,十分优秀!