如何从字符串或类路径资源创建 RegularFileProperty?

Mic*_*das 5 gradle

我正在创建一个 Gradle 插件来配置另一个 Gradle 插件。一项配置是提供RegularFileProperty实例。String如果我手头只有对象,如何创建此类接口的实例?或者与我的插件捆绑在一起的资源?

我是通过或TextResource做的resources.text.fromString(...)

        project.with {
            resources.text.fromString(buildscript.classLoader.getResource(resourceName).text)
        }
Run Code Online (Sandbox Code Playgroud)

创建 的等效片段是什么RegularFileProperty?如果唯一的方法是使用临时文件,我应该如何以及在哪里创建它?

Jas*_*ite 2

以下是我如何引用打包在自定义 Gradle 插件的 jar 中的文件。我有一个test-rules.xmlSpotBugs 的通用配置,我想将其应用于任何应用我的插件的项目。

String spotBugsTestRules = getClass().getResourceAsStream("config/spotbugs/test-rules.xml").text
File spotBugsTestRulesFile = File.createTempFile("test-rules", ".xml")
spotBugsTestRulesFile.write(spotBugsTestRules)

project.tasks.matching {
    it.name.startsWith('spotbugs') && it.name.endsWith('Test')
}.all {
    it.includeFilter = spotBugsTestRulesFile
}
Run Code Online (Sandbox Code Playgroud)

此代码将资源文件读入字符串,然后将其作为临时文件输出。这是因为该includeFilter属性是 aRegularFilePropery并且需要指向文件系统上的实际文件。