简体   繁体   English

Jenkins 共享库 - 从 /vars 中的 /src 文件夹导入类

[英]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.

相关问题 Jenkins共享库:/ vars文件夹和/ src文件夹中的函数有什么区别? - Jenkins shared library: what is the difference between functions in /vars folder and /src folders? 在Jenkins Pipeline中,从vars文件夹中的共享库返回一个值(映射或列表) - In Jenkins Pipeline return a value ( map or list ) from shared library in vars folder 使用共享的\\ vars库在Jenkins中定义FOLDER级别的变量 - Defining FOLDER level variables in Jenkins using a shared \vars library jenkins管道:无法将构建参数传递给共享库变量 - jenkins pipeline: can't pass build parameters to shared library vars 访问jenkins共享库类中的插件 - Accessing plugins in jenkins shared library classes Jenkins 共享库使用 `src/**` 下的 groovy 文件动态加载 - Jenkins shared library loading dynamically with groovy file under `src/**` Jenkins 共享库 src class - 无法解析 class - Jenkins Shared Library src class - unable to resolve class Jenkins 管道共享库无法调用 src 目录中的方法 - Jenkins pipeline shared library can't invoke method in src directory 如何从 src 目录中的 class 访问共享库资源文件夹中的 static 文件 - How to access to a static file in resources folder of the shared library from within a class in src directory 使用Jenkins共享库,从vars /?中的文件中导入Class。 - Using Jenkins Shared Libraries, import Class from file within vars/?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM