我最初有这个:
<echo file="${my.file}"
message="This is line #1"/>
<echo file="${my.file}"
append="true"
message="This is line #2"/>
<echo file="${my.file}"
append="true"
message="This is line #3"/>
并在我的文件中得到了这个: This is line#1This is line #2This is line#3
所以,我试过: <echo file="${my.file}">
This is line #1
This is line #2
This is line #3
</echo>
并得到: This is line#1This is line #2This is line#3
我这样做了: <echo file="${my.file}">
This is line #1${line.separator}
This is line #2${line.separator}
This is line #3${line.separator}
</echo>
并得到: This is line#1This is line #2This is line#3
我这样做了: <echo file="${my.file}">
This is line #1
This is line #2
This is line #3
</echo>
并得到: This is line#1This is line #2This is line#3
我什至试过这个: <concat destfile="${my.file}">
This is line #1
This is line #2
This is line #3
</concat>
并且仍然得到: This is line#1This is line #2This is line#3
如果我对控制台执行任何这些操作,它会在单独的行上打印。将它们保存到文件中,行不会显示。我在 Win7,Ant 1.8 上。
有任何想法吗?
window 的东西?
我已经在我的 Mac 上尝试过这些。第一个给了我相同的结果。但是,第二种方式在文件中给了我三行。添加
${line.separator}
的第三种方式每隔一行跳过一次。使用 &10;
的第四种方式给了我每行最后一个 ^M
(毕竟,Mac 是 Unix 并且不使用 CRLF 结尾,而只是 LF)。并且,使用
<concat>
还创建了单独的行。更多信息
我已将问题追溯到我的
build.xml
文件中的以下代码段:这就像宣传的那样工作。也就是说,空白和
NL
出现在结果文件中:<target name="package"
depends="-resolve,compile"
description="Packaging of the artifact">
<!-- Build version.txt and let the user figure out how to use it -->
<mkdir dir="${target.dir}"/>
<echo file="${version.file}">
Jenkins Project: ${env.JOB_NAME}
Jenkins Build Number: ${env.BUILD_NUMBER}
Build Date: ${build.date}
</echo>
<!--
<antcall target="jar"/>
<antcall target="war"/>
<antcall target="ear"/>
-->
</target>
这不会:<target name="package"
depends="-resolve,compile"
description="Packaging of the artifact">
<!-- Build version.txt and let the user figure out how to use it -->
<mkdir dir="${target.dir}"/>
<echo file="${version.file}">
Jenkins Project: ${env.JOB_NAME}
Jenkins Build Number: ${env.BUILD_NUMBER}
Build Date: ${build.date}
</echo>
<antcall target="jar"/>
<antcall target="war"/>
<antcall target="ear"/>
</target>
这些片段之间的唯一区别在于我在编写文件后执行了三个 <antcall>
任务。三个 antcall 任务都有一个 if
子句,因此它们可能会或可能不会根据设置的属性运行。这是我们开发团队的构建模板,所以我真的很想让它工作。为什么
<antcall>
会引起问题。顺便说一句,有时它太坏了,我需要重新打开一个新的控制台窗 Eloquent 能使 NL
东西工作。我以前遇到过类似的问题,这是一个 Windows 代码页问题。
最佳答案
下面的 Ant 项目使用 echo
任务创建了四个文件:
当项目在 Windows 7 上使用 Ant 1.8.x 运行时,每个文件的 hex View 如下:
// echo1.txt
// HEX // TEXT
6c 69 6e 65 20 23 31 line #1
6c 69 6e 65 20 23 32 line #2
6c 69 6e 65 20 23 33 line #3
// echo2.txt
// HEX // TEXT
6c 69 6e 65 20 23 31 0a line #1 (0a => line feed)
6c 69 6e 65 20 23 32 0a line #2
6c 69 6e 65 20 23 33 0a line #3
// echo3.txt
// HEX // TEXT
6c 69 6e 65 20 23 31 0d 0a 0a line #1 (0d => carriage return)
6c 69 6e 65 20 23 32 0d 0a 0a line #2
6c 69 6e 65 20 23 33 0d 0a 0a line #3
// echo4.txt
// HEX // TEXT
6c 69 6e 65 20 23 31 0d 0a line #1
6c 69 6e 65 20 23 32 0d 0a line #2
6c 69 6e 65 20 23 33 0d 0a line #3
只有 echo4.txt [将所有三行放在由
message
分隔的 ${line.separator}
属性中] 会在 Windows 上生成正确的行结尾(回车 + 换行)。在记事本(使用回车 + 换行)以及保留 Unix 行尾(单换行)的编辑器中编辑项目文件时,输出是相同的。看起来当 Ant 解析 XML 文件时,行尾被规范化为单个换行符。Ant 计划
<?xml version="1.0" encoding="UTF-8"?>
<project name="echo-project" basedir="." default="echo-all">
<target name="echo-test1">
<property name="echo1.file" location="${basedir}/echo1.txt" />
<echo file="${echo1.file}" message="line #1" />
<echo file="${echo1.file}" message="line #2" append="true" />
<echo file="${echo1.file}" message="line #3" append="true" />
</target>
<target name="echo-test2">
<property name="echo2.file" location="${basedir}/echo2.txt" />
<echo file="${echo2.file}">line #1
line #2
line #3
</echo>
</target>
<target name="echo-test3">
<property name="echo3.file" location="${basedir}/echo3.txt" />
<echo file="${echo3.file}">line #1${line.separator}
line #2${line.separator}
line #3${line.separator}
</echo>
</target>
<target name="echo-test4">
<property name="echo4.file" location="${basedir}/echo4.txt" />
<echo file="${echo4.file}" message=
"line #1${line.separator}line #2${line.separator}line #3${line.separator}" />
</target>
<target name="echo-all"
depends="echo-test1, echo-test2, echo-test3, echo-test4" />
</project>