Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins groovy classpath issue - unable to resolve class

Tags:

jenkins

groovy

I have an 'Execute Groovy script' build step in Jenkins. This step consists of two files - a client file called createWorkspaces.groovy and a bean file called WorkspaceBean.groovy. Both live in the same location in the job workspace.

Previously running Jenkins 1.554 this ran without issues, but after upgrading to 1.594 I am getting the following error:

/jenkins/workspace/testjob/scripts/groovy/createWorkspaces.groovy: 75: unable to resolve class WorkspaceBean 
 @ line 75, column 21.
       def workspace = new WorkspaceBean()
                       ^

1 error

I have approved the scripts in the new script approval function and I have also added the location of the files to the class path parameter in the job step as well as the location of the jenkins-core.jar file.

Any ideas why this has stopped working?

like image 833
JamesE Avatar asked Jan 20 '15 14:01

JamesE


People also ask

Is Groovy necessary for Jenkins?

Groovy scripts are not necessarily suitable for all users, so Jenkins created the Declarative pipeline. The Declarative Pipeline syntax is more stringent. It needs to use Jenkins' predefined DSL structure, which provides a simpler and more assertive syntax for writing Jenkins pipelines.

Why Groovy script is used in Jenkins?

Groovy is a very powerful language which offers the ability to do practically anything Java can do including: Create sub-processes and execute arbitrary commands on the Jenkins controller and agents. It can even read files in which the Jenkins controller has access to on the host (like /etc/passwd )

How does Groovy work in Jenkins?

Within a Pipeline Project (read plugin), Jenkins introduces a domain-specific language (DSL) based on 'Groovy', which can be used to define a new pipeline as a script. The flow that would typically require many “standard” Jenkins jobs chained together, can be expressed as a single script.


2 Answers

This appears to be a bug in the groovy plugin. Adding paths to the Class path field within the plugin configuration does not change the class path.

This does not work:

Adding here does not work

Adding a CLASSPATH variable via the 'Inject environment variables into the build process' plugin does work.

This works:

enter image description here

like image 52
JamesE Avatar answered Nov 02 '22 15:11

JamesE


Try to load your jars dynamically. This is the final working solution I found. This sample is for copy network folder to local machine.

def file = new File("jcifs-1.3.18.jar")
this.class.classLoader.rootLoader.addURL(file.toURI().toURL())


def auth_server = Class.forName("jcifs.smb.NtlmPasswordAuthentication").newInstance("domain", "username", "password")
def auth_local = Class.forName("jcifs.smb.NtlmPasswordAuthentication").newInstance(null, "local_user", "password")

def source_url = args[0]
def dest_url = args[1]
def auth = auth_server

//prepare source file
if(!source_url.startsWith("\\\\"))
{
  source_url = "\\\\localhost\\"+ source_url.substring(0, 1) + "\$" + source_url.substring(1, source_url.length());
  auth = auth_local  
}
source_url = "smb:"+source_url.replace("\\","/");
def source = Class.forName("jcifs.smb.SmbFile").newInstance(source_url,auth_server)

//prepare destination file
if(!dest_url.startsWith("\\\\"))
{
  dest_url = "\\\\localhost\\"+ dest_url.substring(0, 1) + "\$" +dest_url.substring(2, dest_url.length());
  auth = auth_local  
}
dest_url = "smb:"+dest_url.replace("\\","/");
def dest = Class.forName("jcifs.smb.SmbFile").newInstance(dest_url,auth_local)
source.copyTo(dest)
like image 1
Nayana Adassuriya Avatar answered Nov 02 '22 15:11

Nayana Adassuriya