0%

构建编译JavaGradlePlugin

https://docs.gradle.org/current/userguide/java_gradle_plugin.html#sec:gradle_plugin_dev_usage

  • gradleApi()依赖项添加到配置中,并在任务执行api期间执行插件元数据的验证
  • 集成TestKit
1
apply plugin: 'java-gradle-plugin'
1
2
3
4
5
6
7
8
gradlePlugin {
plugins {
simplePlugin {
id = 'org.gradle.sample.simple-plugin'
implementationClass = 'org.gradle.sample.SimplePlugin'
}
}
}

jar在文件META-INF目录中生成插件描述符。

image.png

gradle-api 中 JavaGradlePluginPlugin

核心代码

这里只关注properties的生成

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
private void configureDescriptorGeneration(Project project, GradlePluginDevelopmentExtension extension) {
// 创建一个task
TaskProvider<GeneratePluginDescriptors> generatePluginDescriptors = project.getTasks().register("pluginDescriptors", GeneratePluginDescriptors.class, (task) -> {
task.setGroup("Plugin development");
task.setDescription("Generates plugin descriptors from plugin declarations.");
// 设置plugins数据
task.getDeclarations().set(extension.getPlugins());
// 设置输出目录
task.getOutputDirectory().set(project.getLayout().getBuildDirectory().dir(task.getName()));
});
// 复制 生成的properties
project.getTasks().named("processResources", Copy.class, (task) -> {
CopySpec copyPluginDescriptors = task.getRootSpec().addChild();
copyPluginDescriptors.into("META-INF/gradle-plugins");
copyPluginDescriptors.from(new Object[]{generatePluginDescriptors});
});
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
public class GeneratePluginDescriptors extends DefaultTask {
private final ListProperty<PluginDeclaration> declarations;
private final DirectoryProperty outputDirectory;

public GeneratePluginDescriptors() {
ObjectFactory objectFactory = this.getProject().getObjects();
this.declarations = objectFactory.listProperty(PluginDeclaration.class);
this.outputDirectory = objectFactory.directoryProperty();
}

@Input
public ListProperty<PluginDeclaration> getDeclarations() {
return this.declarations;
}

@OutputDirectory
public DirectoryProperty getOutputDirectory() {
return this.outputDirectory;
}

@TaskAction // task执行入口
public void generatePluginDescriptors() {
// 输出目录
File outputDir = ((Directory)this.outputDirectory.get()).getAsFile();
this.clearOutputDirectory(outputDir);
// plugins
Iterator var2 = ((List)this.getDeclarations().get()).iterator();

while(var2.hasNext()) {
PluginDeclaration declaration = (PluginDeclaration)var2.next();
//注意这里的命名(plugin-id).properties
File descriptorFile = new File(outputDir, declaration.getId() + ".properties");
Properties properties = new Properties();
// implementation-class=class路径
properties.setProperty("implementation-class", declaration.getImplementationClass());
// 写入文件中
this.writePropertiesTo(properties, descriptorFile);
}

}

@Inject
protected Deleter getDeleter() {
throw new UnsupportedOperationException("Decorator takes care of injection");
}

private void clearOutputDirectory(File directoryToClear) {
try {
this.getDeleter().ensureEmptyDirectory(directoryToClear);
} catch (IOException var3) {
throw new UncheckedIOException(var3);
}
}

private void writePropertiesTo(Properties properties, File descriptorFile) {
try {
PropertiesUtils.store(properties, descriptorFile);
} catch (IOException var4) {
throw new UncheckedIOException(var4);
}
}
}