使用地图作为Maven插件参数 [英] Using map of maps as Maven plugin parameters

查看:94
本文介绍了使用地图作为Maven插件参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用地图作为Maven插件参数?

Is it possible to use a map of maps as a Maven plugin parameter?, e.g.

@Parameter
private Map<String, Map<String, String>> converters;

然后像

<converters>
  <json>
     <indent>true</indent>
     <strict>true</strict>
  </json>
  <yaml>
      <stripComments>false</stripComments>
  </yaml>
<converters>

如果我以这种方式使用它,则converters仅包含键jsonyaml,其值为空.

If I use it like this, converters only contain the keys json and yaml with null as values.

我知道可以将复杂的对象作为值,但是像本例中一样,是否可以将映射用于可变元素值?

I know it is possible to have complex objects as values, but is it also somehow possible to use maps for variable element values like in this example?

推荐答案

一种解决方案非常简单,可用于1级嵌套.在替代答案中可以找到一种更复杂的方法,该方法也可能允许对Maps进行更深层的嵌套.

One solution is quite simple and works for 1-level nesting. A more sophisticated approach can be found in the alternative answer which possibly also allows for deeper nesting of Maps.

代替使用接口作为类型参数,只需使用像TreeMap

Instead of using an interface as type parameter, simply use a concrete class like TreeMap

 @Parameter
 private Map<String, TreeMap> converters.

原因是在

The reason is this check in MapConverter which fails for an interface but suceeds for a concrete class:

   private static Class<?> findElementType( final Type[] typeArguments )
   {
       if ( null != typeArguments && typeArguments.length > 1 
            && typeArguments[1] instanceof Class<?> )
       {
           return (Class<?>) typeArguments[1];
       }
       return Object.class;
   }

作为旁注,它也与此 answer 相关 a>对于Maven> 3.3.x,它还可以通过将BasicComponentConfigurator子类化并将其用作Plexus组件来安装自定义转换器. BasicComponentConfigurator具有DefaultConverterLookup作为受保护的成员变量,因此可以很容易地访问以注册自定义转换器.

As a side-note, an as it is also related to this answer for Maven > 3.3.x it also works to install a custom converter by subclassing BasicComponentConfigurator and using it as a Plexus component. BasicComponentConfigurator has the DefaultConverterLookup as a protected member variable and is hence easily accessible for registering custom converters.

这篇关于使用地图作为Maven插件参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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