从TaskAction获取jar资源

时间:2018-12-05 19:26:10

标签: kotlin gradle-plugin

我正在创建gradle插件,其中一项任务需要一些依赖JAR内的XML资源文件。

如何在用@TaskAction注释的方法内部访问依赖项的资源文件?

open class MyTask : DefaultTask() {

    @InputDirectory
    var projectResources: File = project.file("${project.projectDir}/src/main/resources")

    @TaskAction
    fun perform() {

        // I can iterate over the project resources files like that
        projectResources.walk().forEach { file -> 
            doStuffWithFile(file) 
        }

        // TODO but how to interate over resources files of dependencies?

    }
}

1 个答案:

答案 0 :(得分:0)

我明白了

open class MyTask : DefaultTask() {

    @InputDirectory
    var projectResources: File = project.file("${project.projectDir}/src/main/resources")

    @TaskAction
    fun perform() {

        // I can iterate over the project resources files like that
        projectResources.walk().forEach { file -> 
            doStuffWithFile(file) 
        }

        // Get the default configuration that contains the dependency jars and unzip it
        project.configurations.getAt("default")
            .filter { it.name.endsWith("jar") }
            .map { project.zipTree(it) }
            .flatten()
            .forEach { file ->
                doStuffWithFile(file)
            }
    }
}