如何将 log4j2.xml 替换为 Gradle?
How to replace in log4j2.xml with Gradle?
我想在构建过程中用 Gradle 替换 log4j2.xml 中的一个值。我找到了一种方法:
task reaplaceInLogFile {
String apiSuffix = System.properties['apiSuffix'] ?: ''
println "In my task"
String contents = file('src/main/resources/log4j2.xml').getText( 'UTF-8' )
println "File found"
contents = contents.replaceAll( "svc0022_operations", "svc0022_operations${apiSuffix}")
new File( 'src/main/resources/log4j2.xml' ).write( contents, 'UTF-8' )
}
但是,这也会永久更改源文件,我不想那样做。我想更改仅包含在构建 zip 中的 log4j2.xml。我知道我可以使用这样的东西:
tasks.withType(com.mulesoft.build.MuleZip) { task ->
String apiSuffix = System.properties['apiSuffix'] ?: ''
task.eachFile {
println name
if (name == 'mule-app.properties') {
println "Expanding properties for API Version suffix: ${apiSuffix}"
filter { String line ->
line.startsWith("${serviceId}.api.suffix") ? "${serviceId}.api.suffix=${apiSuffix}" : line
}
}
但是我不知道log4j2文件的类型是什么。如果有其他方法可以做到这一点,我将不胜感激!
我们正在使用 Mule gradle 插件。
类型不是 log4j2 文件的类型,而是创建 ZIP 的任务类型或 log4j2 文件打包到的任何位置。如果 log4j2 文件包含在由 MuleZip 任务生成的 ZIP 中,那么您可以简单地为 log4j2 文件添加另一个 if
-分支。
但实际上,最好只编辑将 log4j2 文件打包到某个存档中的具体任务,而不是所有相同类型的任务。
除此之外,我认为您应该能够使用 filesMatching
而不是 eachFile
和 if
。
我想在构建过程中用 Gradle 替换 log4j2.xml 中的一个值。我找到了一种方法:
task reaplaceInLogFile {
String apiSuffix = System.properties['apiSuffix'] ?: ''
println "In my task"
String contents = file('src/main/resources/log4j2.xml').getText( 'UTF-8' )
println "File found"
contents = contents.replaceAll( "svc0022_operations", "svc0022_operations${apiSuffix}")
new File( 'src/main/resources/log4j2.xml' ).write( contents, 'UTF-8' )
}
但是,这也会永久更改源文件,我不想那样做。我想更改仅包含在构建 zip 中的 log4j2.xml。我知道我可以使用这样的东西:
tasks.withType(com.mulesoft.build.MuleZip) { task ->
String apiSuffix = System.properties['apiSuffix'] ?: ''
task.eachFile {
println name
if (name == 'mule-app.properties') {
println "Expanding properties for API Version suffix: ${apiSuffix}"
filter { String line ->
line.startsWith("${serviceId}.api.suffix") ? "${serviceId}.api.suffix=${apiSuffix}" : line
}
}
但是我不知道log4j2文件的类型是什么。如果有其他方法可以做到这一点,我将不胜感激!
我们正在使用 Mule gradle 插件。
类型不是 log4j2 文件的类型,而是创建 ZIP 的任务类型或 log4j2 文件打包到的任何位置。如果 log4j2 文件包含在由 MuleZip 任务生成的 ZIP 中,那么您可以简单地为 log4j2 文件添加另一个 if
-分支。
但实际上,最好只编辑将 log4j2 文件打包到某个存档中的具体任务,而不是所有相同类型的任务。
除此之外,我认为您应该能够使用 filesMatching
而不是 eachFile
和 if
。