Maven替代品:包含美元符号的替代价值 [英] Maven replacer: replacement value containing dollar signs

查看:195
本文介绍了Maven替代品:包含美元符号的替代价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个Maven脚本,在该脚本中我必须修改一些文件内容.我目前正在使用替换插件,当替换值包含美元符号时,这会给我带来麻烦.

I am dealing with a Maven script where I have to modify some file content. I am currently using the replacer plugin, which gives me trouble when the replacement value contains dollar signs.

我遇到问题的替换相对简单:在我的log4j.xml中,将<param name="File" value="wat.log" />行替换为<param name="File" value="${FOO_BAR}/wat.log" />

The replacement I have problems with is relatively simple: in my log4j.xml, replace the line <param name="File" value="wat.log" />with <param name="File" value="${FOO_BAR}/wat.log" />

我知道,Maven这样写,会将${FOO_BAR}解释为属性.我查找了解决方案并尝试了.当我只使用${FOO}

I know that, written like that, Maven would interpret ${FOO_BAR} as property. I looked up a solution and tried it. When I just use ${FOO}

<properties>
    <dollar>$</dollar>
    <foo>{FOO_BAR}</foo>
    <dollar.foo>${dollar}${foo}</dollar.foo>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>com.google.code.maven-replacer-plugin</groupId>
            <artifactId>replacer</artifactId>
            <version>1.5.3</version>
            <executions>
                <execution>
                    <id>configure-logging</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>replace</goal>
                    </goals>
                    <configuration>
                        <includes>
                            <include>${my.configDir}/log4j.xml</include>
                        </includes>
                        <replacements>
                            <replacement>
                                <token>value="wat.log"</token>
                                <value>value="${dollar.foo}/wat.log"</value>
                            </replacement>
                        </replacements>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

结果是错误named capturing group is missing trailing '}'.据我了解,该插件使用通常的Java正则表达式替换,该替换解释了替换文本中的美元符号和小标签,以捕获正则表达式中的组.

The result is an error named capturing group is missing trailing '}'. As I understand it, the plugin uses the usual Java regex replacement which interprets dollar signs and curlies in the replacement text for capturing groups in the regex.

我尝试了其他一些操作,在这种情况下,具体的错误似乎是由于下划线引起的.如果将foo属性更改为{FOOBAR},则错误更改为:No group with name {FOOBAR}.

I tried a few other things, and it seems the specific error in this case is due to the underscore. If I change the foo property to {FOOBAR}, the error changes: No group with name {FOOBAR}.

我也尝试了其他一些事情:

I tried a few other things as well:

  • foo属性更改为{foo},我没有收到错误,但是替换掉了$,即我得到了value="{foo}/wat.log"-用FOO_BAR再次替换foo ,但我仍然想念美元符号
  • 将属性更改为{dollar}会给我一个Illegal group reference错误
  • 以几种不同的方式(例如$$\$\\$)对美元,大括号和/或下划线进行转义并没有给我带来更多的好处,也没有使用unicode或$作为美元符号.
  • changing the foo property to {foo}, I don't get an error, but the replacement drops the $, i.e. I get value="{foo}/wat.log" - a second replacement of foo with FOO_BAR works, but I still am missing the dollar sign
  • changing the property to {dollar} gives me an Illegal group reference error
  • escaping the dollar, braces and/or underscores in several different ways (e.g. $$, \$, \\$) did not bring me any further, neither did using unicode or $ for the dollar sign.

有没有一种方法可以用Maven实际解决呢?我很乐意使用更多的属性,更多的替换或完全不同的插件.

Is there a way to actually solve this with Maven? I'd be happy to use more properties, more replacements or an entirely different plugin.

更新:我正在Windows上工作-不确定是否/如何影响结果.

Update: I am working on Windows - not sure if/how that affects the results.

推荐答案

我自己找到了答案,寻找与反斜杠有关的另一个问题: 将<regex>false</regex>添加到replacer插件的配置中,然后替换器将替换它看到的纯文本,并且${dollar.foo}技巧将按预期工作:

I found the answer myself, looking for another problem related to backslashes: Add <regex>false</regex> to the configuration of the replacer plugin, then the replacer will just replace the plain text it sees and the ${dollar.foo} trick works as intended:

<properties>
    <dollar>$</dollar>
    <foo>{FOO_BAR}</foo>
    <dollar.foo>${dollar}${foo}</dollar.foo>
</properties>

...
                    <configuration>
                        <includes>
                            <include>${my.configDir}/log4j.xml</include>
                        </includes>


                        <!-- don't treat token/value as regular expressions -->
                        <regex>false</regex>


                        <replacements>
                            <replacement>
                                <token>value="wat.log"</token>
                                <value>value="${dollar.foo}/wat.log"</value>
                            </replacement>
                        </replacements>
                    </configuration>
...

这篇关于Maven替代品:包含美元符号的替代价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆