Skip to main content

Use .netrc from within Grails BuildConfig.groovy

With the switch from ivy to aether providing credentials to Basic-Auth protected internal maven repos according to documentation needs the credentials in the BuildConfig.groovy. Not much saver, but at least not in the repo you can use the content of your .netrc quite easy with a small helper. Add this script in your BuildConfig.groovy:

// parse a valid .netrc and provide the content as netrc.<machine>.key = value
def netrc = [:]
def netrcFile = new File("${System.getProperty('user.home')}/.netrc")
if (netrcFile.exists()) {
        lastMachine = null
        netrcFile.eachLine{
                it.split().collect{ it.trim() }.with{
                        if (it.size()>1) {
                                switch (it[0]) {
                                case 'machine': lastMachine = it[1]; netrc.put(lastMachine, [:]); break
                                default: netrc."$lastMachine".put(it[0],it[1])
                                }
                        }
                }
        }
}

And then you can use it along with the mavenRepo:

def inhouseHost = 'inhouse.com'
mavenRepo("https://${inhouseHost}/mvn/"){
    auth([
            username: netrc."${inhouseHost}".login,
            password: netrc."${inhouseHost}".password,
    ])
}