Jenkins pipeline throws "StackOverflowError: Excessively nested closures/functions"

Calling getProjectPath() method causes this exception. It happens, because if Groovy finds a getter method for a field foo like getFoo() it fallbacks to execute this method anytime it sees accessing foo value.

What does it mean in your case? When you call the method

def getProjectPath() {
    projectPath.split("/")[-1]
}

it runs into infinite recursion because this method is seen as:

def getProjectPath() {
    getProjectPath().split("/")[-1]
}

so it never reaches .split("/")[-1] - that is why replacing it with tokenize() method didn't change a thing.

Solution: rename getProjectPath() method or projectPath variable name.

Groovy class properties

A property is an externally visible feature of a class. Rather than just using a public field to represent such features (which provides a more limited abstraction and would restrict refactoring possibilities), the typical convention in Java is to follow JavaBean conventions, i.e. represent the property using a combination of a private backing field and getters/setters.

Source: http://groovy-lang.org/objectorientation.html#properties

This part of Groovy's documentation explains this behavior. It can be simplified to an example - a class like:

class Person {
    String name
}

is compiled to something like this:

class Person {
    private String name

    void setName(String name) {
        this.name = name
    }

    String getName() {
        return this.name
    }
}

The general rule of thumb when working with Groovy is that when you specify a field foo you implement getFoo() carefully (if you actually have to do it). Especially you avoid accessing field foo inside this method, because it runs into this infinite recursive call issue.