Can I convert an artifactId to a classname prefix in my maven archetype?

后端 未结 3 1507
北海茫月
北海茫月 2021-02-19 03:19

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 format

3条回答
  •  感情败类
    2021-02-19 03:37

    I wanted to be able to do this in file names, so I came up with a hack for a camel case artifactId property:

    
      ${artifactId.replaceAll("^a|-a", "A").replaceAll("^b|-b", "B").replaceAll("^c|-c", "C").replaceAll("^d|-d", "D").replaceAll("^e|-e", "E").replaceAll("^f|-f", "F").replaceAll("^g|-g", "G").replaceAll("^h|-h", "H").replaceAll("^i|-i", "I").replaceAll("^j|-j", "J").replaceAll("^k|-k", "K").replaceAll("^l|-l", "L").replaceAll("^m|-m", "M").replaceAll("^n|-n", "N").replaceAll("^o|-o", "O").replaceAll("^p|-p", "P").replaceAll("^q|-q", "Q").replaceAll("^r|-r", "R").replaceAll("^s|-s", "S").replaceAll("^t|-t", "T").replaceAll("^u|-u", "U").replaceAll("^v|-v", "V").replaceAll("^w|-w", "W").replaceAll("^x|-x", "X").replaceAll("^y|-y", "Y").replaceAll("^z|-z", "Z")}
    
    

    That will convert any artifactId that follows the naming convention of hyphenated, lower case a-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:

    > mvn -v
    Apache Maven 3.2.5 (12a6b3acb947671f09b81f49094c53f426d8cea1; 2014-12-14T11:29:23-06:00)
    

    Also here is a sometimes-useful, unhyphenated version of artifactId:

    
      ${artifactId.replace("-","")}
    
    

提交回复
热议问题