Importing custom groovy classes from lib/ at configuration parsing time

Hi, I am trying to create a custom afterScript groovy function that runs after every process (basically, it implements some custom logging and cleanup). It uses a custom class, which I have created in a file in the lib/ subdirectory. However, I can’t figure out how to import the custom class.

Based on some searching (a relevant post here), below I have what I’ve tried. However, I’m not sure how to get groovy code (and particularly the parseClass call) to execute before the configuration is parsed and an ‘unable to resolve class’ error is thrown.

What’s the correct way to do this?


nextflow.config:

process.afterScript = {
    customAfterScript()
}

lib/common.groovy:


import groovy.lang.GroovyClassLoader
GroovyClassLoader class_loader = new GroovyClassLoader()
def parseClass = {filename -> class_loader.parseClass(new File(filename))}
CustomClass = parseClass("CustomClass.groovy")

def customAfterScript = {

    // do some stuff, then
    // fail here
    // error: /path/to/lib/common.groovy: linenumber: unable to resolve class CustomClass
    // also, I've tried moving the parseClass into this script; no change.
    new CustomClass(params.foo)
}

lib/CustomClass.groovy:

class CustomClass{
    //do stuff
}

Hello @quejebo,

According to the documentation, the afterScript directive allows you to run custom bash script, so as far as I know you can’t run Groovy code with afterScript or beforeScript. For examples of what you can do with bash in these directives, check real examples in the nf-core GitHub org here.

UPDATE: You can provide a closure to afterScript or beforeScript with Groovy code but this won’t give you what you expect. It will be evaluated before the task is run which is definitely not “after script” :sweat_smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.