Hasan Hosgel (alosdev)
02/23/2024, 12:42 PMdvConfig {
publishable.set(true)
}
Setting the config in the gradle file, doesn't have any effectVampire
02/23/2024, 1:02 PMget() on the properties.
You might be tempted to "fix" that by using afterEvaluate { ... } but don't, it is evil and should be avoided at almost any cost.
Especially to set properties from other properties calling .get() is pretty counterproductive, as with that you void the sense in using the lazy properties.
Instead you should wire your extension properties to the properties that you want to set, this way they are resolved as late as possible and thus hopefully after the user configured the extension, unless there is something else causing eager evaluation.Vampire
02/23/2024, 1:04 PMtoolchain.languageVersion.set(JavaLanguageVersion.of(extension.jvmTarget.get()))
you would do
toolchain.languageVersion.set(extension.jvmTarget.map(JavaLanguageVersion::of))Hasan Hosgel (alosdev)
02/23/2024, 1:06 PMpublishable?
because this one will be used in another plugin do add additional configs
public abstract class KotlinLibraryPlugin : Plugin<Project> {
public override fun apply(project: Project): Unit =
with(project) {
plugins.apply(KotlinBasePlugin::class.java)
plugins.apply(JavaLibraryPlugin::class.java)
val extension = extensions.getByType(DvConfigExtension::class.java)
logger.warn("library known extension for publishable = ${extension.publishable.get()}")
extensions.getByType(JavaPluginExtension::class.java).apply {
withJavadocJar()
withSourcesJar()
}
if (extension.publishable.get()) {
addKotlinCompilerOptions()
}
}
private fun Project.addKotlinCompilerOptions() {
extensions.getByType(KotlinJvmProjectExtension::class.java).apply {
compilerOptions.apply {
freeCompilerArgs.set(
mutableListOf<String>().apply {
addAll(freeCompilerArgs.get())
add("-Xjsr305=strict")
add("-Xexplicit-api=strict")
}
)
}
}
}
}Vampire
02/23/2024, 1:12 PMHasan Hosgel (alosdev)
02/23/2024, 1:14 PMVampire
02/23/2024, 1:32 PMVampire
02/23/2024, 1:33 PMpublishable() in the extension and move the code that you do in addKotlinCompilerOptions inside that function.Vampire
02/23/2024, 1:33 PMProject instance you can simply let Gradle inject into the extension as long as you let Gradle instantiate it and don't do it yourself which is always a good idea as Gradle also adds some decoration like making it ExtensionAware (which I personally also always declare explicitly, that makes using it later easier, you just need to declare that you extend ExtensionAware, nothing else necessary)Hasan Hosgel (alosdev)
02/23/2024, 1:56 PMVampire
02/23/2024, 2:21 PMtrue and then to false?
And assuming there is some doThisAfterTheConfigurationWasDone, there would probably be little that prevents the consumer to also use that and change it again, making your plugin still reacting to the wrong value (actually despite the different name, that exactly is afterEvaluate { ... } and one of its biggest problems and why `Property`/`Provider` was added.Vampire
02/23/2024, 2:23 PMdvConfig {
config {
publishable.set(true)
}
}
Here again config would be a function in your extension that gets an Action<ClassWithPublishableField>.
In the config function you would start with ensuring it is only called once in a lifetime and after the check you would execute the argument and can safely react to the values.Vampire
02/23/2024, 2:25 PMVampire
02/23/2024, 2:26 PMHasan Hosgel (alosdev)
02/23/2024, 2:28 PMVampire
02/23/2024, 2:30 PMpublishable() function?Hasan Hosgel (alosdev)
02/23/2024, 2:32 PM