Search code examples
mavenmaven-archetype

Maven: passing requiredProperty values in archetype-metadata.xml through properties or yaml file


I want to generate maven archetypes by using custom properties through archetype-metadata.xml's requiredProperty -

<requiredProperties>
    <requiredProperty key="proxy-name">
        <defaultValue>${proxy.name}</defaultValue>
    </requiredProperty>
    <requiredProperty key="proxy-desc">
        <defaultValue>${proxy.description}</defaultValue>
    </requiredProperty>
</requiredProperties>

However, my requirement is to initialize these requiredProperty values using key:value pairs provided either in a properties file or a yaml file, so that I can get these values injected in pom.xml of custom project structure under archetype-resources.

  <properties>
    <proxy-name>${proxy.name}</proxy-name>
    <proxy-desc>>${proxy.description}</proxy-desc>
  </properties>

I do not want to provide the values to these properties via command line or by providing default values. I want the initialization of these property values dynamic based on reading an external properties file when I run the mvn archetype:generate command.

Is this even possible? My apologies in advance if the question seems too vague or really elementary. This is my first experience dealing with custom maven archetypes.

P.S - I have tried using the yaml-properties-maven-plugin, however the values still don't get populated in archetype-resources pom.xml, which normally takes values when initializing property values via command line.


Solution

  • So, to answer my own question and to help anyone who is into the same problem:

    What I did, is to also include an archetype.xml in META-INF/maven which takes in the resource properties file name to use to substitute custom values in the archetype-metadata.xml. Here's how the archetype.xml looks:

    <?xml version="1.0" encoding="UTF-8"?>
    <archetype>
        <id>quickstart-archetype</id>
        <sources/>
        <resources>
            <resource>archetype.properties</resource>
        </resources>
    </archetype>
    

    This way the yaml-maven-properties plugin reads the yaml file and writes an archetype.properties file in the src/main/resources folder which also contains the archetype-resources folder structure for archetype generation.

    Let me know in case anyone needs more clarification on how I achieved this.