簡體   English   中英

如何代碼重用 Jenkins 共享庫?

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

我正在使用Jenkins 共享庫,並且文件包含這樣定義的函數和類

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

如何在SomeOtherClass定義中聲明SomeClass類型的變量? 我試過這些...

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
  ...
}

...但是在我的 Jenkins 管道作業中,兩種情況下都出現“無法解析類”編譯錯誤。 正確的方法是什么?

編輯:根據下面的評論

注意:我有一個 Jenkins 管道作業,內聯管道代碼很簡單

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

當我運行作業時,我得到了錯誤。

在共享庫和同一個 package 中,您不需要導入任何內容。 在一個 package 里面它是透明的。 請從包中刪除所有導入。

例如

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

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

在管道內部,您有導入類。

例如

// 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()
}

祝你好運

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM