尝试使用 XPath 用 Maven 替换插件替换 web.xml 时出现以下错误
[ERROR] Failed to execute goal com.google.code.maven-replacer-plugin:replacer:1.5.2:replace (default) on project my-project:
Error during XML replacement: Content is not allowed in prolog. -> [Help 1]
这是 Maven 代码
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.2</version>
<executions>
<execution>
<phase>prepare-package</phase> <goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<file>src/main/webapp/WEB-INF/web.xml</file>
<outputFile>target/web.xml</outputFile>
<regexFlags>
<regexFlag>CASE_INSENSITIVE</regexFlag>
</regexFlags>
<replacements>
<replacement>
<xpath>/web-app/display-name</xpath>
<token>.*</token>
<value>${project.name}</value>
</replacement>
</replacements>
</plugin>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="my-project"
version="2.5">
<display-name>My Project</display-name>
...
</web-app>
知道为什么吗?
最佳答案
您需要注册“http://java.sun.com/xml/ns/javaee ”命名空间,并选择一个前缀,比如“javaee”,然后使用 /javaee:web-app/javaee:display-name
作为您的 XPath 表达式。
根据这个link ,您可以在插件的 <configuration>
中执行此操作部分:
<plugin>
...
<configuration>
<namespaceContexts>
<javaee>http://java.sun.com/xml/ns/javaee</javaee>
</namespaceContexts>
...
关于maven - Maven 替换插件中的 XPath 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18600544/