0

我正在尝试为 XML Beans 编写一个 Gradle 插件。我从“Hello from Gradle”插件示例之一开始,也是 R. Artavia在此处发布的插件。该插件直接进入 jar - 我试图只生成源代码。生成的源代码必须与其他项目源代码一起编译并包含在单个 jar 中。其他目标包括 - 完整的插件 - 我只需要“应用插件:'xmlbean'” - 如果我愿意,我可以配置源/代码生成位置和一些功能 - 它检测是否需要重建。(嗯,最终!!!)

我有一个很好的开始,但是被阻止定义一个新的 sourceSet。我收到一个错误“没有这样的属性 'srcDirs'”(或 'srcDir')。似乎我必须在某个地方定义一些东西才能使新的 sourceSet 工作,但我找不到它。我尝试了几种不同的语法(带/不带等号、括号、srcDir/srcDirs 等 - 没有任何效果......

我需要在插件中做什么才能正确识别新的 sourceSet 条目?

谢谢!JKE

文件:xmlbean.gradle(包括用于调试的问候插件)

apply plugin: xmlbean
apply plugin: 'java'

xmlbean {
  message = 'Hi'
  greeter = 'Gradle'
}

class xmlbean implements Plugin<Project> {

  void apply(Project project) {

    project.extensions.create("xmlbean", xmlbeanExtension)
    Task xmlbeanTask = project.task('xmlbean')
    xmlbeanTask << {

      project.configurations {
        xmlbeans
      }

      project.dependencies {
        xmlbeans 'org.apache.xmlbeans:xmlbeans:2.5.0'
      }

      project.sourceSets {
        main {
          java {
            srcDirs += '$project.buildDir/generated-source/xmlbeans'
          }
        }
        xmlbeans {
          srcDirs = ['src/main/xsd']
        }
      }

      ant.taskdef(name: 'xmlbean',
                  classname: 'org.apache.xmlbeans.impl.tool.XMLBean',
                  classpath: project.configurations.xmlbeans.asPath)
      ant.xmlbean(schema: project.sourceSets.xmlbean.srcDir,
                  srconly: true,
                  srcgendir: "$project.buildDir/generated-sources/xmlbeans",
                  classpath: project.configurations.xmlbeans.asPath)

      println "${project.xmlbean.message} from ${project.xmlbean.greeter}"
    }
    project.compileJava.dependsOn(xmlbeanTask)
  }
}

class xmlbeanExtension {
  String message
  String greeter
}

文件:build.gradle

apply from: '../gradle/xmlbeans.gradle'

dependencies {
  compile "xalan:xalan:$ver_xalan",
          ":viz-common:0.0.1",
          ":uform-repository:0.1.0"
}

控制台:错误消息:

:idk:xmlbean FAILED

FAILURE: Build failed with an exception.

* Where:
Script 'C:\jdev\cpc-maven\try.g2\comotion\gradle\xmlbeans.gradle' line: 32

* What went wrong:
Execution failed for task ':idk:xmlbean'.
> No such property: srcDirs for class: org.gradle.api.internal.tasks.DefaultSourceSet_Decorated
...
BUILD FAILED

Gradle 信息:Windows 7 AMD64 上的版本 2.5 / groovy 2.3.10 / JVM 7u55

4

1 回答 1

3

您应该尝试熟悉 Gradle DSL 参考指南,因为它在这种情况下会有很大帮助。例如,如果您单击sourceSets { }左侧导航栏中的链接,您将被带到有关源集的这一部分。

从那里,您会发现该sourceSets {}块由一个类支持,SourceSetContainer. 嵌套在内部的下一级配置由SourceSet对象支持,然后在其中您有一个或多个SourceDirectorySet配置。当您点击 的链接时SourceDirectorySet,您会看到有getSrcDirs()setSrcDirs()方法。

So how does this help? If you look closely at the exception, you'll see that Gradle is saying it can't find a srcDirs property on DefaultSourceSet_Decorated, which you can hopefully infer is an instance of SourceSet. That interface does not have an srcDirs property. That's because your xmlbeans {} block is configuring a SourceSet, not a SourceDirectorySet. You need to add another nested configuration to gain access to srcDirs.

At this point, I'm wondering whether a new source set is the appropriate solution. Unfortunately it's not clear to me exactly what the plugin should be doing, so I can't offer any alternatives at this point.

于 2015-07-22T17:33:11.867 回答