0

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

"C:\some\entry D:\another\entry"

   (processing here ... below is the content of variable after processing)

<fileset dir="C:\some\entry" includes="*.myext" />
<fileset dir="D:\another\entry" includes="*.myext" />

替换后的属性应稍后用于复制给定的工件:

<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" />,这是不希望的。

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

4

1 回答 1

0

我没有提到重要的事情 - 这个项目是从原型创建的。从原型生成项目意味着可以使用Velocity 语法。这大大简化了我的特定用例。的工作摘录如下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>

#foreach指令将被 Velocity 拾取,并为属性<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>
于 2016-09-05T16:12:05.303 回答