[英]Jenkins Shared Library - Importing classes from the /src folder in /vars
I am trying to writing a Jenkins Shared Library for my CI process.我正在尝试为我的 CI 流程编写 Jenkins 共享库。 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.我想引用一个 class,它位于\vars
文件夹中定义的全局 function 内的\src
文件夹中,因为它允许我将大部分逻辑放在类中而不是全局函数中。 I am following the repository structure documented on the official Jenkins documentation: Jenkins Shared Library structure我正在关注官方 Jenkins 文档中记录的存储库结构: Jenkins 共享库结构
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詹金斯文件
@Library('<lib-name>') _
pipeline {
...
post {
always {
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).我的目标是让/vars
文件夹中的全局类充当一种公共外观,并在我的Jenkinsfile
其用作自定义步骤,而无需在script
块中实例化 class(使其与声明性管道兼容)。 It all seems pretty straightforward to me, but I am getting this error when running the classFromVars
file:这对我来说似乎很简单,但是在运行classFromVars
文件时出现此错误:
<root>\vars\classFromVars.groovy: 1: unable to resolve class com.example.SrcClass
@ line 1, column 1.
import com.example.SrcClass
^
1 error
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.我尝试在本地和 Jenkins 服务器上直接使用groovy
CLI 运行classFromVars
class 并且我在两个环境中都有相同的错误。 I also tried specifying the classpath when running the /vars
script, getting the same error, with the following command:我还尝试在运行/vars
脚本时指定类路径,得到相同的错误,使用以下命令:
<root>>groovy -cp <root>\src\com\example vars\classFromVars.groovy
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?或者我应该简单地将所有逻辑放在/vars
class 中并避免使用/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.我在 GitHub 上找到了几个存储库,它们似乎表明这是可能的,例如这个: https://github.com/fabric8io/fabric8-pipeline-library ,它在许多类中使用/src
文件夹中的类在/vars
文件夹中。
As @Szymon Stepniak pointed out, the -cp
parameter in my groovy
command was incorrect.正如@Szymon Stepniak 指出的那样,我的groovy
命令中的-cp
参数不正确。 It now works locally and on the Jenkins server.它现在可以在本地和 Jenkins 服务器上运行。 I have yet to explain why it wasn't working on the Jenkins server though.我还没有解释为什么它不能在 Jenkins 服务器上运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.