自动属性扩展:使用带有转义字符的Gradle

huangapple go评论74阅读模式
英文:

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&lt;ProcessResources&gt;(&quot;processResources&quot;) {
        outputs.upToDateWhen { false }

        filesMatching(&quot;**/application.yml&quot;) {
            filter {
                it.replace(&quot;#project.version#&quot;, version as String)
            }
            filter {
                it.replace(&quot;#spring.profiles.active#&quot;, profiles)
            }
        }
    }
}

And of course #spring.profiles.active# is a string you want to replace in your file.

huangapple
  • 本文由 发表于 2020年4月5日 07:58:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/61036350.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定