在build-helper中禁用转义替换值:regex-property

时间:2016-09-05 12:19:13

标签: regex maven escaping build-helper-maven-plugin

我想基于正则表达式替换Maven中的属性。为此我使用regex-property插件。属性将包含以空格分隔的条目,我需要从每个条目创建一个xml“节点”。

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.4</version>
    <executions>
        <execution>
            <id>copy files</id>
            <phase>initialize</phase>
            <configuration>
                <tasks>
                    <copy todir="${project.basedir}/somedir">
                        ${processedPaths} <!-- THIS WILL EXPAND TO <fileset ... /> -->
                    </copy>
                </tasks>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>

然后,被替换的属性应该用于复制给定的工件:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.12</version>
    <executions>
        <execution>
            <id>regex-property</id>
            <goals>
                <goal>regex-property</goal>
            </goals>
            <configuration>
                <name>testprop</name>
                <value>${testprop}</value>
                <regex>([^\s]+)</regex>
                <replacement>&lt;fileset dir="$1" includes="*.myext" /&gt;</replacement>
                <failIfNoMatch>false</failIfNoMatch>
            </configuration>
        </execution>
    </executions>
</plugin>

我有一些几乎可行的东西:

replacement

但这里的问题是<fileset dir\="C\:\\some\\entry" includes\="*.myext" />在途中某处逃脱了。因此,结果属性将包含{{1}},这是不可取的。

这种做法看起来很糟糕,但我找不到任何其他方法可以让我从属性中指定的目录中复制文件。

1 个答案:

答案 0 :(得分:0)

我没有提到重要的事情 - 这个项目是从一个原型创建的。从原型生成项目意味着可以使用Velocity syntax。这简化了我的特定用例。 pom.xml的工作内容如下所示:

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.4</version>
    <executions>
        <execution>
            <id>copy files</id>
            <phase>initialize</phase>
            <configuration>
                <tasks>
                    <copy todir="${project.basedir}/${somedir}">
                        #foreach( $file in $filesPath.split(",") )
                        <fileset dir="$file.trim()" includes="*.myext"/>
                        #end
                    </copy>
                </tasks>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Velocity会选择#foreach指令,并会为<fileset ...媒体资源中每个以逗号分隔的条目打印$filesPath行。

archetype-metadata.xml声明:

<requiredProperty key="filesPath"/>

调用mvn archetype:generate ... "-DfilesPath=/some/path/, /other/path"将生成正确的节点:

<copy todir="${project.basedir}/${somedir}">
    <fileset dir="/some/path" includes="*.myext"/>
    <fileset dir="/other/path" includes="*.myext"/>
</copy>