【发布时间】:2020-07-20 03:28:32
【问题描述】:
我的 Jenkins 共享库具有以下结构:
resources
|-> config.yaml
|-> projects.yaml
src
|_ com
|_ rathath
|_ jenkins
|-> Configuration.groovy
在 src/com/rathath/jenkins/Configuration.groovy 中,我想读取资源目录下的 YAML 文件。
我试过了:
@Grab('org.yaml:snakeyaml:1.17')
import org.yaml.snakeyaml.Yaml
import hudson.FilePath
// ...
def readConfig() {
def config = [:]
def cwd = hudson.model.Executor.currentExecutor().getCurrentWorkspace().absolutize()
new FilePath(cwd, 'resources').list('*.yaml').each {
config << it.read()
}
}
不幸的是,我得到的hudson.model.Executor.currentExecutor() 为空。
我尝试了另一种方法:
@Grab('org.yaml:snakeyaml:1.17')
import org.yaml.snakeyaml.Yaml
import hudson.FilePath
import groovy.transform.SourceURI
import java.nio.file.Paths
// ...
@SourceURI
URI source Uri
def readConfig() {
def config = [:]
def cwd = new FilePath(Paths.get(sourceUri).getParent().getParent().getParent().getParent().getParent().getParent().toFile());
new FilePath(cwd, 'resources').list('*.yaml').each {
config << it.read()
}
}
我遇到了更大的问题,.. Jenkins 无法加载文件:
java.lang.NoClassDefFoundError: Could not initialize class com.rathath.jenkins.Configuration
at java.io.ObjectStreamClass.hasStaticInitializer(Native Method).
.....
....
【问题讨论】:
-
这能回答你的问题吗? How to load files from resources folder in Shared library without knowing their names (or number)? -- 赞成的答案对我有用。
-
我的问题没有重复。在问题的标题中,我说“从 src 目录中的一个类中”。看起来我的问题的答案不同,.. 是从 vars 目录中回答的。
-
我相信该解决方案可以应用于您的问题,只需稍作调整。
标签: jenkins groovy filepath jenkins-shared-libraries