<img height="1" width="1" style="display: none" src="https://www.facebook.com/tr?id=976470819114134&amp;ev=PageView&amp;noscript=1">

How to Import user libraries in ScriptRunner




Working out how to find the absolute path of a file run by ScriptRunner is a handy skill  - it means not having to statically define the file root. So how does one identify an executed script’s absolute file path?

At GLiNTECH, we like to structure ScriptRunner projects for Atlassian products like regular development projects - that is, being able to have non-production and production versions of scripts. It would be great to always develop on a non-production instance, but even for SIT, we commonly see scripts deployed on the production server.

To use the default mechanism, user libraries must be located in relation to the script root, ie. <Jira home>/scripts:

/var/atlassian/application-data/jira/scripts/com/glintech/myproduct/util/myLibrary.groovy
package com.glintech.myproduct.util
 
class MyLibrary {
    int id
 
    static String usefulFunction (String param1) {
        return "A useful action"
    }
 
    MyLibrary(int id) {
        this.id = id
    }
}



/var/atlassian/application-data/jira/scripts/myScript.groovy
import com.glintech.myproduct.util.MyLibrary
 
class MyScript extends Script {
    Object run() {
        def myLib = new MyLibrary(21)
        int id = myLib.id
 
        return MyLibrary.usefulFunction("do it")
    }
}



That doesn't help to separate production from non-production scripts, and new script roots can only be defined as a JVM option on start of Jira. Instead, scripts must be imported from files at runtime:

/var/atlassian/application-data/jira/scripts/dev/myrepo/myScript.groovy
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl
 
class MyScript extends Script {
 
    // MyLibrary is located at /var/atlassian/application-data/jira/scripts/dev/myrepo/util/myLibrary.groovy
    // The package line isn't used when it's imported in this way
    String fileRoot = '/var/atlassian/application-data/jira/scripts/dev/myrepo'
    def myLibraryClass = ScriptRunnerImpl.scriptRunner.parseFileAsClass('',fileRoot+'/util/myLibrary.groovy')
 
    Object run() {
        def myLib = myLibraryClass.newinstance(21)
        int id = myLib.id
 
        return myLibraryClass.usefulFunction("do it")
    }
}

The pattern here is to deploy a repository and run scripts there that load libraries in other locations in that repository. You could walk the repository to find files and import as libraries. You lose ScriptRunner's static type checking but that's a small price to pay for safe rapid development.






Need help configuring your Atlassian tools? Reach out to us for help.