In our in-house gradle distribution, we have multi...
# community-support
n
In our in-house gradle distribution, we have multiple init scripts. Is there a way to share some logic between those scripts without having to resort to the
extraProperties
abstraction?
v
There should always be a way to not use
extraProperties
, as you should always feel dirty when using it. 😄 How to best solve it might depend on the details of the situation. Do you maybe have some MCVE one could look at that demonstrates what you try to do or how you use the extra properties?
n
We're simply reading from the gradle.properties file (manually, as those properties aren't available yet of course) and setting up a buildscript repository connection to our internal repository to resolve dependencies. Currently this logic is copied in every init script that requires it, so we aren't actually using
extraProperties
(yet).
We now are starting a migration project for our internal repository, which would require switching the uri to it, based on a system property. So that means I have to adjust every init script to make this possible. So it made me not want to do the copy/paste thing anymore 😛
v
https://docs.gradle.org/current/userguide/init_scripts.html#sec:using_an_init_script
Put a file called
yourfilename.init.gradle(.kts)
in the
$_GRADLE_HOME_/init.d/
directory. Entries will be evaluated in alphabetic order.
So can't you simply make sure the thing needed commonly is done in an init script coming first?
n
Isn't the buildscript repository configuration "local" to a single init script?
v
Do you mean
initscript
?
n
yes, sorry
initscript
🤦 For reference: the logic we're executing
Copy code
val startParam = gradle.startParameter as org.gradle.api.internal.StartParameterInternal
    val environ = org.gradle.internal.cc.impl.services.DefaultEnvironment()
    val gradlePropsLoader = org.gradle.initialization.DefaultGradlePropertiesLoader(startParam, environ)
    val projPropsLoader = org.gradle.initialization.properties.DefaultProjectPropertiesLoader(startParam, environ)
    val propsController =
        org.gradle.initialization.DefaultGradlePropertiesController(gradlePropsLoader, { _ -> }, projPropsLoader)
    propsController.loadGradlePropertiesFrom(startParam.currentDir, false)
    val props = propsController.gradleProperties

    val usr = props.find("<usernameProp>") as String
    val psw = props.find("<passwordProp>") as String
    repositories {
        maven {
            url = uri("<repo uri>")
            credentials {
                username = usr
                password = psw
            }
        }
    }
v
That probably cannot be deduplicated then I guess. Just like
buildscript
blocks in build scripts and settings scripts, this is evaluated up-front separately to find the dependencies that are needed to execute the init script. Maybe you could leverage a legacy script plugin by using
apply from
within the
initscript
to apply the one with the common logic, but I have no idea whether that will work.
🤔 1
e
I think you can write a proper init plugin which holds the shared state, and apply it from your init scripts
v
@ephemient did you read on what it is about? He wants to unify the
initscript { repositories { ... } }
declaration among the init scripts