maven-replacer-plugin和windows路径

时间:2013-06-04 04:37:06

标签: java windows maven maven-replacer-plugin

我正在尝试使用maven构建目录替换xml文件中的硬编码linux路径,以便我可以在Windows上进行测试但是当我使用maven-replacer-plugin的变量替换时,窗口反斜杠路径分隔符将被删除。有办法解决这个问题吗?

例如:

    <plugin>
      <groupId>com.google.code.maven-replacer-plugin</groupId>
      <artifactId>replacer</artifactId>
      <executions>
        <execution>
          <phase>prepare-package</phase>
          <goals>
            <goal>replace</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <filesToInclude>my_file</filesToInclude>
        <escape>true</escape>
        <replacements>
          <replacement>
            <token>/path/to/replace</token> 
            <value>${project.build.directory}</value>
          </replacement>
        </replacements>
      </configuration>
    </plugin>

结果是我获得了像“C:UsersPathNoSeparators”这样的替换值

任何线索?

2 个答案:

答案 0 :(得分:4)

替换字符串中的backslash可能会导致它将其视为转义字符,因为替换是使用正则表达式完成的。

尝试将regex属性添加到false。

<plugin>
  <groupId>com.google.code.maven-replacer-plugin</groupId>
  <artifactId>replacer</artifactId>
  <executions>
    <execution>
      <phase>prepare-package</phase>
      <goals>
        <goal>replace</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <filesToInclude>my_file</filesToInclude>
    <escape>true</escape>
    <regex>false</regex>
    <replacements>
      <replacement>
        <token>/path/to/replace</token> 
        <value>${project.build.directory}</value>
      </replacement>
    </replacements>
  </configuration>
</plugin>

请注意<regex>false</regex>配置属性。

答案 1 :(得分:1)

regex参数用于令牌匹配,对值没有影响。 maven google replacer插件存在问题。

要解决该问题,您可以使用build-helper-maven-plugin撤消project.build.directory值,然后再将其传递给maven google replacer插件。

这是一种编码解决方案:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.8</version>
    <executions>
        <execution>
            <id>regex-property</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>regex-property</goal>
            </goals>
            <configuration>
              <name>cheminCible</name>
              <value>${project.build.directory}</value>
              <regex>\\</regex>
              <replacement>/</replacement>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
  <groupId>com.google.code.maven-replacer-plugin</groupId>
  <artifactId>replacer</artifactId>
  <executions>
    <execution>
      <phase>prepare-package</phase>
      <goals>
        <goal>replace</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <filesToInclude>my_file</filesToInclude>
    <escape>true</escape>
    <replacements>
      <replacement>
        <token>/path/to/replace</token> 
        <value>cheminCible</value>
      </replacement>
    </replacements>
  </configuration>
</plugin>