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.
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.
An archetype is made up of: an archetype descriptor ( archetype-metadata. xml in directory: src/main/resources/META-INF/maven/ ). It lists all the files that will be contained in the archetype and categorizes them so they can be processed correctly by the archetype generation mechanism.
In the artifact's root directory, there is a pom. xml file of the archetype. You can see the packaging maven-archetype . You can customize build of your archetype here as with any other pom.
maven-archetype-simple is an archetype which generates a simple Maven project: project. |-- pom. xml.
A Maven archetype is an abstraction of a kind of a project that can be instantiated into a concrete customized Maven project. In short, it's a template project template from which other projects are created.
This is a bug in the archetype plugin, and is documented here: https://issues.apache.org/jira/browse/ARCHETYPE-490
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With