英文:
Automatic Property Expansion Using Gradle with escape characters
问题
我在Gradle中使用属性展开。所有的部分都正常工作,但文件路径有问题。
我在Gradle中使用processResources
任务:
processResources {
filesMatching('**/application.properties') {
expand project.properties
}
}
在我的Spring application.properties
文件中,我有一个属性如下:
root.location = ${rootDir}
在我的build.gradle
文件中,我定义了如下内容:
ext['rootDir'] = rootProject.buildDir.path + File.separator + "tmp"
我在application.properties
中得到的结果是:
root.location = D:\Projects\myproject\build\tmp
在我的Spring类中,当我执行以下代码:
@Value("${cpm.repository.root.location}") String rootLocation;
我得到的结果是D:Projectsmyprojectbuild\tmp
。
我希望属性展开后的结果是:
root.location = D:\\projects\\myproject\\build\\tmp
这将导致D:\projects\myproject\build\tmp
。
我做错了什么?展开确实按预期工作,只是在展开时路径中的\
没有被转义。
提前感谢您的帮助。
英文:
I'm using property expansion in Gradle. All works fine but for file-paths.
I am using the processResources task in Gradle
processResources {
filesMatching ('**/application.properties') {
expand project.properties
}
}
I have a property in my Spring application.properties
as follows:
root.location = ${rootDir}
In my build.gradle
I have defined the following:
ext['rootDir'] = rootProject.buildDir.path + File.separator + "tmp"
Result I get in application.properties
is
root.location = D:\Projects\myproject\build\tmp
Which turns into D:Projectsmyprojectbuild\tmp in my Spring class when doing:
@Value("${cpm.repository.root.location}") String rootLocation;
I need the property to be expanded to:
root.location = D:\\projects\\myproject\\build\\tmp
Because that would result in D:\projects\myproject\build\tmp
What am I doing wrong? Expansion does work as intended, it's just that the '' in the path are not escapped when expanded.
Thanks in advance
答案1
得分: 1
我认为你应该使用processResources
任务来修改你的资源。
我们是这样使用的(使用application.yml
和Gradle Kotlin DSL):
tasks {
named<ProcessResources>("processResources") {
outputs.upToDateWhen { false }
filesMatching("**/application.yml") {
filter {
it.replace("#project.version#", version as String)
}
filter {
it.replace("#spring.profiles.active#", profiles)
}
}
}
}
当然,#spring.profiles.active#
是你想要在文件中替换的字符串。
英文:
I think you should use processResources
task to modify your resources.
We are using it that way (with application.yml
and Gradle Kotlin DSL):
tasks {
named<ProcessResources>("processResources") {
outputs.upToDateWhen { false }
filesMatching("**/application.yml") {
filter {
it.replace("#project.version#", version as String)
}
filter {
it.replace("#spring.profiles.active#", profiles)
}
}
}
}
And of course #spring.profiles.active#
is a string you want to replace in your file.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论