【问题标题】:gradle script to escape single quote before buildgradle脚本在构建之前转义单引号
【发布时间】:2023-02-11 01:12:37
【问题描述】:

我有带翻译的 i18n 消息文件。问题在于某些语言(例如意大利语)在某些单词中使用单引号。我想运行一些脚本(可能作为 gradle.build 任务)用双引号替换单引号。我将以下代码粘贴到我的build.gradle.kts 文件中

task("replaceSingleQuotes") {
    doLast {
        ant.ReplaceRegExp(match:'\'', replace:'\'\'', flags:'g', byline:false) {
            fileset(dir: 'src/main/resources/i18n/', includes: '*')
        }
    }
}

第一个问题是现在我的项目配置失败并出现以下错误:

e: build.gradle.kts:139:32: Expecting ')'e: D:\Sources\delivery-backend\build.gradle.kts:139:33: Unexpected tokens (use ';' to separate expressions on the same line)
e: build.gradle.kts:140:15: Expecting ')'
e: build.gradle.kts:140:17: Unexpected tokens (use ';' to separate expressions on the same line)
e: build.gradle.kts:154:13: Expecting an element
e: build.gradle.kts:155:28: Expecting ')'
e: build.gradle.kts:155:29: Unexpected tokens (use ';' to separate expressions on the same line)
e: build.gradle.kts:156:20: Expecting ')'
e: build.gradle.kts:156:22: Unexpected tokens (use ';' to separate expressions on the same line)
e: build.gradle.kts:139:13: Unresolved reference: ReplaceRegExp
e: build.gradle.kts:139:27: Unresolved reference: match
e: build.gradle.kts:140:4: Unresolved reference: fileset
e: build.gradle.kts:140:12: Function invocation 'dir(...)' expected
e: build.gradle.kts:140:12: Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: 
public inline fun SourceSetOutput.dir(dir: Any, vararg options: Pair<String, Any?>): Unit defined in org.gradle.kotlin.dsl
e: build.gradle.kts:154:1: Function invocation 'task(...)' expected
e: build.gradle.kts:154:1: None of the following functions can be called with the arguments supplied: 
public abstract fun task(p0: String!): Task! defined in org.gradle.api.Project
public abstract fun task(p0: String!, p1: Closure<(raw) Any!>!): Task! defined in org.gradle.api.Project
public abstract fun task(p0: String!, p1: Action<in Task!>!): Task! defined in org.gradle.api.Project
public abstract fun task(p0: (Mutable)Map<String!, *>!, p1: String!): Task! defined in org.gradle.api.Project
public abstract fun task(p0: (Mutable)Map<String!, *>!, p1: String!, p2: Closure<(raw) Any!>!): Task! defined in org.gradle.api.Project
e: build.gradle.kts:155:9: Unresolved reference: replaceregexp
e: build.gradle.kts:155:23: Unresolved reference: match
e: build.gradle.kts:156:9: Unresolved reference: fileset
e: build.gradle.kts:156:17: Function invocation 'dir(...)' expected
e: build.gradle.kts:156:17: Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: 
public inline fun SourceSetOutput.dir(dir: Any, vararg options: Pair<String, Any?>): Unit defined in org.gradle.kotlin.dsl

我想,第二个问题是,每次执行我的任务时,它都会添加越来越多的双引号。 因此,这里有两个问题:如何让我的项目配置此任务以及如何更改正则表达式以仅匹配单引号?

【问题讨论】:

  • 需要考虑的一件事是您可能不想编辑源目录中的文件。这是您获得双引号的原因之一。一个更好的主意可能是处理将文件复制到构建目录并在将它们发送到构建目录的过程中进行更改的任务。

标签: spring kotlin gradle groovy resourcebundle


【解决方案1】:

使用匹配“([^\])'”并替换“\\'”应该有效

正如这里所建议的https://stackoverflow.com/a/60127123

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-27
    • 2014-05-30
    相关资源
    最近更新 更多