2016-08-02 133 views
0

我想在构建过程中用Gradle替换log4j2.xml中的值。我找到了一种方法:如何使用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插件。

回答

1

该类型不是log4j2文件的类型,而是创建ZIP的任务的类型或您的log4j2文件打包到的任何位置。如果log4j2文件包含在由MuleZip任务生成的ZIP中,那么您可以简单地为log4j2文件添加另一个if -branch。

但实际上,编辑将log4j2文件打包到某个归档文件而不是相同类型的所有任务的具体任务可能会更好。

除此之外,你应该能够使用filesMatching而不是eachFileif我认为。

+0

谢谢你的回答。不幸的是,log4j2.xml不在MuleZip任务的集合中。这是任务代码https://github.com/mulesoft-labs/mule-gradle-plugin/blob/master/src/main/groovy/com/mulesoft/build/MuleZip.groovy –

+0

正如我所说的。修改包装'log4j2.xml'文件的任务。我需要看到你的'build.gradle'和'log4j2.xml'文件的路径来建议一个具体的任务来修改。 – Vampire

+0

我们的log4j2.xml标准地位于“src \ main \ resources \ log4j2.xml”下面这是我们的build.gradle:https://www.dropbox.com/s/iuv4j9htpseyed4/build.gradle?dl=0 –