6

I am trying to create a Jenkins Pipeline Script using groovy. However, the import statement is giving me a compilation error - Unknown Type : Import. Not sure why.

Screenshot attached here

Szymon Stepniak
  • 40,216
  • 10
  • 104
  • 131
Bipin
  • 63
  • 1
  • 3

1 Answers1

10

You should define import jxl.* at the top of the pipeline script, e.g.

import jxl.*

node {
    stage('Execute Tests') {
        try {
           dir('.') {
               sh '......' // etc.
           }
        }
    }
}

When you added it inside node {} block Jenkins was looking for a method instead of import class statement. The good convention is to put all import statements at the top of the Groovy file.

Szymon Stepniak
  • 40,216
  • 10
  • 104
  • 131
  • If you are looking to do this for a pipeline library, where there are multiple .groovy files, you can do so just above the first function in the groovy file that needs the import. – daevski Mar 30 '23 at 21:10