我有以下共享库结构
com/company/test/
/ServiceBuilder.groovy
/MonoBuilder.groovy
/MicroServiceBuilder.groovy
然后在src/com/test/builders
中,我有这3个文件
#ServiceBuilder.groovy
package com.company.test
abstract class ServiceBuilder implements Serializable {
protected String msg = null
// Used by all sub-classes
protected def printMsg() {
println "${this.msg}"
}
// Implement in sub-classes
protected def setMsg(final String msg) {
}
}
#MicroserviceBuilder.groovy
package com.company.test
class MicroserviceBuilder extends ServiceBuilder {
MicroserviceBuilder(){
super()
}
def setMsg(final String msg) {
println this.getClass()
this.msg = "Mirco: $msg"
}
}
#MonoServiceBuilder
package com.company.test
class MonoBuilder extends ServiceBuilder {
MonoBuilder(){
super()
}
def setMsg(final String msg) {
println this.getClass()
this.msg = "Mono: $msg"
}
}
下面是使用上述共享库的Jenkinsfile cat
@Library("my-shared-lib@feature/test-shared-lib") _
import com.company.test.ServiceBuilder
import com.company.test.MonoBuilder
import com.company.test.MicroserviceBuilder
node("linux-ubuntu") {
stage("Mono"){
ServiceBuilder builder = new MonoBuilder()
builder.setMsg("hello")
builder.printMsg()
}
stage("Micro"){
ServiceBuilder builder = new MicroserviceBuilder()
builder.setMsg("hola")
builder.printMsg()
}
}
代码执行了,但我没有看到任何预期的println
输出。我以为
- 调用
object.setMsg(...)
时打印的类类型 Mono: hello
用于MonoService.printMsg()
调用Micro: hola
用于Microservice.printMsg()
调用
但在Jenkins控制台中
Running on linux-ubuntu-1 in /home/jenkins/workspace/Utilities/Playground/test-shared-lib
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Mono)
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Micro)
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
我错过了什么?TIA
1条答案
按热度按时间g6baxovj1#
我通过查看Why do I love Jenkins shared libraries?来使它工作。下面是我修改过的3个Groovy文件,我在其中添加了一个
jfile
属性,它是调用Jenkinsfile
的this
指针。这是
Jenkinsfile
Jenkins控制台输出现在显示
这是预期的输出。但是为什么我必须这样做,使用调用者的
println
,而不是能够在类本身中这样做呢?