【问题标题】:Can I convert an artifactId to a classname prefix in my maven archetype?我可以在我的 Maven 原型中将 artifactId 转换为类名前缀吗?
【发布时间】:2014-04-24 10:36:51
【问题描述】:

我正在创建一个 maven 原型,并且在生成的项目中想要一个以生成项目的工件 id 命名的类。

工件 ID 的格式如下:the-project-name,类应命名为 TheProjectNameMain

我已尝试在我的 archetype-metadata.xml 中执行此操作,但无法正确操作。

<archetype-descriptor>
    <requiredProperties>
        <requiredProperty key="classNamePrefix">
            <defaultValue>${WordUtils.capitalize(artifactId.replaceAll("-", " ")).replaceAll(" ", "")}</defaultValue>
        </requiredProperty>        
    </requiredProperties>
</archetype-descriptor>

如您所见,我尝试使用 WordUtils(来自 apache-commons),但我猜这不可用,因为我遇到了错误。 Error merging velocity templates:.... 。我也尝试了.replaceAll 的不同组合,但我无法获得正确的格式。

在这种情况下,有谁知道从 a-hypenated-string 到 CamelCaseClassName 的方法?

【问题讨论】:

标签: java maven velocity maven-archetype


【解决方案1】:

Velocity 无法访问任意 java 类,但您可以调用现有对象的方法。在 Maven 原型的上下文中,您可以使用来自 java.lang.String 的方法和 Velocity 循环来完成工作。

#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 处理源文件。

另见:

有 2.0 版的更新文档:http://velocity.apache.org/engine/2.0/user-guide.html#loops

【讨论】:

  • 这适用于文件中的类名,但我仍然需要手动更改文件名。我在文件名中使用了__classNamePrefix__。这不是一个大问题,因为 Intellij 会自动尝试纠正它,但如果这一步由 maven 自动化会更好。有什么想法吗?
  • Velocity 指令在 archetype-metadata.xml 中不起作用,并且使用该文件是更改生成的文件名的唯一方法,我知道。如果可以接受,您可以尝试通过创建一个没有默认值的必需属性来提示用户输入前缀。
  • 生成类后,您可以创建一个META-INF/archetype-post-generate.groovy 脚本,如this post 所述,并重命名文件。
【解决方案2】:

我希望能够在文件名中做到这一点,所以我想出了一个针对骆驼案的 hack artifactId 属性:

<requiredProperty key="artifactIdCamelCase">
  <defaultValue>${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")}</defaultValue>
</requiredProperty>

这会将遵循连字符、小写a-z 命名约定的任何artifactId 转换为驼峰式。通过一些有限的测试,该格式很脆弱,因此换行和正则表达式添加等小修改可能会阻止 Velocity 替换该属性。

应该在 2.1 之后的 Maven 版本上工作(当this bug 被修复时)。我的版本:

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

这里还有一个有时有用的、不带连字符的artifactId

<requiredProperty key="artifactIdUnhyphenated">
  <defaultValue>${artifactId.replace("-","")}</defaultValue>
</requiredProperty>

【讨论】:

  • 非常奇怪 - 当运行 archetype:generate 时使用 -DartifactId=... 它不再起作用,因为字符串转换应用于原型的 (!) 工件 id(即 -DartifactId=我的项目 artifactIdCamelCase = "Myartifact" 而不是 "MyProject")。如果我稍后在 maven 要求时指定工件 ID,则不会发生这种奇怪的行为。
  • @fishbone 我也有同样的问题。你终于修好了?
【解决方案3】:

要改变你的文件名,你可以设置一个 requiredProperty 并使用其他属性来构建它

&lt;requiredProperty key="routeBuilderFileName"&gt;&lt;defaultValue&gt;RouteBuilder${flowName.toUpperCase()}${flowWay.substring(0,1).toUpperCase()}${flowWay.substring(1)}&lt;/defaultValue&gt;&lt;/requiredProperty&gt;

然后将模板文件命名为:__routeBuilderFileName__.java

【讨论】:

  • 不幸的是,当在命令行 (-DflowName=something) 中提供基本名称(在此示例中为 flowName)时,它不起作用。您必须以交互模式提供它 - 然后它将起作用。
  • 你可以使用 artifactId 代替 flowName
猜你喜欢
  • 2022-08-17
  • 1970-01-01
  • 1970-01-01
  • 2014-05-17
  • 1970-01-01
  • 1970-01-01
  • 2017-11-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多