Jenkins共享库-从/vars中的/src文件夹导入类 [英] Jenkins Shared Library - Importing classes from the /src folder in /vars
问题描述
我正在尝试为我的CI流程编写Jenkins共享库.我想引用\vars
文件夹中定义的全局函数内\src
文件夹中的类,因为它可以让我将大多数逻辑放在类中,而不是在全局函数中.我正在遵循Jenkins官方文档中记录的存储库结构:
Jenkins共享库结构
I am trying to writing a Jenkins Shared Library for my CI process. I'd like to reference a class that is in the \src
folder inside a global function defined in the \vars
folder, since it would allow me to put most of the logic in classes instead of in the global functions. I am following the repository structure documented on the official Jenkins documentation:
Jenkins Shared Library structure
这是我所拥有的简化示例:
Here's a simplified example of what I have:
/src/com/example/SrcClass.groovy
/src/com/example/SrcClass.groovy
package com.example
class SrcClass {
def aFunction() {
return "Hello from src folder!"
}
}
/vars/classFromVars.groovy
/vars/classFromVars.groovy
import com.example.SrcClass
def call(args) {
def sc = new SrcClass()
return sc.aFunction()
}
Jenkinsfile
Jenkinsfile
@Library('<lib-name>') _
pipeline {
...
post {
always {
classFromVars()
}
}
}
我的目标是让/vars
文件夹中的全局类充当一种公共外观,并在我的Jenkinsfile
中将其用作自定义步骤,而不必在script
块中实例化类(使其与声明性管道兼容).这一切对我来说似乎很简单,但是在运行classFromVars
文件时出现此错误:
My goal was for the global classes in the /vars
folder to act as a sort of public facade and to use it in my Jenkinsfile
as a custom step without having to instantiate a class in a script
block (making it compatible with declarative pipelines). It all seems pretty straightforward to me, but I am getting this error when running the classFromVars
file:
<root>\vars\classFromVars.groovy: 1: unable to resolve class com.example.SrcClass
@ line 1, column 1.
import com.example.SrcClass
^
1 error
我尝试直接在本地和Jenkins服务器上使用groovy
CLI运行classFromVars
类,并且在两种环境下我都遇到相同的错误.我还尝试在运行/vars
脚本时指定类路径,并使用以下命令得到相同的错误:
I tried running the classFromVars
class directly with the groovy
CLI locally and on the Jenkins server and I have the same error on both environments. I also tried specifying the classpath when running the /vars
script, getting the same error, with the following command:
<root>>groovy -cp <root>\src\com\example vars\classFromVars.groovy
我要实现的目标是可能的吗?还是我应该将所有逻辑都放在/vars
类中,并避免使用/src
文件夹?
Is what I'm trying to achieve possible? Or should I simply put all of my logic in the /vars
class and avoid using the /src
folder?
我在GitHub上发现了几个存储库,这些存储库似乎表明这是可能的,例如,这一个存储库: https://github.com/fabric8io/fabric8-pipeline-library ,它在/vars
文件夹中的许多类中使用/src
文件夹中的类.
I have found several repositories on GitHub that seem to indicate this is possible, for example this one: https://github.com/fabric8io/fabric8-pipeline-library, which uses the classes in the /src
folder in many of the classes in the /vars
folder.
推荐答案
正如@Szymon Stepniak指出的那样,我的groovy
命令中的-cp
参数不正确.现在,它可以在本地和Jenkins服务器上工作.我尚未解释为什么它不能在Jenkins服务器上运行.
As @Szymon Stepniak pointed out, the -cp
parameter in my groovy
command was incorrect. It now works locally and on the Jenkins server. I have yet to explain why it wasn't working on the Jenkins server though.
这篇关于Jenkins共享库-从/vars中的/src文件夹导入类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!