Jenkins groovy classpath问题 - 无法解析类

时间:2015-01-20 14:11:25

标签: groovy jenkins

我在Jenkins中有一个'Execute Groovy script'构建步骤。此步骤包含两个文件 - 名为createWorkspaces.groovy的客户端文件和名为WorkspaceBean.groovy的Bean文件。两者都位于作业工作区的相同位置。

以前运行Jenkins 1.554这没有问题,但升级到1.594后我收到以下错误:

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

1 error

我已批准新脚本审批功能中的脚本,并且还将文件的位置添加到作业步骤中的类路径参数以及jenkins-core.jar文件的位置。

为什么会停止工作的任何想法?

2 个答案:

答案 0 :(得分:8)

这似乎是groovy插件中的一个错误。在插件配置中添加类路径字段的路径不会更改类路径。

这不起作用:

Adding here does not work

通过“注入”环境变量将CLASSPATH变量添加到构建过程中'插件确实有用。

这有效:

enter image description here

答案 1 :(得分:1)

尝试动态加载您的jar。这是我发现的最终工作解决方案。此示例用于将网络文件夹复制到本地计算机。

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)