Create custom MavenArtifactRepository in gradle

plugins

(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.

Plugin.groovy:

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 = "'com.android.application' or 'com.android.library' plugin required"

    private Project project

    @Override
    void apply(Project project) {
        this.project = project
        requireAndroidPlugins(project)
        EspressoExtension extension =
                project.getExtensions().create(PLUGIN_EXTENSION, EspressoExtension.class, project)
        project.getTasks().create(PLUGIN_TASK, PrintEspressoResultsTask.class, extension)
        project.getRepositories().add(kostas())
    }

    void requireAndroidPlugins(Project project) {
        Plugin androidApp = project.getPlugins().findPlugin('com.android.application')
        Plugin androidLibrary = project.getPlugins().findPlugin('com.android.library')
        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)
    }
}

KostasRepository.groovy:

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())
            drh.forEach({
                println "Repo: " + it.name
            })
        }
        return repo
    }
//... other methods from interface
}

and here is the KostasRepoAction.groovy:

class KostasRepoAction implements Action<MavenArtifactRepository> {
    @Override
    void execute(MavenArtifactRepository mavenArtifactRepository) {
        mavenArtifactRepository.setUrl(new URI("http://repo.joblessdomination.com/repository/android-releases/"))
        mavenArtifactRepository.setName("kostas")
    }
}

The problem arrives when I try to use it like:

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

sourceCompatibility = 1.8

repositories {
    jcenter()
    mavenCentral()
    kostas()
}

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?