@SpringBootApplication
@PropertySource({"classpath:${properties.file}"})
public class AppConfiguration {
}

我需要传递给spring“properties.file”属性,该属性在基于任务图的gradle构建中动态更改。

最佳答案

ext {
    dynamicResources = "$buildDir/dynamicResources"
    prop1 = 'value1'
    prop2 = 'value2'
}
dependencies {
    runtime files(dynamicResources)
}
task createDynamicResources {
    inputs.property 'prop1', prop1
    inputs.property 'prop2', prop2
    outputs.dir dynamicResources
    doLast {
        mkdir dynamicResources
        file("$dynamicResources/foo.properties").text = """
prop1=${prop1}
prop2=${prop2}
        """
    }
}
bootRun.dependsOn createDynamicResources

现在foo.properties将位于bootRun的运行时类路径上

参见here类似的模式

10-08 20:21