title: Jenkins Shared Library - Importing classes from the /src folder in /vars
body: <p>I am trying to writing a Jenkins Shared Library for my CI process. I'd like to reference a class that is in the <code>\src</code> folder inside a global function defined in the <code>\vars</code> folder, since it would allow me to put most of the logic in classes instead of in the global functions. I am following the repository structure documented on the official Jenkins documentation:
  <a href="https://i.stack.imgur.com/aioVZ.png" rel="nofollow noreferrer">Jenkins Shared Library structure</a></p>
  
  <p>Here's a simplified example of what I have:</p>
  
  <hr>
  
  <p>/src/com/example/SrcClass.groovy</p>
  
  <pre><code>package com.example
  
  class SrcClass {
    def aFunction() {
      return "Hello from src folder!"
    }
  }
  </code></pre>
  
  <hr>
  
  <p>/vars/classFromVars.groovy</p>
  
  <pre><code>import com.example.SrcClass
  
  def call(args) {
    def sc = new SrcClass()
    return sc.aFunction()
  }
  </code></pre>
  
  <hr>
  
  <p>Jenkinsfile</p>
  
  <pre><code>@Library('&lt;lib-name&gt;') _
  pipeline {
    ...
    post {
      always {
        classFromVars()
      }
    }
  }
  </code></pre>
  
  <p>My goal was for the global classes in the <code>/vars</code> folder to act as a sort of public facade and to use it in my <code>Jenkinsfile</code> as a custom step without having to instantiate a class in a <code>script</code> block (making it compatible with declarative pipelines). It all seems pretty straightforward to me, but I am getting this error when running the <code>classFromVars</code> file:</p>
  
  <pre><code>&lt;root&gt;\vars\classFromVars.groovy: 1: unable to resolve class com.example.SrcClass
   @ line 1, column 1.
     import com.example.SrcClass
     ^
  
  1 error
  </code></pre>
  
  <p>I tried running the <code>classFromVars</code> class directly with the <code>groovy</code> CLI locally and on the Jenkins server and I have the same error on both environments. I also tried specifying the classpath when running the <code>/vars</code> script, getting the same error, with the following command:</p>
  
  <pre><code>&lt;root&gt;&gt;groovy -cp &lt;root&gt;\src\com\example vars\classFromVars.groovy
  </code></pre>
  
  <p>Is what I'm trying to achieve possible? Or should I simply put all of my logic in the <code>/vars</code> class and avoid using the <code>/src</code> folder?</p>
  
  <p>I have found several repositories on GitHub that seem to indicate this is possible, for example this one: <a href="https://github.com/fabric8io/fabric8-pipeline-library" rel="nofollow noreferrer">https://github.com/fabric8io/fabric8-pipeline-library</a>, which uses the classes in the <code>/src</code> folder in many of the classes in the <code>/vars</code> folder.</p>