custom maven archetype: default value for version not used

2.1k views Asked by At

In my maven archetype definition, I would like to set the default version to 0.0.1-SNAPSHOT when the user doesnt enter anything. However, when I create a project based on the archetype, I am always prompted for the version and the default is 1.0-SNAPSHOT.

How do I do this correctly?

I defined a version property in the archetype.properties file and when I compile the archetype, it says the correct version and when I change the value in the archetype.properties, I can see that the console output changes accordingly.

Just on creating a project based on the archetype, I get prompted for the version again.

Thanks for help and tips!

(I'll provide code if necessary)

1

There are 1 answers

5
glytching On BEST ANSWER

You can define custom properties in you archetype metadata. Have a look at the archetype-metadata.xml in META-INF/maven. For example:

my-archetype
|
+ src
  |
  + main
    |
    + resources
      |
      + META-INF
        |
        + maven
          |
          + archetype-metadata.xml

A custom property for version would look like this:

<?xml version="1.0" encoding="UTF-8"?>
<archetype-descriptor name="basic">
    <requiredProperties>
        <requiredProperty key="version">
            <defaultValue>0.0.1-SNAPSHOT</defaultValue>
        </requiredProperty>
    </requiredProperties>
</archetype-descriptor>

More details in the docs.

When you run the mvn archetype:generate command referencing an archetype with the above configuration you'll see this in the console output:

[INFO] Using property: version = 0.0.1-SNAPSHOT

Or, if you run the mvn archetype:generate command with the parameter -Dversion=FOO then you'll see this in the console output:

[INFO] Using property: version = FOO

Note: this version is, of course, separate from the archetypeVersion which defines the version of the archetype itself rather than the version of the module produced by the archetype.