用于Mojo配置的自定义类型转换器? [英] Custom type converter for Mojo configuration?

查看:145
本文介绍了用于Mojo配置的自定义类型转换器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在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配置的自定义类型转换器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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