Create custom MavenArtifactRepository in gradle


(Konstantinos Drakonakis) #1

Hello, I am trying to build my first Gradle plugin and I also want to include a shortcut to my Nexus. The shortcut I want to look something like this: kostas() like jcenter().

The problem is that Gradle does not identify the MavenArtifactRepository like it should.


class EspressoPlugin implements Plugin<Project> {

    public static final String PLUGIN_EXTENSION = "espressoResults"
    public static final String PLUGIN_TASK = "printEspressoResults"
    private static final String ERROR_PLUGIN_REQUIRED = "'' or '' plugin required"

    private Project project

    void apply(Project project) {
        this.project = project
        EspressoExtension extension =
                project.getExtensions().create(PLUGIN_EXTENSION, EspressoExtension.class, project)
        project.getTasks().create(PLUGIN_TASK, PrintEspressoResultsTask.class, extension)

    void requireAndroidPlugins(Project project) {
        Plugin androidApp = project.getPlugins().findPlugin('')
        Plugin androidLibrary = project.getPlugins().findPlugin('')
        boolean missingAndroid = androidApp == null
        boolean missingLibrary = androidLibrary == null

        if (!missingAndroid && !missingLibrary) {
            throw new GradleException(ERROR_PLUGIN_REQUIRED)

    MavenArtifactRepository kostas() {
        println "Hello from custom maven repository inside Plugin"
        return KostasRepository.getInstance(project)


class KostasRepository implements MavenArtifactRepository {

    private static MavenArtifactRepository repo
    private URI url
    private String name

    static MavenArtifactRepository getInstance(Project project) {
        if (repo == null) {
            RepositoryHandler drh = project.getRepositories()
            repo = drh.maven(new KostasRepoAction())
                println "Repo: " +
        return repo
//... other methods from interface

and here is the KostasRepoAction.groovy:

class KostasRepoAction implements Action<MavenArtifactRepository> {
    void execute(MavenArtifactRepository mavenArtifactRepository) {
        mavenArtifactRepository.setUrl(new URI(""))

The problem arrives when I try to use it like:

apply plugin: 'groovy'
apply plugin: 'com.kostasdrakonakis.espresso.results'

sourceCompatibility = 1.8

repositories {

espressoResults {
    greeter = "Kostas"
    message = "This is my new plugin"

dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.3.11'
    testCompile group: 'junit', name: 'junit', version: '4.12'

It produces error like:

> Configure project :
Hello from custom maven repository inside Plugin
Repo: kostas

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred evaluating root project 'espresso-plugin'.
> Could not find method kostas() for arguments [] on repository container of type org.gradle.api.internal.artifacts.dsl.DefaultRepositoryHandler.

even though I have at least 1 repo in RepositoryHandler as shown in console

Any ideas of what am I doing wrong?