Issue
This Content is from Stack Overflow. Question asked by gek
Here in the @Field annotation documentation written that:
The annotated variable will become a private field of the script class.
But this is not working in Jenkins:
If i have two scripts, one of them loaded another one, @Field annotated variable becomes global:
/* test.groovy */
import groovy.transform.Field
@Field def scriptPrivateVar
def main() {
node('main') {
cleanWs()
checkout scm
scriptPrivateVar = 'a'
def module = load("${pwd()}/module.groovy")
module.callPrivate()
}
}
main()
module:
/* module.groovy */
def callPrivate() {
echo "private var: ${this.scriptPrivateVar}"
}
return this
And the output is:
20:59:32 private var: a
Am i understand correctly that the only way to make scriptPrivateVar global in scope of on only test.groovy script -> define it as global without @Field? Any options?
Solution
This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.
This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.