首页 > 解决方案 > Gradle 插件:无法将 Java.io.File 类的“A.txt”转换为 org.gradle.api.file.Property 类

问题描述

我正在尝试使用 DSL 编写插件:

model {
 model1 {input = file(A.txt) output = file("/")}
 model2 {input = file(A.txt) output = file("/")}
}

并基于model1和model2我将进行处理并将结果输出到输出目录中。

​class Extension 
{
 final String name
 final Property<file> input
 final Property<file> output

 @Inject
 Extension(String name,ObjectFactory objectfactory)
 {
   this.name = name
   input = objectfactory.property(File)
   output = objectfactory.property(File)
 }

}

///插件类

class Plugin implements Plugin<Project> {
@Override
void apply(final Project project) {
    NamedDomainObjectContainer<Extension> container= project.container(Extension) {
            name -> new Extension(name, project.getObjects());
        }
    project.getExtensions().add("model", container);

    container.each {
            String taskName = "model" + name;
            project.tasks.create(taskName, Task)  {
                    input.set(input)
                    output.set(output)
                }
        }
}

} ​</p>

//任务类

class Deploy extends DefaultTask
{
  @InputFile
  Property<File> input = project.objects.property(File)


  @OutputFile
  Property<File> output= project.objects.property(File)

  @TaskAction
 def task()
 { 
   //custom logic
 }
}​

这段代码的灵感来自 https://guides.gradle.org/implementing-gradle-plugins/(ServerEnvironment Plugin)

但是当我运行它时,我无法将 Java.io.File 类的“A.txt”转换为 org.gradle.api.file.Property 类。

谁能告诉这里有什么问题和解决方法。

谢谢

额外观察:当我更改 = with .set in my build.gradle 错误更改为 stackoverflow

标签: javaandroidgradlebuildgradle-plugin

解决方案


推荐阅读