How can I deduplicate the repositories block that ...
# gradle
p
How can I deduplicate the repositories block that occurs twice in the buildScript and the allProjects block?
e
Put it in
buildSrc
as a
RepositoryHandler
extension
Copy code
// buildSrc/src/main/kotlin/file.kt

fun RepositoryHandler.configureBaseRepos() {
  google()
  kotlinEap()
  maven { ... }
}
Copy code
// whatever/build.gradle(.kts)
buildscript {
  repositiories.configureBaseRepos()
}

allprojects {
  repositiories.configureBaseRepos()
}
If project script is in groovy not kotlin, you might use FileKt.configureBaseRepos(repositories) instead. Groovy has class extension mechanism too cant remember now how it works
p
Thanks 🙂
c
Yes, you can use RepositoryHandler as ExtensionAware and add a method to it. Then using it is just:
repositories { myRepos() }
( both using groovy or Kotlin )
You can pull from this issue: https://github.com/gradle/gradle/issues/7412