6

What is the correct way to configure authentication to Artifactory using the Maven resolver?

Currently I'm using:

grails.project.ivy.authentication = {
    repositories {
        mavenRepo "http://SERVER:8081/artifactory/remote-repos"

    }
    credentials {
        realm = "Artifactory Realm"
        host = "SERVER"
        username = "USER"
        password = "PASSWORD"
    }
}

grails.project.dependency.resolver = "maven" // or ivy

grails.project.dependency.resolution = {
    // inherit Grails' default dependencies
    inherits("global") {
        // specify dependency exclusions here; for example, uncomment this to disable ehcache:
        // excludes 'ehcache'
    }
    log "warn" // log level of Ivy resolver, either 'error', 'warn', 'info', 'debug' or 'verbose'
    checksums true // Whether to verify checksums on resolve
    legacyResolve false // whether to do a secondary resolve on plugin installation, not advised and here for backwards compatibility

    repositories {
        inherits true // Whether to inherit repository definitions from plugins

//        mavenLocal()

        mavenRepo id: 'Artifactory', url: "http://SERVER:8081/artifactory/remote-repos"

    }

If I change the resolver to "ivy" dependencies are downloaded.

With maven resolver Artifactory request log shows 401 errors

Relevant Grails documentation: http://grails.org/doc/latest/guide/conf.html#dependencyRepositories

Perhaps it has not yet been updated for Maven.

Greg Pagendam-Turner
  • 2,356
  • 5
  • 32
  • 49

3 Answers3

4

Our shop is currently using Grails 2.3.8, and each developer keeps a external build config in our home directory that contains the following:

artifactory.username = 'username'
artifactory.password = 'password'
artifactory.repo = 'http://our.artifactory.server.com:8080/artifactory/central'
artifactory.repositoryLocation = "http://our.artifactory.server.com:8080/artifactory/libs-release-local"

Here is how we configure all of our Grails projects in our BuildConfig.groovy:

def config = new ConfigSlurper(grailsSettings.grailsEnv).parse(new File("$home/my_build_config.groovy").toURI().toURL())

grails.project.dependency.resolver = "maven"

grails.project.dependency.resolution = {

    inherits("global") {
    }

    log "error"
    checksums true
    legacyResolve false

    repositories {

        String artifactoryUrl = config.artifactory.repo
        mavenRepo(artifactoryUrl) {

            auth([
                    username: config.artifactory.username,
                    password: config.artifactory.password
            ])

            updatePolicy "always"
        }
        mavenLocal()
    }

    dependencies {

        // ...
    }

    plugins {

        // ...
    }
}

If that doesn't work, I would suggest looking at the Artifactory permission settings for your virtual repositories and user permissions in general.

ZDJ
  • 84
  • 4
  • 1
    I get the following error when I try this: groovy.lang.MissingMethodException: No signature of method: groovy.util.ConfigSlurper$_parse_closure5.auth() is applicable for argument types: (java.util.LinkedHashMap) values: [[username:username, password:password]] Possible solutions: with(groovy.lang.Closure), wait(), run(), any(), dump(), wait(long) – Greg Pagendam-Turner Jul 09 '14 at 00:52
  • What does the config file that the ConfigSlurper is reading look like? – ZDJ Jul 09 '14 at 00:55
  • Working now. I had the resolver set to 'master' instead of 'maven' – Greg Pagendam-Turner Jul 09 '14 at 01:04
  • In case anyone else is curious, it looks like the RepositoryConfiguration class defines the methods available for the "mavenRepo" closure (as of grails 2.3.11, anyway). – RMorrisey Mar 04 '15 at 20:25
3

In BuildConfig.groovy, use:

grails.project.repos.default = "AT"

grails {
    project {
        repos {
            AT {
                url = "http://localhost:8081/artifactory/AT/"
                username = "bob"
                password = "MyUnguessablePassword"
            }
        }
    }
}

The doco is a bit hidden away, see: http://grails-plugins.github.io/grails-release/docs/manual/guide/single.html#repositories

A worked example is at: http://wordpress.transentia.com.au/wordpress/

Bob Brown
  • 930
  • 2
  • 7
  • 14
  • The v3 doco says: "All the configuration options described in the next section can either go into your project's BuildConfig.groovy file or your personal ~/.grails/settings.groovy. The latter is particular useful for storing credentials since the file is typically not stored in a shared source repository, thus making it easy to keep that information confidential." so you pays yer money and you takes yer choice... – Bob Brown Apr 05 '15 at 03:28
2

One alternative is to put the following into ~/.grails/settings.groovy

myArtifactory{
    username = 'some_user'
    password = 'some_pass'
}

and than maven repository is configured like

mavenRepo("http://my-artifactory") {
        auth([
                username: grailsSettings.config.myArtifactory.username,
                password: grailsSettings.config.myArtifactory.password
        ])

        updatePolicy "always"
    }