I'm writing Jenkins pipeline code and have two similar Groovy files with class definitions inside them. When I run a job using on Groovy file, the job runs fine. When I run a similar job using the other Groovy file I get an error.
Please let me explain.
I have the following file structure
com/company/versionmgrs/
/VersionDirector.groovy
/VersionBuilder.groovy
/PlatformVersionBuilder.groovy
Here are the contents of the above Groovy files, and the corresponding `Jenkinsfile`s that instatiates them. As you can see, they're pretty similar. Nothing magical, in all of them, the class name matches the Groovy file name.
#VersionDirector.groovy
package com.company.versionmgrs
class VersionDirector {
VersionDirector(def file) {
this.file = file
}
def file
}
# Jenkinsfile
@Library("shared-lib) _
import com.company.versionmgrs.VersionDirector
node("build-node") {
def vd = new VersionDirector()
}
#VersionBuilder.groovy
package com.company.versionmgrs
abstract class VersionBuilder {
VersionBuilder(def file) {
this.file = file
}
def file
}
#PlatformVersionBuilder.groovy
package com.company.versionmgrs
class PlatformVersionBuilder extends VersionBuilder {
PlatformVersionBuilder(def file) {
this.file = file
}
def file
}
# Jenkinsfile
@Library("shared-lib) _
import com.company.versionmgrs.PlatformVersionBuilder
node("build-node") {
def builder = new PlatformVersionBuilder()
}
When I run the first Jenkinsfile that instantiates VersionDirector, I get no error. But when I run the second Jenkinsfile that instantiates PlatformVersionBuilder, I get the following error
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
/var/jenkins_home/jobs/.../PlatformVersionBuilder.groovy: 3: Invalid duplicate class definition of class com.company.versionmgrs.PlatformVersionBuilder : The source /var/jenkins_home/jobs/...PlatformVersionBuilder.groovy contains at least two definitions of the class com.company.versionmgrs.PlatformVersionBuilder.
One of the classes is an explicit generated class using the class statement, the other is a class generated from the script body based on the file name. Solutions are to change the file name or to change the class name.
@ line 3, column 1.
class PlatformVersionBuilder extends VersionBuilder {
^
I expect one job to pass, or fail, like the other. What am I missing? TIA