【问题标题】:AntBuilder ant echo task to append text to a fileAntBuilder ant echo 任务将文本附加到文件
【发布时间】:2014-01-29 10:22:11
【问题描述】:

我在一个 groovy 文件中运行 ant 任务,下面是它的 sn-p:

def user="USER"
project.ant.echo(file:"/a/b/c/test.properties",  
message:"user=${user}", append="true")

我试图通过多次运行脚本来将文本附加到文件中。但是它没有按预期工作。以下是当前输出:

cat test.properties
user=USERtrue

如果使用 append 运行 groovy 文件和 ant 任务,该文件将被覆盖并在最后设置 true。 如果在没有附加的情况下运行 groovy 文件和 ant 任务,则文件会被简单地覆盖。

不知道这里出了什么问题。任何帮助表示赞赏。

【问题讨论】:

    标签: ant groovy antbuilder


    【解决方案1】:

    你试过了吗:

    project.ant.echo( file:"/a/b/c/test.properties",
                      message:"user=${user}",
                      append:true )
    

    即:使用append:true,而不是append="true"

    【讨论】:

    • 不敢相信我放了 = 而不是 :。为失明而道歉。但是,上面的代码附加到文件的同一行,而不是新行。我已经在下面完成了每次将文本附加到新行的操作。 project.ant.echo( file:"/a/b/c/test.properties", message:"\n user=${user}", append:true ) 有没有更好的方法来实现它?
    • @user1470220 我不知道原始文件末尾是否没有换行符:-(
    • 感谢蒂姆的帮助
    • @user1470220 你能accept the comment as an answer 解决问题吗?希望它做到了:-)
    【解决方案2】:

    ant.concat 可以是另一种选择(少给一个键:)。

    project.ant.concat(destfile:"/some/prop.file", append: true, "user=${user}") 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-22
      • 2018-05-05
      • 1970-01-01
      相关资源
      最近更新 更多