I want to create an Archetype in which the user can provide artifactId
. Then I want to take this artifactId
and create two files.
Here is an example.
artifactId= box
FILE 1: copy-box.txt
FILE 2 : Box.java
Creating copy-box.txt
is quite easy. But how to create Box.java
with B
capital?
What is Archetype? In short, Archetype is a Maven project templating toolkit. An archetype is defined as an original pattern or model from which all other things of the same kind are made. The name fits as we are trying to provide a system that provides a consistent means of generating Maven projects.
The metadata about an archetype is stored in the archetype-metadata. xml file located in the directory META-INF/maven of its JAR file, see the reference documentation. The metadata file stores the additional properties, with corresponding default values. It also stores the project's generated files in filesets.
It's an XML file named archetype-metadata. xml and located in the META-INF/maven directory of the jar.
I looked through the code that creates the replacement in the filenames of the archetype resources (which can be found here). It seems that the values of the arguments are taken from the context, which means that they are not evaluated. In my opinion, for the moment it is not possible (sad, but true) to use the evaluation mechanism directly in the file names.
However, by simply implementing the FilesetArchetypeGenerator
interface, a good contribution can be made to the archetypes generation.
There is a bug in maven archetype. See bug reports ARCHETYPE-406 and ARCHETYPE-397.
When fixed it will be possible to define and use custom requiredProperty
for your case.
In archetype-metadata.xml add
<requiredProperties>
<requiredProperty key="classPrefix" >
<defaultValue>
${artifactId.substring(0,1).toUpperCase()}${artifactId.substring(1)}
</defaultValue>
</requiredProperty>
</requiredProperties>
Don't forget to add classPrefix
to archetype.properties
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