简体   繁体   English

如何代码重用 Jenkins 共享库?

[英]How to code-reuse a Jenkins shared library?

I'm using a Jenkins shared library , and have files that contain functions and classes defined like this我正在使用Jenkins 共享库,并且文件包含这样定义的函数和类

src/com/company/someDir/SomeFile.groovy
----------------------------------------
package com.company.someDir
class SomeClass {
  ...
}

How can I declare a variable of type SomeClass in the SomeOtherClass definition?如何在SomeOtherClass定义中声明SomeClass类型的变量? I tried these...我试过这些...

src/com/company/someDir/SomeOtherFile.groovy
--------------------------------------------
package com.company.someDir
import com.company.someDir.SomeFile
class SomeOtherClass {
  SomeClass aClass=null // ...or...
  com.company.someDir.SomeFile.SomeClass aClass=null
  ...
}

...but I get "Unable to resolve class" compilation error in both cases in my Jenkins pipeline job. ...但是在我的 Jenkins 管道作业中,两种情况下都出现“无法解析类”编译错误。 What's the correct way?正确的方法是什么?

EDIT: Per comments below编辑:根据下面的评论

NOTE: I have a Jenkins pipeline job, and the inline pipeline code is simply注意:我有一个 Jenkins 管道作业,内联管道代码很简单

node("build-node") {
  // Shared library set up in Jenkins system config
  @Library("shared-library")
  def object = new com.company.someDir.SomeOtherFile() 
  object.somePublicFunc()
}

When I run the job I get the error.当我运行作业时,我得到了错误。

Within shared library and within same package you don't need to import anything.在共享库和同一个 package 中,您不需要导入任何内容。 Inside one package it's transparent.在一个 package 里面它是透明的。 Please remove all imports from packages.请从包中删除所有导入。

eg例如

src/com/company/someDir/SomeOtherFile.groovy
--------------------------------------------
package com.company.someDir

class SomeOtherClass {
  SomeClass aClass=null // ...or...
  com.company.someDir.SomeFile.SomeClass aClass=null
  ...
}

Inside pipeline you have import classes.在管道内部,您有导入类。

eg例如

// this must be first before import
@Library("shared-library") _    

// after that pipeline knows about SomeOtherFile class
import com.company.someDir.SomeOtherFile

node("build-node") {
  
  def object = new SomeOtherFile() 
  object.somePublicFunc()
}

Good luck祝你好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM