Access extra props (set in initializer) in precompiled script plugin

Hi,

I am not sure if that should work or not.

Lets consider this, a init gradle file in ~/.gradle/init.d/repo.gradle which has this:

allprojects {
    apply plugin: 'base'  
    ext.repository_username = "username"
    ...
}

And now consider a buildSrc/src/main/kotlin/docker-conventions.gradle.kts plugin which reads like this:

val repositoryUserName = extra.get("repository_username").toString()

This gets applied to the project but trying to use the prop has this as a result:

Cannot get property 'repository_username' on extra properties extension as it does not exist

If I use that code in a non buildSrc plugin, just in the build.gradle.kts itself, it is able to resolve that property.

What do I miss here to make that also work in a scripted plugin?

Besides that using ext / extra properties is practically always a work-around for doing something not properly, do you use extra from the right object? Practically all domain objects are ExtensionAware and thus have it. Maybe you set in project but get it on task. Try for example project.extra....

1 Like

You’re on point. I missed that most objects are ExtensionAware, so is the docker { … } extension I wanted to configure - thus it was looking there (where it was not set), I have to be explicit at the lookup so it uses project, thanks - working now.

For the “work-around” - the use-case is, that my build environment provider does set some props in the init.d gradle file in the GRADLE_USER_HOME and I am just a consumer, so they can change infrastructure related stuff and my build will use that automatically, e.g. repository credentials, URLs etc. which is managed externally.
How would you propose to properly do that, so that projects are still able to customize their whole build stack to their needs but can access externally managed build env props?

If they want to do infrastructure things, they should just do them from the init script and not rely on the build using certain properties.

To actually provide properties, they should just put them to <GRADLE_USER_HOME>/gradle.properties or supply them using -P ... instead of setting extra properties through an init script.