I'm creating a maven archetype and in the projects generated a want a class that is named after the artifact id of the generated project.
The artifact id will be formatted like: the-project-name
and the class should be named TheProjectNameMain
.
I've tried to do this in my archetype-metadata.xml
but I can't get it right.
<archetype-descriptor>
<requiredProperties>
<requiredProperty key="classNamePrefix">
<defaultValue>${WordUtils.capitalize(artifactId.replaceAll("-", " ")).replaceAll(" ", "")}</defaultValue>
</requiredProperty>
</requiredProperties>
</archetype-descriptor>
As you can see i tried to use WordUtils (from apache-commons) but i'm guessing this is not available because i'm getting an error. Error merging velocity templates:....
. I also tried different combinations of .replaceAll
but i couldn't get the right format.
Does anyone know of a way to go from a-hypenated-string to a CamelCaseClassName in this case?
I wanted to be able to do this in file names, so I came up with a hack for a camel case
artifactId
property:That will convert any
artifactId
that follows the naming convention of hyphenated, lower casea-z
to camel case. From some limited testing, the format is fragile so small edits such as line breaks and regex additions may stop Velocity from replacing the property.Should work on Maven versions after 2.1 (when this bug was fixed). My version:
Also here is a sometimes-useful, unhyphenated version of
artifactId
:There is no access to arbitrary java classes from Velocity, but you can call methods of existing objects. In the context of Maven archetype you can use methods from
java.lang.String
and a Velocity loop to get the job done.If you are using a
fileSet
tag, addfiltered="true"
property to make sure source files are processed with Velocity.See also:
There's updated documentation for version 2.0: http://velocity.apache.org/engine/2.0/user-guide.html#loops
To variabilize your file name, you can set a requiredProperty and use other properties to build it
<requiredProperty key="routeBuilderFileName"><defaultValue>RouteBuilder${flowName.toUpperCase()}${flowWay.substring(0,1).toUpperCase()}${flowWay.substring(1)}</defaultValue></requiredProperty>
Then name the template file :
__routeBuilderFileName__.java