maven archetype: generate a default value for requiredProperty from a value of an existing property
maven, maven-archetype
Solution
This is a bug in the archetype plugin, and is documented here: https://issues.apache.org/jira/browse/ARCHETYPE-490
Problem
I want to set a default value for a requiredProperty in archetype-metadata.xml such that it is based on another property passed from command line. Say ``` <requiredProperty key="customProperty"> <defaultValue>${artifactId.toUpperCase()}</defaultValue> </requiredProperty> ``` However, when I use the resulting archetype to generate a new project, it is not the project's artifactId that gets uppercased, but rather the archetype's artifactId. Whereas when I change it to ``` <requiredProperty key="customProperty"> <defaultValue>${artifactId}</defaultValue> </requiredProperty> ``` I get the project's artifactId as one would expect. Is there a way to assign a default value for a custom property based on the value of another property? Note: this happens only in interactive mode. How to reproduce: ``` mvn -B archetype:generate -DartifactId=archet -DgroupId=com.example -Dversion=1.0-SNAPSHOT -DarchetypeArtifactId=maven-archetype-archetype ``` For some reason, archetype.xml gets generated. It is my understanding, it is an old format. Replaced it with archetype-metadata.xml (minimized for the sake of an example): ``` <?xml version="1.0" encoding="UTF-8"?> <archetype-descriptor name="basic"> <requiredProperties> <requiredProperty key="customPropertyUppercased"> <defaultValue>${artifactId.toUpperCase()}</defaultValue> </requiredProperty> <requiredProperty key="customProperty"> <defaultValue>${artifactId}</defaultValue> </requiredProperty> <!--JUnit version to use in generated project--> <requiredProperty key="junit-version"> <defaultValue>4.12</defaultValue> </requiredProperty> </requiredProperties> <fileSets> <fileSet filtered="true" packaged="true"> <directory>src/main/java</directory> </fileSet> <fileSet filtered="true" packaged="true"> <directory>src/test/java</directory> </fileSet> </fileSets> </archetype-descriptor> ``` A template at `./src/main/resources/archetype-resources/src/main/java/App.java:` ``` package ${groupId}.${artifactId}; /** ${customPropertyUppercased} ${customProperty} */ public class App { public static void main( String[] args ) { System.out.println( "Hello World!" ); } } ``` Generating a new project using the resulting archetype ``` mvn archetype:generate -DgroupId=com.example -DartifactId=stacktest -DarchetypeArtifactId=archet -DarchetypeGroupId=com.example -DarchetypeCatalog=local ``` with all properties set to default produces `stacktest/src/main/java/com/example/App.java:` ``` package com.example.stacktest; /** ARCHET stacktest */ public class App { public static void main( String[] args ) { System.out.println( "Hello World!" ); } } ``` So, the customPropertyUppercased is based on the archetype's artifactId, while customProperty is based on the project's artifactId.