[英]Gradle Settings plugin extension
I want to create a settings plugin (not project plugin) to simplify some stuff, but I cannot get the configuration clause to work.我想创建一个设置插件(不是项目插件)来简化一些东西,但我无法让配置子句起作用。
This is my plugin (Java code)这是我的插件(Java 代码)
public class SettingsPlugin implements Plugin<Settings> {
@Override
public void apply(Settings target) {
target.getExtensions()
.create("modules", IncludeModulesExtension.class, target);
System.err.println("Applied settings plugin");
}
}
public class IncludeModulesExtension {
private final Settings _settings;
public IncludeModulesExtension(Settings settings) {
_settings = settings;
}
public void include(String path) {
}
}
My problem is, that gradle is not picking up the "modules" dynamic function in my settings.gradle.kts:我的问题是,gradle 没有在我的 settings.gradle.kts 中选择“模块”动态 function:
pluginManagement {
...
}
plugins {
id("com.ieffects.gradle-tools.settings-server") version ("7.0.23-SNAPSHOT")
}
modules {
}
I omitted the pluginManagement, the plugin is found and applied, however the "modules" is not picked up.我省略了pluginManagement,找到并应用了插件,但是没有拾取“模块”。 What is it I'm doing wrong?
我做错了什么?
Starting Gradle Daemon...
Gradle Daemon started in 2 s 396 ms
Applied settings plugin
e: /Volumes/Development/Git/server-framework-galcon/settings.gradle.kts:22:1: Unresolved reference: modules
FAILURE: Build failed with an exception.
* Where:
Settings file '/Volumes/Development/Git/server-framework-galcon/settings.gradle.kts' line: 22
* What went wrong:
Script compilation error:
Line 22: modules {
^ Unresolved reference: modules
1 error
I faced the same issue when implementing a similar plugin, and although I didn't find out why, I did manage to work around it:我在实现一个类似的插件时遇到了同样的问题,虽然我没有找到原因,但我确实设法解决了这个问题:
// SettingsExtensions.kt
import org.gradle.api.Action
import org.gradle.api.initialization.Settings
import org.gradle.api.plugins.ExtensionAware
/* WORKAROUND: for some reason a type-safe accessor is not generated for the extension,
* even though it is present in the extension container where the plugin is applied.
* This seems to work fine, and the extension methods are only available when the plugin
* is actually applied. */
/**
* Retrieves the [modules][IncludeModulesExtension]
* extension.
*/
val Settings.modules: IncludeModulesExtension
get() =
(this as ExtensionAware).extensions.getByName("modules") as IncludeModulesExtension
/**
* Configures the [modules][IncludeModulesExtension] extension.
*/
fun Settings.modules(configure: Action<IncludeModulesExtension>): Unit =
(this as ExtensionAware).extensions.configure("modules",
configure)
As the comment explains, it behaves exactly the same as a generated type-safe accessor (same syntax and the extension is only available when the plugin is actually applied).正如评论所解释的,它的行为与生成的类型安全访问器完全相同(相同的语法和扩展仅在实际应用插件时可用)。
I didn't test if it works for the Groovy DSL, but since the extension methods are syntactically identical to the generated accessors, I assume it does.我没有测试它是否适用于 Groovy DSL,但由于扩展方法在语法上与生成的访问器相同,我认为它可以。
Alternatively you can do without if instead of the modules
DSL you do this in the settings script where the plugin is applied:或者,如果您在应用插件的设置脚本中执行此操作,而不是
modules
DSL,则可以这样做:
configure<IncludeModulesExtension> {
...
}
This also works because even though the type-safe accessor is not generated, the extension is properly initialized and added to the extensions container.这也有效,因为即使没有生成类型安全的访问器,扩展也会正确初始化并添加到扩展容器中。 But the DSL is obviously nicer.
但是DSL显然更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.