Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call Java methods in Maven archetype XML

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?

like image 983
ddd Avatar asked Feb 15 '26 00:02

ddd


1 Answers

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>
like image 78
KYL Avatar answered Feb 16 '26 13:02

KYL