1

I have a Maven build and i'd like to build some property based on files found in the resources.

Concretely, I'd like to build a ${builtinLocales} variable at the "process-resources" phase based the resources found. I can then incorporate it in some application property file.

E.g. if 2 files "labels_en.properties" and "labels_de.properties" are found, that variable should return "en;de" or "labels_en;labels_de".

The ultimate goal is to present to the users the available languages without having to, at run time, parse the full jar for seek after "^labels_(\w+)\.properties$" files.

Anyway to do this ?

lvr123
  • 524
  • 6
  • 24
  • 1
    I suspect this is a duplicate of https://stackoverflow.com/questions/13636378/using-maven-replacer-plugin-to-list-files-in-a-folder – flup Oct 30 '22 at 22:51
  • Thanks for me pointing to me this solution. It is working nicely. You can post it as solution, if you want the Bounty ;-) – lvr123 Oct 31 '22 at 14:55
  • 1
    This is a bit of a duplicate from [This Example](https://stackoverflow.com/questions/13636378/using-maven-replacer-plugin-to-list-files-in-a-folder) – Robert Bradley Oct 31 '22 at 18:04

1 Answers1

0

Based on the answers to Using maven replacer plugin to list files in a folder and Exporting Maven properties from Ant code, I came up with this solution:

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
        <execution>
            <id>list-locales</id>
            <phase>generate-resources</phase>
            <configuration>
                <target>
                    <fileset id="my-fileset" dir="src/main/resources">
                        <filename regex="[^/]+_.+.properties"/>
                    </fileset>
                    <pathconvert targetos="unix" pathsep=";"
                                 property="project.locales" refid="my-fileset">
                        <map from="${basedir}\src\main\resources\" to="" />
                        <regexpmapper from="[^/]+?_(.+).properties" to="\1"/>
                    </pathconvert>
                </target>
            <exportAntProperties>true</exportAntProperties>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>ant-contrib</groupId>
            <artifactId>ant-contrib</artifactId>
            <version>1.0b3</version>
            <exclusions>
                <exclusion>
                    <groupId>ant</groupId>
                    <artifactId>ant</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
</plugin>

The solution relies on :

  1. Building a Ant property "project.locales" based on the files found
  2. Exposing this Ant property to Maven with <exportAntProperties>true</exportAntProperties>
  3. Using the the Maven property ${project.locales} in any file where required.
lvr123
  • 524
  • 6
  • 24