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目录中生成插件描述符。
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) { TaskProvider<GeneratePluginDescriptors> generatePluginDescriptors = project.getTasks().register("pluginDescriptors", GeneratePluginDescriptors.class, (task) -> { task.setGroup("Plugin development"); task.setDescription("Generates plugin descriptors from plugin declarations."); task.getDeclarations().set(extension.getPlugins()); task.getOutputDirectory().set(project.getLayout().getBuildDirectory().dir(task.getName())); }); 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 public void generatePluginDescriptors() { File outputDir = ((Directory)this.outputDirectory.get()).getAsFile(); this.clearOutputDirectory(outputDir); Iterator var2 = ((List)this.getDeclarations().get()).iterator();
while(var2.hasNext()) { PluginDeclaration declaration = (PluginDeclaration)var2.next(); File descriptorFile = new File(outputDir, declaration.getId() + ".properties"); Properties properties = new Properties(); 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); } } }
|