Is it possible to access a custom extension value ...
# gradle
j
Is it possible to access a custom extension value before a project is evaluated in a Gradle plugin? I created my own Gradle plugin like this:
Copy code
interface TestI { val value: String }

abstract class TestExtension @Inject constructor(objects: ObjectFactory) {
    val testI = 
        objects.property(TestI::class.java).convention(object : TestI {
            override val value: String = "AA"
        })
}

class MyGradlePlugin : Plugin<Project> {
    override fun apply(project: Project) {
        val mgp = project.extensions.create<TestExtension>("mgp")
        println("testI: ${mgp.testI.get().value}")
    }
}
I’m using this like below, but the
testI
log value always printed as “AA”. How do I get “BB”?
Copy code
mgp {
  testI.set(object : TestI {
    override val value: String = "BB"
  })
}
not kotlin but kotlin colored 1
v
If you have a look at the channel topic, you will find, that you question is off-topic. But regarding the question, it depends on what you want to do with that value. If you need it at execution time of a task, write the property to the according property of the task and then read the value at execution time, that's exactly what Provider, Property, and alike are introduced for. If you need the value to do some decision in the plugin, better use a different approach, like having a function in the extension that does the logic with the given argument for example.
j
As you say, I didn't check the channel topics. (sorry 🙇‍♂️ ) Thanks to you I joined the Gradle community slack! Thanks for making me aware of this. And thanks for answering my question. gratitude thank you
👌 1