为原型Maven项目设置动态速度属性 [英] Setting dynamic velocity properties for archetype maven projects

查看:183
本文介绍了为原型Maven项目设置动态速度属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个原型,并且想要创建一个项目范围内的可用属性,其中包含诸如当前日期和工件ID的所有小写变体之类的内容.我发现有关Stackoverflow的以下帖子,说明了通常应该怎么做.

I am creating an archetype and want to create a project wide available property containing things like the current date and an all lower case variant of the artifact id. I found the following post on Stackoverflow, that shows how it normally should be possible.

我试图像这样将其添加到archetype-metadata.xml中:

I tried adding this to the archetype-metadata.xml like so:

    ...
    <requiredProperty key="artifactIdLower">
        <defaultValue>${artifactId.toLowerCase()}</defaultValue>
    </requiredProperty>
    <requiredProperty key="ldt">
        <defaultValue>${package.getClass().forName("java.time.LocalDateTime").getMethod("now").invoke(null)}</defaultValue>
    </requiredProperty>
    <requiredProperty key="dtf">
        <defaultValue>${package.getClass().forName("java.time.format.DateTimeFormatter").getMethod("ofPattern", $package.getClass().forName("java.lang.String")).invoke(null, "dd MMM yyyy")}</defaultValue>
    </requiredProperty>
    <requiredProperty key="date">
        <defaultValue>${ldt.format($dtf)}</defaultValue>
    </requiredProperty>
    ...

属性artifactIdLower就像一个超级按钮一样工作,但是ldtdtfdate似乎不起作用,出现错误:

The property artifactIdLower works like a charm, but the ldt, dtf and date don't seem to work, giving the error:

Null reference [template 'dtf', line 1, column 1] : ${package.getClass().forName("java.time.format.DateTimeFormatter").getMethod("ofPattern", $package.getClass().forName("java.lang.String")).invoke(null, "dd MMM yyyy")} cannot be resolved.

之后,我尝试查看该空引用在链中的位置.我可以将pkgClass设置为$package.getClass()(或$package.Class),

After that I tried to see where in the chain this null reference comes from. I was able to set pkgClass to $package.getClass() (or to $package.Class),

,但是此后我无法将$strClass设置为$package.getClass().forName("java.lang.String")$pkgClass.forName("java.lang.String")(很有趣,pkgClassstrClass都应该是Class对象).

but after that I was unable to set $strClass to $package.getClass().forName("java.lang.String") or to $pkgClass.forName("java.lang.String") (funnily enough, both pkgClass and strClass should be a Class object).

这让我想知道在archetype-metadata.xml内部使用反射是否受到限制.

This made me wonder if there is a restriction on using reflection inside the archetype-metadata.xml.

我的问题是:如何设置可以在项目范围内使用的动态生成的属性值(如上)?

My question is: how can I set dynamically generated property values (like above) that can be used project wide?

我不想在我创建的每个文件中都定义这些属性,因为以后可能需要添加更多属性.

I don't want to have to define these properties in every file that I create, because there might be more properties that I want to add later.

修改: 我试着创建一个包含#set指令的generalproperties.vm文件.然后,第一行中的每个文件都会使用#parse("generalproperties.vm")加载该文件.虽然我确实从pom.xml文件中解析了该文件,但它的行为没有达到我想要的.

Edit: I tried to instead create a generalproperties.vm file that contained the #set directives. This file would then be loaded by every file on the first line using #parse("generalproperties.vm"). While I did get the file to be parsed from within the pom.xml file, it didnt behave as I wanted.

以下输入

test
$null
#set( $ldtClass = $package.getClass().forName("java.time.LocalDateTime") )
$ldtClass.Name
$ldtClass.getMethod("now")
#set( $ldtNowMethod = $ldtClass.getMethod("now") )
$ldtNowMethod.Name
#set( $clsLoader = $package.getClass().getClassLoader() )
$clsLoader
#set( $ldtClass2 = $clsLoader.loadClass("java.time.LocalDateTime") )
$ldtClass2.Name
$ldtClass2.getMethod("now")
#set( $ldtNowMethod2 = $ldtClass2.getMethod("now") )
$ldtNowMethod2.Name
#set( $ldt = $ldtNowMethod2.invoke($null) )
$ldt
#set( $dtf = $package.getClass().forName("java.time.format.DateTimeFormatter").getMethod("ofPattern", $package.getClass().forName("java.lang.String")).invoke($null, "yyyy/MM/dd HH:mm:ss") )
$dtf

生成了以下输出:

test
$null
java.time.LocalDateTime
$ldtClass.getMethod("now")
$ldtNowMethod.Name
$clsLoader
$ldtClass2.Name
$ldtClass2.getMethod("now")
$ldtNowMethod2.Name
$ldt
$dtf

前3个输出与预期的一样,但是在那之后我没有得到想要的结果.如果有人能够解决上述任何一个问题(使用元数据文件或generalproperties文件),将不胜感激.

The first 3 outputs are as expected, but after that I don't get the results I want. If someone is able to solve either of the above issues (with the metadata file or the generalproperties file), it would be highly appreciated.

推荐答案

这应该有效:

...
<requiredProperty key="date">
    <defaultValue>${package.getClass().forName("java.time.LocalDateTime").getMethod("now").invoke(null).format($package.Class.forName("java.time.format.DateTimeFormatter").getMethod("ofPattern", $package.Class).invoke(null, "dd MMM yyyy"))}</defaultValue>
</requiredProperty>
...

这篇关于为原型Maven项目设置动态速度属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆