1

Say I define a plugin, like so:

import org.gradle.api.artifacts.dsl.RepositoryHandler
import org.gradle.api.artifacts.repositories.MavenArtifactRepository
import org.gradle.api.Project
import org.gradle.api.Plugin

class MyRepos implements Plugin<Project> {
    static final String NEXUS_URL = 'http://nexus.mine.com/nexus/content/repositories/'
    static final List<String> NEXUS_REPOS = [
        'central',
        'build',
        'snapshots',
        'release-candidates',
        'releases',
    ]

    void apply(Project project) {
        project.repositories {
            NEXUS_REPOS.each {
                maven {
                    url (NEXUS_URL + it)
                }
            }
            mavenLocal()
        }
        project.buildscript {
            repositories {
                maven {
                    url (NEXUS_URL + 'central')
                }
                mavenLocal()
            }
        }
    }
}

and in a local build.gradle, I write

apply plugin: MyRepos

buildscript {
    dependencies {
       ...
    }
}

My desire is for the two buildscript sections to be merged such that the repositories are defined in the plugin and the dependencies in build.gradle, but it appears that the local declaration overrides the plugin and I end up with an error indicating "no repositories defined".

If I do this via configuration injection from the top level build.gradle, is the result the same?

Maybe the right solution is for the plugin to provide an ext closure to define useMyRepos() similar to the way mavenCentral() is used...

Christian Goetze
  • 2,254
  • 3
  • 34
  • 51

1 Answers1

1

Generally configuration items in gradle are merged so you can apply configurations in different locations. You can, for example, configure some of the dependencies in one build script that will be applied to another build script that will add additional dependencies.

However, your case is a bit different since you're using buildScript configuration which is can be used to define the dependencies for the build script itself rather than the root repositories node which is intended for defining the dependencies of the project you build.

In this case, according to Purpose of buildScript in Gradle as these are different configurations you'll have to define your dependency twice.

Community
  • 1
  • 1
Amnon Shochot
  • 8,998
  • 4
  • 24
  • 30
  • maybe dependencies are merged, I haven't tested that - but it seems the contents of `buildscripts` and `repositories` are not merged. I still need to check whether that is true for configuration injection. – Christian Goetze Mar 31 '15 at 19:50
  • You can first try it with two scripts (one applies the other using `apply from`). Gradle may have different rules for different object types or for merging plugins and scripts. – Amnon Shochot Mar 31 '15 at 19:52
  • Ok - did some more research and according to [Purpose of buildScript in Gradle](http://stackoverflow.com/questions/13923766/gradle-buildscript-dependencies) since the 'buildscript' block is meant to define the dependencies for the build script itself while the 'repositories' is meant for defining the dependencies of the project you build then in this case the two repositories nodes will not be merged. – Amnon Shochot Mar 31 '15 at 22:13
  • I'm testing now with plain configuration injection... What I want is to inject `buildscripts { repositories { ... } }` and allow local build.gradle files to add additional items to buildscripts. I hope that that's possible. – Christian Goetze Mar 31 '15 at 23:35
  • Looks like it's true. I just have to use injection and cannot set these things via a plugin. – Christian Goetze Apr 01 '15 at 22:48