Jenkins Groovy with Java Stream API

16 views
Skip to first unread message

Sverre Moe

unread,
Feb 7, 2021, 1:00:39 PM2/7/21
to Jenkins Users

The following piece of code works fine in Groovy, but Jenkins complains about unexpected token: ->

    final def files = findFiles(glob: filePattern)
    final def rootNode = files.stream()
        .map(file -> findByContent(file.getPath()))
        .filter(node -> node != null)
        .findFirst()

To get this working in a local Groovy script the findFile is switched with a simple List of strings.

So why is it not working in Jenkins?

Sverre Moe

unread,
Feb 7, 2021, 1:10:09 PM2/7/21
to Jenkins Users
Here is a solution that does work in both Groovy and Jenkins

    def foundRootNode = null
    for(file in files) {
        final def rootNode = findByContent(file.getPath());
        if (rootNode != null) {
            foundRootNode = rootNode
            break;
        }
    }

However it is not as elegant as using Java Stream API.
Reply all
Reply to author
Forward
0 new messages