mr.nothing
mr.nothing

Reputation: 5399

gmaven get access to the maven variables from the groovy class

Does anybody know how to get access to the maven variable (project.properties getProperty('justAbotherProperty')) from within the groovy class? When I'm trying to do this within the plain script(outside of the class) I'm able to do this. According to the gmaven documentation maven project variable is accessable from within the script. If it is so I just want to confirm it and then come back to the scipts instead of classes...

Here is the code I use:

class Connector {
    private String sshHost
    private String sshUser
    private String sshPass
    private Integer sshPort

    private def parseParametes() {
        sshHost = project.properties.getProperty('scp.host')
        sshUser = project.properties.getProperty('scp.user')
        sshPass = project.properties.getProperty('scp.password')
        sshPort = project.properties.getProperty('scp.port').toInteger()
    }
}  

Execution of parseParameters() spits out:

groovy.lang.MissingPropertyException: No such property: project for class: Connector 

Thanks in advance for any help!

Upvotes: 1

Views: 1987

Answers (2)

user1236636
user1236636

Reputation: 21

You need to pass the script variables into your classes where needed. So if you redefine your class like this:

class Connector {
    private String sshHost
    private String sshUser
    private String sshPass
    private Integer sshPort

    Connector(properties) {
        sshHost = properties['scp.host']
        sshUser = properties['scp.user']
        sshPass = properties['scp.password']
        sshPort = properties['scp.port'] as Integer
    }
}

And then in your plugin execution, construct the connector passing it the project.properties:

<source>
    def connector = new Connector(project.properties)
</source>

Upvotes: 2

dmahapatro
dmahapatro

Reputation: 50275

(Untested)
Before you move back to scripts from classes, have a look at Using Groovy Classes. Try to set the scriptpath and follow the example as mentioned in the page.

I tried to answer a similar question earlier. I hope when you set the Groovy Class in the scriptpath the default variables which are tied to the script environment should be available in the classes.

Upvotes: 1

Related Questions