简体   繁体   中英

groovy.lang.MissingPropertyException: No such property:

I am trying to get my head around groovy scripting to make some changes to a jenkins pipeline and I keep getting this error:

groovy.lang.MissingPropertyException: No such property: credentials for class:

I have tried declaring the variable with def but I still get the exception, eclipse does not recognise that the property exists.

What am I doing wrong?!

#!/usr/bin/groovy

package common.pipeline

import common.pipeline.Credentials

Credentials credentials = new Credentials()

def withCredentials(steps) {
    credentials.productionPipeline(steps)
}

This script will be compiled by groovy into a Script class with the field definition inside the run method, and with another method withCredentials that is trying to access the field (kinda like this):

import common.pipeline.Credentials

class Script1 extends Script {

    def withCredentials(steps) {
        credentials.productionPipeline(steps)
    }

    def run(args) {
        Credentials credentials = new Credentials()
    }
}

As you can see, this won't work, as the credentials aren't at Field level in the class...

Groovy has an annotation to make this happen:

#!/usr/bin/groovy

package common.pipeline

import common.pipeline.Credentials
import groovy.transform.Field

@Field Credentials credentials = new Credentials()

def withCredentials(steps) {
    credentials.productionPipeline(steps)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM