Unable to resolve class for Jenkins plugin during build

I am fairly new to Gradle so I am almost certainly doing something really silly but I have been trying to solve this for several hours now and I am stuck

I have a Jenkins shared library and I am trying to add tests to it but when I run my tests I get an error with the class path

❯ ./gradlew build test

> Task :compileGroovy FAILED
startup failed:
/Users/stephen.ball/Desktop/foo/vars/foo.groovy: 1: unable to resolve class org.jenkinsci.plugins.pipeline.modeldefinition.Utils
 @ line 1, column 1.
   import org.jenkinsci.plugins.pipeline.modeldefinition.Utils
   ^

1 error

my vars/foo.groovy is the following (obviously this is an example)

import org.jenkinsci.plugins.pipeline.modeldefinition.Utils

def call(String skip) {
    if (skip == 'blah') {
        Utils.markStageSkippedForConditional(skip)
    }
    
    return 'Yay!'
}

if I just have a basic function which just returns a String without importing the Utils it works as I would expect

My build settings contain

dependencies {
    implementation 'org.codehaus.groovy:groovy-all:3.0.22'
    testImplementation 'junit:junit:4.13.2'

    implementation "org.jenkins-ci.main:jenkins-core:2.462.2"

    implementation 'org.jenkinsci.plugins:pipeline-model-definition:2.2214.vb_b_34b_2ea_9b_83'
}

so I would expect it to be available and if I run ./gradlew help dependencies it shows in testRuntimeClasspath - Runtime classpath of source set 'test'.

I thought maybe it needed to be testImplementation but that didn’t make any difference

I have been searching for solutions but all the posts I have found are over 5 years old so talk about much older formats of the setting files do didn’t really help

I pushed an example to here to make it a bit clearer

Any help would be much appreciated

Thanks

org.jenkinsci.plugins:pipeline-model-definition:2.2214.vb_b_34b_2ea_9b_83 has packing set to hpi, so you do not get a .jar file, but an .hpi file.
This .hpi file is something Jenkins-specific and has the actual jar file inside.
So you either need an artifact transform that unpacks the jar file from the hpi files, or - as the jar is also published separately - need to request it, for example using

implementation('org.jenkinsci.plugins:pipeline-model-definition:2.2214.vb_b_34b_2ea_9b_83') {
    artifact {
        extension = 'jar'
    }
}

Btw. I strongly recommend switching to Kotlin DSL. By now it is the default DSL, you immediately get type-safe build scripts, actually helpful error messages if you mess up the syntax, and amazingly better IDE support if you use a good IDE like IntelliJ IDEA or Android Studio.

Amazing, thanks, that fixed it. I will take a look at the Kotlin DSL, thanks

1 Like