简体   繁体   中英

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

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? 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. What's the correct way?

EDIT: Per comments below

NOTE: I have a Jenkins pipeline job, and the inline pipeline code is simply

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. Inside one package it's transparent. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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