修改
我正在尝试在当前构建目录中构建一个文件。采用构建环境变量,我创建了一个字符串,并尝试使用它创建一个新文件。但是,当我这样做时,我得到错误“没有这样的属性”
String jobDir = "C:\\Program Files (x86)\\Jenkins\\jobs\\${env.JOB_NAME}\\builds\\${env.BUILD_NUMBER}\\"
String FILEDIR = jobDir+"SOTR.log"
@Field File outFile = new File(FILEDIR)
我收到此错误:
groovy.lang.GroovyRuntimeException: Failed to create Script instance for class: class WorkflowScript. Reason: groovy.lang.MissingPropertyException: No such property: FILEDIR for class: WorkflowScript
at org.codehaus.groovy.runtime.InvokerHelper.createScript(InvokerHelper.java:466)
at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:67)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:430)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:393)
at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:238)
at hudson.model.ResourceController.execute(ResourceController.java:97)
at hudson.model.Executor.run(Executor.java:405)
Caused by: groovy.lang.MissingPropertyException: No such property: FILEDIR for class: WorkflowScript
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)
at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:52)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:307)
at WorkflowScript.<init>(WorkflowScript:15)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at org.codehaus.groovy.runtime.InvokerHelper.createScript(InvokerHelper.java:434)
... 7 more
Finished: FAILURE
答案 0 :(得分:2)
发生此错误是因为您已使用变量@Field
的{{1}}注释 - 当您这样做时,它会将此变量从本地范围移动到脚本类范围。并且因为您引用了本地范围变量outFile
,所以它会抛出FILEDIR
,因为类级别没有这样的字段。如果您打算这样做,请删除groovy.lang.MissingPropertyException
注释或将其添加到所有其他变量。
您也可以将@Field
变量构建为:
jobDir
另请注意,默认情况下,Jenkins不允许使用String jobDir = "${env.JENKINS_HOME}\\jobs\\${env.JOB_NAME}\\builds\\${env.BUILD_NUMBER}"
类,如果您不允许服务器这样做,您将获得如下异常:
java.io.File