5

I'd like to use the maven-replacer-plugin to replace $file-list$ in a file with a comma separated list of files in a folder of my project. Is this possible?

thanks

Gootik
  • 526
  • 1
  • 7
  • 18
  • The replacer plugin supports replacements with regular expression, maven-resources-plugin support replacements with placeholders like ${...}. What would you like to ? – khmarbaise Nov 30 '12 at 08:32

1 Answers1

14

Here is what I did :

using the antrun plugin create a temp file with the list in it :

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.7</version>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <configuration>
                <target>
                    <fileset id="my-fileset" dir="src/main/resources" />
                    <pathconvert targetos="unix" pathsep=","
                        property="my-file-list" refid="my-fileset">
                        <map from="${basedir}\src\main\resources\" to="" />
                    </pathconvert>
                    <echo file="${basedir}\target\file-list.txt">${my-file-list}</echo>
                </target>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>

then using the replacer plugin I replace the list in the file that I want :

<plugin>
    <groupId>com.google.code.maven-replacer-plugin</groupId>
    <artifactId>maven-replacer-plugin</artifactId>
    <executions>
        <execution>
            <id>replace-file-list</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>replace</goal>
            </goals>
            <configuration>
                <ignoreMissingFile>false</ignoreMissingFile>
                <file>target/MY_FILE.TXT</file>
                <regex>false</regex>
                <token>$file-list$</token>
                <valueFile>target/file-list.txt</valueFile>
            </configuration>
        </execution>
    </executions>
</plugin>

I'm sure there must be a better way, but hope this helps someone.

Gootik
  • 526
  • 1
  • 7
  • 18
  • This page suggests the “right” way isn't really any prettier: https://maven.apache.org/plugin-developers/cookbook/add-build-time-to-manifest.html – Luke Maurer Aug 07 '17 at 16:58
  • You can actually remove the use of the `maven-replacer-plugin` by exporting the maven property to maven directly (see [Exporting Maven properties from Ant code](https://stackoverflow.com/a/19456328/2398993) ) and then use `${file-list}` wherever needed. – lvr123 Nov 01 '22 at 12:34