为什么 Ant 在属性文件中添加一个相等的字符

Why Ant add an equal character in properties file

我有一些这样的给定属性文件:

firstvalue=1
secondvalue=hello

我使用这个 Ant 脚本来更改值:

    <propertyfile file="A.properties">
        <entry key="firstvalue" value="2" />
    </propertyfile>

执行脚本后,属性文件现在是:

#Wed, 20 May 2015 11:42:46 +0200
=
firstvalue=2
secondvalue=hello

我明白为什么 Ant 任务会添加一个带有日期的注释,但为什么 Ant 还要在第二行添加这个等于符号?

我认为这是由于编码文件引起的,因为我无法用我的所有属性文件重现该问题,只能重现其他人手动制作的一些文件。在 Ant 中使用文件之前,有没有办法避免这种行为或修复编码?

编辑: 这是我的 属性 文件,在使用在线 hexdump 工具的脚本之前看到: 文件名:A.properties mime 类型:

0000-0010:  ef bb bf 0d-0a 66 69 72-73 74 76 61-6c 75 65 3d  .....fir stvalue=
0000-0020:  31 0d 0a 73-65 63 6f 6e-64 76 61 6c-75 65 3d 68  1..secon dvalue=h
0000-0024:  65 6c 6c 6f  

                                ello

和 :

之后的同一文件
file name: A.properties
mime type: 

0000-0010:  23 54 68 75-2c 20 32 31-20 4d 61 79-20 32 30 31  #Thu,.21 .May.201
0000-0020:  35 20 31 35-3a 32 31 3a-31 32 20 2b-30 32 30 30  5.15:21: 12.+0200
0000-0030:  0d 0a ef bb-bf 3d 0d 0a-66 69 72 73-74 76 61 6c  .....=.. firstval
0000-0040:  75 65 3d 32-0d 0a 73 65-63 6f 6e 64-76 61 6c 75  ue=2..se condvalu
0000-0049:  65 3d 68 65-6c 6c 6f 0d-0a                       e=hello. .

问题出在文件前面的 UTF-8 编码字节顺序标记 (ef bb ff)。属性文件始终以 ISO8859-1 而非 UTF-8 编码,因此这些文件不是有效的属性文件。我强烈建议修复属性文件(并让其他人切换到不会重新引入问题的编辑器),但如果您需要短期解决方法,您可以先删除 BOM:

<copy file="A.properties" encoding="UTF-8" tofile="A.properties.tmp">
  <filterchain>
    <deletecharacters chars="&#xfeff;"/>
  </filterchain>
</copy>
<move file="A.properties.tmp" tofile="A.properties"/>

<propertyfile file="A.properties">
  <entry key="firstvalue" value="2" />
</propertyfile>