Issue
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 ?
Solution
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 :
- Building a Ant property "project.locales" based on the files found
- Exposing this Ant property to Maven with
<exportAntProperties>true</exportAntProperties>
- Using the the Maven property
${project.locales}
in any file where required.
Answered By - lvr123
Answer Checked By - Marie Seifert (JavaFixing Admin)