0

我试图在设置 outputDir 时引用分配的属性 generatorName。

尝试使用与其他任务属性(即 $buildDir)相同的语法来引用 generatorName 属性。还尝试更全面地限定属性名称 openApiGenerator.generatorName。

openApiGenerate {
    verbose = false
    generatorName = "html2"   // assignment to property
    inputSpec = "$buildDir/swagger/testing.yml".toString()
    //outputDir = "$buildDir/generated".toString()
    outputDir = "$buildDir/generated/$generatorName".toString() // fails
    apiPackage = "org.openapi.example.api"
    invokerPackage = "org.openapi.example.invoker"
    modelPackage = "org.openapi.example.model"

    // debugging code
    println "  buildDir:      $buildDir".toString()
    println "  generatorName: $generatorName".toString()  // this fails
}

调试代码的输出显示无法引用 generatorName 属性:

> Configure project :
  buildDir:      C:\Users\jgunchy\repos\testingproject\build
  generatorName: property(class java.lang.String, fixed(class java.lang.String, html2))
4

1 回答 1

1

这是一个可观察的属性,而不是字符串。您应该能够使用.get()如下方式访问基础字符串:

openApiGenerate {
    verbose = false
    generatorName = "html2"
    inputSpec = "$buildDir/swagger/testing.yml".toString()
    outputDir = "$buildDir/generated/${generatorName.get()}".toString()
    apiPackage = "org.openapi.example.api"
    invokerPackage = "org.openapi.example.invoker"
    modelPackage = "org.openapi.example.model"
}

另一种选择是直接使用配置而不是项目扩展容器的属性。例如,添加到gradle.properties

generatorName=html2

然后,您的配置将如下所示:

openApiGenerate {
    verbose = false
    generatorName = project.ext.generatorName
    inputSpec = "$buildDir/swagger/testing.yml".toString()
    outputDir = "$buildDir/${project.ext.generatorName}".toString()
    apiPackage = "org.openapi.example.api"
    invokerPackage = "org.openapi.example.invoker"
    modelPackage = "org.openapi.example.model"
}

$buildDir是 Project 实例上的 getter,其toString()方法恰好输出文件路径,这就是它行为不同的原因。

于 2019-08-24T11:40:28.650 回答