Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Maven ArcheType . Setting file name

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?

like image 254
Ahmad.Masood Avatar asked Sep 12 '13 19:09

Ahmad.Masood


People also ask

What is archetype while creating Maven project?

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.

Where are Maven archetypes stored?

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.

How do I find my Maven archetype?

It's an XML file named archetype-metadata. xml and located in the META-INF/maven directory of the jar.


2 Answers

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.

like image 190
Dimitar Ivanov Avatar answered Oct 19 '22 10:10

Dimitar Ivanov


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

like image 2
Dmytro Avatar answered Oct 19 '22 08:10

Dmytro