ctruchi
(Cyril Truchi)
August 2, 2023, 7:28am
1
In my buildSrc, I have one precompiled script plugin used to configure the feature name of my module (in a multi module project) like so :
buildSrc/src/main/kotlin/myFeature.gradle.kts
:
interface MyFeaturePluginExtension {
val name: Property<String>
}
val extension = extension.create<MyFeaturePluginExtension>("myFeature")
extension.name.convention(project.name.substringBefore("-"))
So in my modules, I can override the default name like so :
awesomeFeature/build.gradle.kts
:
plugins {
`my-feature`
}
myFeature {
name.set("awesome-feature")
}
I would like to use this name in another precompiled script plugin but can’t find how.
I tried this :
buildSrc/src/main/kotlin/baseDeps.gradle.kts
:
plugins {
id("my-feature")
}
val featureName = the<MyFeaturePluginExtension>().name.get()
dependencies {
"implementation"(project(":$featureName-domain"))
}
I get the following error : Unresolved reference: MyFeaturePluginExtension
How can I do something like this ?
Vampire
(Björn Kautler)
August 2, 2023, 10:44am
2
Imagine the contents of a Kotlin as body of a class, that’s close to reality.
So your extension interface is not available to the other script.
Move it out into its own .kt
file where both scripts can access it, then it should work like you tried.
Except that you get the name
before giving the consumer a chance to set it by using an early get()
.
You have to delay the dependency adding.
ctruchi
(Cyril Truchi)
August 2, 2023, 10:48am
3
Ok, I will try that. Thanks for your answer
ctruchi
(Cyril Truchi)
August 2, 2023, 3:02pm
4
So, I have not the error anymore if I put the interface in a separate file. Thanks a lot.
You told me to delay the dependency adding. How can I do this ?
I thought doing this would work :
buildSrc/src/main/kotlin/baseDeps.gradle.kts
:
plugins {
id("my-feature")
}
val featureName = the<MyFeaturePluginExtension>().name
dependencies {
"implementation"(project(":${featureName.get()}-domain"))
}
But it seems dependencies block is ran before my configuration.
Any idea ?
Vampire
(Björn Kautler)
August 2, 2023, 3:52pm
5
That variant has exactly the same problem that you immediately evaluate it.
Iirc, should be something like
val implementation by configurations
implementation.dependencies.addLater(featureName.map { dependencies.project(":$it-domain") })
ctruchi
(Cyril Truchi)
August 3, 2023, 7:07am
6
Works like a charm. Thanks a lot.
In case someone will check later, I had to change a bit to :
implementation.dependencies.addLater(project.provider {featureName.let { dependencies.project(":${it.get()}-domain") }})
Vampire
(Björn Kautler)
August 3, 2023, 7:54am
7
Can you elaborate why that was necessary?
What I gave you at least compiled and I think should work, though I didn’t test it.
Why did you need to uglify it?
ctruchi
(Cyril Truchi)
August 4, 2023, 7:21am
8
Ok, my bad.
It was a misunderstanding on my part.
I went back to your solution.
Thanks a lot.
1 Like