【问题标题】:How to access to a static file in resources folder of the shared library from within a class in src directory如何从 src 目录中的类中访问共享库资源文件夹中的静态文件
【发布时间】: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).
.....
....

【问题讨论】:

标签: jenkins groovy filepath jenkins-shared-libraries


【解决方案1】:

我猜你是从vars/whateverPipelineFile.groovy 调用你的文件src/com/rathath/jenkins/Configuration.groovy 所以,在调用它时,确保你传递了管道上下文,并且从你的类中你将能够使用context.libraryResource()

示例:

Configuration.groovy

    class Configuration {
        def context


        Configuration(pipelineContext) {
            this.context = pipelineContext
        }

        def readConfig() {
            this.context.libraryResource("${PATH_OF_YOUR_FILE}")
            ...
        }
}  

/vars/myPipeline.groovy

   import com.rathath.jenkins.configuration.Configuration

   def call() {
      def configuration = new Configuration(this)
      configuration.readConfig()
      ...
   }

libraryResource() here的文档

【讨论】:

  • 不!我在初始化阶段调用它。在使用 vars/ 下的任何步骤之前,Map 对象(反映 yaml)必须可用作文字 Map 对象
  • 创建一个新的第一步来进行此初始化是否对您的用例有效?我知道这并不理想,但我认为它会解除对您的阻止。
【解决方案2】:

让它工作并不容易。但我做到了:

src/com/rathath/jenkins/Configuration.groovy

@Grab(group='org.yaml', module='snakeyaml', version='1.17')
import org.yaml.snakeyaml.Yaml
import java.io.File
import groovy.transform.SourceURI
import java.nio.file.Path
import java.nio.file.Paths
import static groovy.io.FileType.*

class Configuration {

  Map config = [:]
  
  @SourceURI
  URI sourceUri

  Configuration() {
    new File(getRootLocation(), 'resources')
      .eachFileMatch(FILES, ~/.*\.yaml/) {
       this.config << yaml.load(it.text)
      }
  }
  @NonCPS
  File getRootLocation() {
    return Paths.get(sourceUri)
            .getParent() // jenkins
            .getParent() // rathath
            .getParent() // com
            .getParent() // src
            .getParent() // .
            .toFile()

  }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-21
    • 2012-05-25
    • 2015-07-08
    • 2020-02-20
    • 2017-10-03
    • 1970-01-01
    • 2012-06-16
    相关资源
    最近更新 更多