JBirdVegas February 2016

Gradle Custom buildScriptRepository methods

I would like to make a custom buildScript repository method so I can easily reference our internal maven repo. Right now I'm required to declare a maven block everywhere we use our plugin.

Here is the current setup

buildscript {
    repositories {
        jcenter()
        maven { url 'http://myNexus:8081/nexus/content/repositories/My-Release' }
    }

    dependencies {
        classpath 'com.example.plugin:my-plugin:1+'
    }
}

What I would like to do is something like this

buildscript {
    repositories {
        jcenter()
        myReleaseRepo()
    }

    dependencies {
        classpath 'com.example.plugin:my-plugin:1+'
    }
}

How can I make a method available to create a repository anywhere we may use the plugin in the future?

Answers


RaGe February 2016

It is possible to add a repo to an init script which would then apply to all gradle invocations that use the init script - without having to individually declare your maven repo in each build.gradle.

Solution 1:

Partial solution, does not do exactly what you're asking for. In init.gradle:

allprojects{
    buildscript{
        repositories{
            maven{ url 'http://myNexus:8081/nexus/content/repositories/My-Release' }
        }
    }
}

Then your build.gradle can skip buildscript repo declaration entirely:

buildscript {
    dependencies {
        classpath 'com.example.plugin:my-plugin:1+'
    }
}

Solution 2:

In fact, you can even move your buildscript classpath declaration to init and have the plugin apply to all projects that use the init script:

beefier init.gradle

allprojects{
    buildscript{
        repositories{
            maven{ url 'http://myNexus:8081/nexus/content/repositories/My-Release' }
        }
        dependencies {
            classpath 'com.example.plugin:my-plugin:1+'
        }
    }
}

gives you a lighter build.gradle

apply plugin: 'my-plugin'

I tried to, but apparently you cannot move the apply line to init.gradle as well. see this defect.

Solution 3:

I retract what I said in the comment above, I figured out how to do exactly what you're asking for. Apparently you can create extensions for the buildscript block using the initscript. However I still prefer solution2, because it gives you a cleaner build.gradle.

To create a buildscript extension, in your init.gradle:

class customRepos {
    def buildscript
      customRepos(buildscript) {
        this.buildscrip 

Post Status

Asked in February 2016
Viewed 2,311 times
Voted 5
Answered 1 times

Search




Leave an answer