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

后端 未结 3 1491
北海茫月
北海茫月 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:47

    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.

    #macro( ccase $str )
    #foreach( $word in $str.split('-') )$word.substring(0,1).toUpperCase()$word.substring(1)#end
    #end
    #set( $classNamePrefix = "#ccase( $artifactId )" )
    
    public class ${classNamePrefix}Application {
        // ...
    }
    

    If you are using a fileSet tag, add filtered="true" property to make sure source files are processed with Velocity.

    See also:

    • http://velocity.apache.org/engine/1.4/user-guide.html#Loops

    There's updated documentation for version 2.0: http://velocity.apache.org/engine/2.0/user-guide.html#loops

提交回复
热议问题