If I want to compute a build number once and share...
# community-support
s
If I want to compute a build number once and share it among several projects, what is the best way to do it? Computing it would involve calls to Git because I would like to include the commit hash, and/or accessing some env variables. What's the best place to put the computed build number to comply with the best practices re project isolation and whatnot? I could do it in a precompiled plugin that each project applies but it seems kind of overkill for each project to compute the version over and over.
m
root project task that produces an artifact (file that contains the build number) that is resolved in all other subprojects using the variant-aware dependency dance 🕺
s
oh god...
😅 1
m
I don't see a way out of the dance
BuildService
maybe
n
we use a settings plugin that computes a valuesource and cascades that version to all projects:
Copy code
settings.gradle.beforeProject { version = infoProvider.get() }
👍 3
m
@Niels Doucet where does
infoProvider
live? the settings classloader?
n
Copy code
settings.gradle.settingsEvaluated {
            // Create the version info provider
            val infoProvider: Provider<VersionInfo> =
                settings.providers.of(VersionInfoSource::class.java)
yes, the VersionInfoSource object just takes input from our custom settings plugin extension and takes information from git
til 1
👀 1
m
TIL a new way to share data between projects, thanks!
j
I agree that computing in settings (or a settings plugin) and then sharing via
beforeProject
is the best solution. If possible, use the new
lifecycle
hook though:
Copy code
gradle.lifecycle.beforeProject {}
That one is fully compatible with project isolation.
👍 3
v
In our (still 7.6.4, don't ask) build I have a build service that does the build identifier calculation in its initialization so it is only done once in the whole build. In our base convention plugin that is applied to all projects we set a custom version as
project.version
. This custom version retrieves the build label service and forwards the request for the build identifier to the service. So there is one central place where the identifier is calculated once, the service, and each project has a version instance that forwards the request to that one service.
y
In our (still 7.6.4, don't ask) build
We don't discriminate here 😸
😄 1