alex_bondar
alex_bondar

Reputation: 442

Gradle externalized configuration

We have multiple projects/services where we do repeat same configuration over and over again as a part of build.gradle file. Examples could be configuration for spotless plugin, docker, junit/jacoco, versioning, groovy tasks, etc.

I wonder is there a way to externalize it or move to single place so that if needed we can update configuration once instead of doing the same across each and every project.

Very naive idea is to have master-build.gradle file stored in it's own git repo and where needed we can refer it as a git submodule with capabilities to extend/rewrite. Open for any ideas. Thanks!

Upvotes: 0

Views: 256

Answers (1)

Daniele
Daniele

Reputation: 2837

Gradle scripts can be reused by creating external script files and importing them using apply from: my-script.gradle. apply from also accepts an URL, so you can use something like

apply from: 'https://github.com/user/myproject/raw/master/hello.gradle'

Note tough that using plain references (URLs; or GIT repo URLs) is sub-optimal; a better approach is to identify your build dependencies (and these scripts ARE dependencies!) using group:artifact:version coordinates- that is achieved by writing a plugin and publishing to a repo (eg. to https://plugins.gradle.org/).

Upvotes: 1

Related Questions