Jenkins 共享库 - 从 /vars 中的 /src 文件夹导入 类
Jenkins Shared Library - Importing classes from the /src folder in /vars
我正在尝试为我的 CI 进程编写一个 Jenkins 共享库。我想在 \vars
文件夹中定义的全局函数中引用 \src
文件夹中的 class,因为它允许我将大部分逻辑放在 classes 而不是在全局函数中。我遵循 Jenkins 官方文档中记录的存储库结构:
Jenkins Shared Library structure
这是我所拥有的一个简化示例:
/src/com/example/SrcClass.groovy
package com.example
class SrcClass {
def aFunction() {
return "Hello from src folder!"
}
}
/vars/classFromVars.groovy
import com.example.SrcClass
def call(args) {
def sc = new SrcClass()
return sc.aFunction()
}
詹金斯文件
@Library('<lib-name>') _
pipeline {
...
post {
always {
classFromVars()
}
}
}
我的目标是让 /vars
文件夹中的全局 classes 充当一种 public 门面,并在我的 Jenkinsfile
中将其用作无需在 script
块中实例化 class 的自定义步骤(使其与声明性管道兼容)。这对我来说似乎很简单,但是当 运行 classFromVars
文件时出现此错误:
<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
class,我在两种环境中都遇到了相同的错误。我还尝试在 运行 脚本 /vars
时指定 classpath,得到同样的错误,使用以下命令:
<root>>groovy -cp <root>\src\com\example vars\classFromVars.groovy
我想要实现的目标可行吗?或者我应该简单地将所有逻辑放在 /vars
class 中并避免使用 /src
文件夹?
我在 GitHub 上发现了几个似乎表明这是可能的存储库,例如这个:https://github.com/fabric8io/fabric8-pipeline-library,它使用 /src
中的 classes ] 文件夹中的许多 classes 在 /vars
文件夹中。
正如@Szymon Stepniak 所指出的,我的 groovy
命令中的 -cp
参数不正确。它现在可以在本地和 Jenkins 服务器上运行。我还没有解释为什么它不能在 Jenkins 服务器上运行。
我发现当我想从我拥有的共享库中导入 class 到 /vars
中的脚本步骤时,我需要这样做:
//thanks to '_', the classes are imported automatically.
// MUST have the '@' at the beginning, other wise it will not work.
// when not using "@BRANCH" it will use default branch from git repo.
@Library('my-shared-library@BRANCH') _
// only by calling them you can tell if they exist or not.
def exampleObject = new example.GlobalVars()
// then call methods or attributes from the class.
exampleObject.runExample()
我正在尝试为我的 CI 进程编写一个 Jenkins 共享库。我想在 \vars
文件夹中定义的全局函数中引用 \src
文件夹中的 class,因为它允许我将大部分逻辑放在 classes 而不是在全局函数中。我遵循 Jenkins 官方文档中记录的存储库结构:
Jenkins Shared Library structure
这是我所拥有的一个简化示例:
/src/com/example/SrcClass.groovy
package com.example
class SrcClass {
def aFunction() {
return "Hello from src folder!"
}
}
/vars/classFromVars.groovy
import com.example.SrcClass
def call(args) {
def sc = new SrcClass()
return sc.aFunction()
}
詹金斯文件
@Library('<lib-name>') _
pipeline {
...
post {
always {
classFromVars()
}
}
}
我的目标是让 /vars
文件夹中的全局 classes 充当一种 public 门面,并在我的 Jenkinsfile
中将其用作无需在 script
块中实例化 class 的自定义步骤(使其与声明性管道兼容)。这对我来说似乎很简单,但是当 运行 classFromVars
文件时出现此错误:
<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
class,我在两种环境中都遇到了相同的错误。我还尝试在 运行 脚本 /vars
时指定 classpath,得到同样的错误,使用以下命令:
<root>>groovy -cp <root>\src\com\example vars\classFromVars.groovy
我想要实现的目标可行吗?或者我应该简单地将所有逻辑放在 /vars
class 中并避免使用 /src
文件夹?
我在 GitHub 上发现了几个似乎表明这是可能的存储库,例如这个:https://github.com/fabric8io/fabric8-pipeline-library,它使用 /src
中的 classes ] 文件夹中的许多 classes 在 /vars
文件夹中。
正如@Szymon Stepniak 所指出的,我的 groovy
命令中的 -cp
参数不正确。它现在可以在本地和 Jenkins 服务器上运行。我还没有解释为什么它不能在 Jenkins 服务器上运行。
我发现当我想从我拥有的共享库中导入 class 到 /vars
中的脚本步骤时,我需要这样做:
//thanks to '_', the classes are imported automatically.
// MUST have the '@' at the beginning, other wise it will not work.
// when not using "@BRANCH" it will use default branch from git repo.
@Library('my-shared-library@BRANCH') _
// only by calling them you can tell if they exist or not.
def exampleObject = new example.GlobalVars()
// then call methods or attributes from the class.
exampleObject.runExample()