问题描述
我需要在Mojo对象中使用自定义类型,例如LunarDate
:
I need to use custom type, e.g., LunarDate
, in my Mojo object:
class MyMojo extends AbstractMojo {
/** @parameter */
LunarDate lunarDate;
}
我想在pom.xml的<configuration>
部分中配置参数.
And I want to configure the parameter in <configuration>
section in pom.xml.
<configuration>
<lunarDate>丁丑年二月初四</lunarDate>
</configuration>
(类型LunarDate
只是说明问题的一个示例)
(The type LunarDate
is just an example to illustrate the question)
我已经有了类型转换器,但是如何启用它们呢?
I've already had the type converters, but how to enable them?
推荐答案
DefaultBeanConfigurator
负责使用DefaultConverterLookup
,并且无需使用Plexus容器即可直接实例化它.
DefaultBeanConfigurator
is responsible for using DefaultConverterLookup
, and it instantiates it directly without using the Plexus Container.
您可以在复制扩展中进行复制和修改,但是通过@Component(role=BeanConfigurator.class)
注册复制可能无效.过去,我曾尝试从构建扩展中替换标准的Maven组件,并在maven-dev上被告知是不可能的.
You could I suppose copy and modify it in a build extension, but registering your copy via @Component(role=BeanConfigurator.class)
will likely have no effect; I have tried replacing standard Maven components in the past from build extensions and been told on maven-dev that it is not possible.
您可以查找默认的BeanConfigurator
并使用反射来获取其ConverterLookup converterLookup
字段,然后使用您的自定义转换器调用registerConverter
,但这很脆弱.
You could look up the default BeanConfigurator
and use reflection to get its ConverterLookup converterLookup
field, then call registerConverter
with your custom convertor, but this would be fragile.
最好是放弃,将Mojo参数声明为String
类型,然后在execute
中显式进行转换.
Probably best is to just give up, declare your Mojo parameter to be of type String
, and do the conversion explicitly in execute
.
这篇关于用于Mojo配置的自定义类型转换器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!