问题
When using the Groovy Jenkins plugin (not the Groovy Post Build Plugin, which is a different thing) as a Post Step, I can't resolve classes in the hudson.model
package.
Do I need to add the Jenkins .war onto the classpath, or should these packages already be there?
Script:
import hudson.model.*;
import hudson.util.*;
AbstractBuild currentBuild = (AbstractBuild) Thread.currentThread().executable;
def mavenVer = currentBuild.getMavenArtifacts().getModuleRecords()[0].mainArtifact.version;
println mavenVer;
ParametersAction newParamAction = new hudson.model.ParametersAction(new hudson.model.StringParameterValue(“MAVEN_VERSION”, mavenVer));
currentBuild.addAction(newParamAction);
Output:
[Common] $ /home/tester/tools/Groovy_1.8.3/bin/groovy /home/tester/workspace/Common/hudson8369102960709507246.groovy
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
/home/tester/workspace/Common/hudson8369102960709507246.groovy: 8: unable to resolve class AbstractBuild
@ line 8, column 15.
AbstractBuild currentBuild = (AbstractBuild) Thread.currentThread().executable;
[...]
回答1:
Yes, if it's a Groovy Script
you do need to add the jenkins-core
jar to your classpath, and the stapler jar too.
If it's a Groovy System Script
then it is run inside the master's JVM, meaning all the Jenkins classes and dependencies are already on the classpath.
来源:https://stackoverflow.com/questions/9996315/jenkins-packages-on-groovy-classpath