I am trying to install a custom archetype in my local repo. When user generate a project based on this archetype, they are required to provide the artifactId. Most of the time it is in all lower case. However, the main Class name (also the java filename) is dependent on this artifactId with the first letter capitalized. Instead of asking user to input another variable, I would like to call some String method to convert the artifactId to the correct format for class name.
In Maven archetype: Modify artifactId, looks like you can embed Java method in archetype-metadata.xml as below:
<requiredProperty key="artifactIdWithUnderscore" >
<defaultValue>${artifactId.replaceAll("-", "_")}</defaultValue>
</requiredProperty>
So I did something similar in my archetype-metadata.xml to capitalize first letter.
<requiredProperty key="artifactId" />
<requiredProperty key="serviceName">
<defaultValue>${artifactId.toLowerCase().substring(0,1).toUpperCase()+actifactId.toLowerCase().substring(1)}</defaultValue>
</requiredProperty>
However I got the following parse error:
SEVERE: Parser Exception: serviceName
org.apache.velocity.runtime.parser.ParseException: Encountered "+artifactId.toLowerCase().substring(1)}" at line 1, column 55.
Was expecting one of:
"[" ...
"}" ...
at org.apache.velocity.runtime.parser.Parser.generateParseException(Parser.java:3679)
What is correct way to insert Java String method in this archetype xml?
The plus character can't be used as string concatenation operator here, it's also not needed. Just concatenate two replacements (without any operator between):
<defaultValue>${artifactId.toLowerCase().substring(0,1).toUpperCase()}${actifactId.toLowerCase().substring(1)}</defaultValue>
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