xml 如何在 maven pom 属性值中转义“&”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8701870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 15:05:45  来源:igfitidea点击:

how to escape '&' in maven pom property values

xmlmavenpom.xml

提问by dermoritz

I need to set a URL via a maven property (to be replaced per profile). The problem is that this URL contains some &and maven doesn't like this (if URL contains &_program):

我需要通过 Maven 属性设置 URL(按配置文件替换)。问题是这个 URL 包含一些&而 maven 不喜欢这个(如果 URL 包含 &_program):

"The reference to entity "_program" must end with the ';' delimiter." error occurs in pom on this line.

“对实体“_program”的引用必须以';'结尾 分隔符。” 在这一行的 pom 中发生错误。

So how to escape this character or how to escape a hole line that might contain some "special" characters. (What other characters are forbidden in property-values?)

那么如何转义这个字符或如何转义可能包含一些“特殊”字符的孔线。(属性值中禁止使用哪些其他字符?)

回答by mikey

I've not used maven but from the sounds of it, standard XML escaping would get the job done. In your case you can try using

我没有使用过 maven,但从它的声音来看,标准的 XML 转义可以完成工作。在您的情况下,您可以尝试使用

&instead of &

&代替 &

What characters do I need to escape in XML documents?

我需要在 XML 文档中转义哪些字符?

回答by Tomasz Szymulewski

You can escape one character or a whole line that might contain some "special" characters by surrounding property value with <![CDATA[ property value ]]>. For example for some URL it would look like:

您可以通过将属性值括在<![CDATA[ property value ]]>. 例如,对于某些 URL,它看起来像:

<properties>
    <some.url><![CDATA[http://stackoverflow.com?param_1=1&param_2=2]]></some.url>
</properties>