Ant的echo-task脚本换行书写 如何保证输出内容无多余换行?
解决方案
完全可以实现,以下是两种无需使用反斜杠转义的可行方案:
方案1:使用filterchain过滤换行符
直接在<echo>标签内添加striplinebreaks过滤器,会自动移除所有输出内容中的换行符,脚本内可以随意换行拆分长文本:
<echo file="my.log"> <filterchain> <striplinebreaks/> <!-- 如需同时移除首尾多余空格,可额外添加下面一行 --> <!-- <trim/> --> </filterchain> Really loooong line, that needs to appear without line breaks in the output, but which I'd still like to wrap in the ant-script </echo>
说明:striplinebreaks仅会删除\r、\n类换行符,不会影响文本中间的空格,完全保留原内容的字符结构。
方案2:属性拆分拼接
将长字符串拆分为多个独立属性分段定义,脚本中可以每段单独占一行,最终输出时拼接即可:
<!-- 分段定义长字符串,每行可以单独加注释说明片段含义 --> <property name="long_content.part1" value="Really loooong line, that needs to appear without line breaks in the output,"/> <property name="long_content.part2" value=" but which I'd still like to wrap in the ant-script"/> <!-- 输出时拼接所有片段,无多余字符 --> <echo file="my.log">${long_content.part1}${long_content.part2}</echo>
说明:该方案适合内容逻辑明确、需要拆分注释的场景,可读性更高,拼接逻辑完全可控。
内容的提问来源于stack exchange,提问作者Mikhail T.
相关产品推荐
相关产品推荐

