我可以将artifactId转换为maven原型中的classname前缀吗? [英] Can I convert an artifactId to a classname prefix in my maven archetype?
问题描述
我正在创建一个maven原型,并且在项目中生成了一个想要一个以生成项目的工件ID命名的类。
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.
工件ID将会格式为: the-project-name
,该类应命名为 TheProjectNameMain
。
The artifact id will be formatted like: the-project-name
and the class should be named TheProjectNameMain
.
我已经尝试在我的 archetype-metadata.xml
中执行此操作,但我无法正确使用。
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>
你可以看到我试图使用WordUtils(来自apache-commons),但我猜这个因为我收到错误而无法使用。 合并速度模板时出错:....
。我也尝试了 .replaceAll
的不同组合,但我无法获得正确的格式。
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.
有没有人知道在这种情况下,从a-hypenated-string转到CamelCaseClassName的方法是什么?
Does anyone know of a way to go from a-hypenated-string to a CamelCaseClassName in this case?
推荐答案
无法访问任意java类来自Velocity,但你可以调用现有对象的方法。在Maven原型的上下文中,您可以使用 java.lang.String
中的方法和Velocity循环来完成工作。
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 {
// ...
}
如果您正在使用一个 fileSet
标签,添加 filtered =true
属性以确保使用Velocity处理源文件。
If you are using a fileSet
tag, add filtered="true"
property to make sure source files are processed with Velocity.
参见:
- http://velocity.apache.org/engine/1.4/user-guide.html#Loops
版本2.0的更新文档: http:// velocity.apache.org/engine/2.0/user-guide.html#loops
There's updated documentation for version 2.0: http://velocity.apache.org/engine/2.0/user-guide.html#loops
这篇关于我可以将artifactId转换为maven原型中的classname前缀吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!