0

I want to replace a property in Maven based on a regex. For that I am using the regex-property plugin. Property will contain space-separated entries and I need to create a xml "node" from each of them.

"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" />

The replaced property then should be later used to copy given artifacts:

<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>

I have something that almost works:

<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>

But the problem here is that the replacement is escaped somewhere along the way. So the resulting property would contain <fileset dir\="C\:\\some\\entry" includes\="*.myext" />, which is not desired.

This approach does seem hackish, but I could not find any other way that would allow me to copy files from directories specified in a property.

A_Di-Matteo
  • 26,902
  • 7
  • 94
  • 128
Martin Melka
  • 7,177
  • 16
  • 79
  • 138

1 Answers1

0

I did not mention an important thing - this project is being created from an archetype. Generating a project from an archetype means one can use Velocity syntax. This simplifies my particular usecase quite a bit. The working exerpt of pom.xml looks like this:

<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>

The #foreach directive will be picked up by Velocity and will print out a <fileset ... line for each comma-separated entry in the $filesPath property.

And in archetype-metadata.xml is declared:

<requiredProperty key="filesPath"/>

Calling mvn archetype:generate ... "-DfilesPath=/some/path/, /other/path" will then generate the correct nodes:

<copy todir="${project.basedir}/${somedir}">
    <fileset dir="/some/path" includes="*.myext"/>
    <fileset dir="/other/path" includes="*.myext"/>
</copy>
Martin Melka
  • 7,177
  • 16
  • 79
  • 138