Central location for repositories configuration

Hi!

We have several repositories, and to avoid copy-pasting we have another repository with common gradle files (used as apply from: ".../foo.groovy"). The problem is that every build.gradle and every common gradle file have the same buildscripts.repositories and repositories blocks.

I have used this topic to create a helper method (myrepo()) that can be used inside buildscripts.repositories and repositories blocks. It works fine for build.gradle but not for common gradle files. I’ve tried both

buildscript {
    apply from: "$rootDir/src/main/gradle/myrepo.groovy"
    repositories { myrepo() }
}

and

buildscript {
    apply from: "$rootDir/src/main/gradle/myrepo.groovy"
    repositories { project.buildscript.repositories.ext.myrepo() }
}

In the 1st case myrepo() is not defined, in the 2nd no repositories are actually defined.

Is it possible to achieve required? Or, for example, define repos only in build.gradle and not in files applied using apply from?

Actually, using legacy script plugins (the things you use with “apply from”) have many quirks and are discouraged anyway.

You should instead consider switching to convention plugins implemented in buildSrc or included builds, for example implemented as precompiled script plugins.

It looks like with convention plugins I’ll have the same problem: I want to avoid repository configuration defined only once, but I need to download the convention plugin, so I need repository configuration.

The legacy script plugin you use right now you also do not download but have locally.
If the convention plugin is in buildSrc or an included build it is the same.
If you want to publish the convention plugin then of course, yes, you at least have to specify the plugin repository where it can be downloaded from.