Gradle自定义插件:如何使用预期在构建脚本中设置的属性配置Gradle任务

时间:2015-11-20 19:54:38

标签: gradle build.gradle gradle-plugin

我正在尝试编写一个自定义Gradle插件,我需要在项目中使用属性来配置一个“Zip”任务,我期望这些属性由构建脚本(build.gradle)提供。 / p>

以下是build.gradle脚本的相关部分:

oozie {
  sourceSets {
    workflow {
      dslDir = 'workflow/dsl'
      dslCopySpec = {
        rename { it - ~/\.[^\.]+$/ + '.xml' }
        filter(OozieDslFilter)
      }
      resourceDir = 'workflow/resources'
      resourceCopySpec = {}
    }
    coordinator {
      dslDir = 'coordinator/dsl'
      dslCopySpec = {
        rename { it - ~/\.[^\.]+$/ + '.xml' }
        filter(OozieDslFilter)
      }
      resourceDir = 'coordinator/resources'
      resourceCopySpec = {}
    }
  }
}

这是我的插件代码,目前没有按照我的预期执行:

class Oozie implements Plugin<Project> {

  @Override
  void apply(Project project) {

    project.configurations {
      oozieLibs {
        description = /Java libraries (jars) to be downloaded and included in the workflow 'lib' directory/
        transitive = true
      }
    }

    project.extensions.create('oozie',
        OozieExtension,
        project.container(OozieSourceSet)
    )

    project.task('build', type: Zip) {
      group = 'Oozie Workflow Build'
      with {
        archiveName = "${project.name}-${project.version}.${extension}"
        destinationDir = project.buildDir
      }
      from(project.configurations.oozieLibs) {
        into 'lib'
      }

      // project.oozie.sourceSets is empty at this time for obvious reasons.
      println project.oozie.sourceSets

      project.oozie.sourceSets.each { OozieSourceSet sourceSet ->
        println sourceSet
        from(sourceSet.dslDir, sourceSet.dslCopySpec)
        from(sourceSet.resourceDir, sourceSet.resourceCopySpec)
      }
    }

    project.task('clean', type: Delete) {
      group = 'Oozie Workflow Build'
      delete project.buildDir
    }
  }

}

我希望能够在插件的'apply'方法中读取project.oozie.sourceSets等属性的值。

但是,在调用自定义插件的“apply”方法时,project.oozie.sourceSets的值为空,因为尚未执行设置该属性值的build.gradle脚本。 / p>

如何处理此问题?

谢谢!

1 个答案:

答案 0 :(得分:0)

我选择的解决方案是:

class Oozie implements Plugin<Project> {

  static final String DEFAULT_FILE_EXTENSION_FINDER_REGEX = /\.[^\.]+$/
  static final String DEFAULT_EXTENSION_REPLACEMENT = '.xml'
  final static DEFAULT_WORKFLOW_ROOT = 'workflow'
  final static DEFAULT_COORDINATOR_ROOT = 'coordinator'
  final static DEFAULT_DSL_DIR = 'dsl'
  final static DEFAULT_RESOURCE_DIR = 'resources'

  @Override
  void apply(Project project) {

    project.configurations {
      oozieLibs {
        description = /Java libraries (jars) to be downloaded and included in the workflow 'lib' directory/
        transitive = true
      }
    }

    project.extensions.create('oozie',
        OozieExtension,
        project.container(OozieSourceSet)
    )

    project.oozie.sourceSets {
      workflow {
        dslCopySpec = {
          from("${DEFAULT_WORKFLOW_ROOT}/${DEFAULT_DSL_DIR}") {
            rename { it - ~DEFAULT_FILE_EXTENSION_FINDER_REGEX + DEFAULT_EXTENSION_REPLACEMENT }
            filter(OozieWorkflowDslFilter)
          }
        }
        resourceCopySpec = {
          from("${DEFAULT_WORKFLOW_ROOT}/${DEFAULT_RESOURCE_DIR}")
        }
      }
      coordinator {
        dslCopySpec = {
          from("${DEFAULT_COORDINATOR_ROOT}/${DEFAULT_DSL_DIR}") {
            rename { it - ~DEFAULT_FILE_EXTENSION_FINDER_REGEX + DEFAULT_EXTENSION_REPLACEMENT }
            filter(OozieWorkflowDslFilter)
          }
        }
        resourceCopySpec = {
          from("${DEFAULT_COORDINATOR_ROOT}/${DEFAULT_RESOURCE_DIR}")
        }
      }
    }

    project.task('build', type: Zip) {
      group = 'Oozie Workflow Build'
      with {
        archiveName = "${project.name}-${project.version}.${extension}"
        destinationDir = project.buildDir
      }
    }

    project.task('clean', type: Delete) {
      group = 'Oozie Workflow Build'
      delete project.buildDir
    }

    project.afterEvaluate {
      project.build.with {
        from(project.configurations.oozieLibs) {
          into 'lib'
        }
      }

      project.oozie.sourceSets.each { OozieSourceSet sourceSet ->
        project.build.with(sourceSet.dslCopySpec)
        project.build.with(sourceSet.resourceCopySpec)
      }
    }
  }

}