Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用map of maps作为Maven插件参数_Maven_Maven Plugin - Fatal编程技术网

使用map of maps作为Maven插件参数

使用map of maps作为Maven插件参数,maven,maven-plugin,Maven,Maven Plugin,是否可以将地图地图用作Maven插件参数 @参数 专用地图转换器; 然后像这样使用它 <converters> <json> <indent>true</indent> <strict>true</strict> </json> <yaml> <stripComments>false</stripComments> </y

是否可以将地图地图用作Maven插件参数

@参数
专用地图转换器;
然后像这样使用它

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

真的
真的
假的
如果我这样使用它,
converter
只包含值为null的键
json
yaml


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

这显然是Mojo API内部使用的项目的一个限制。如果查看源代码内部,您会发现它首先尝试通过将配置解释为字符串来获取映射的值(调用
fromExpression
),当此操作失败时。但是,此方法不检查,这是我们这里的情况(因为map值的类型是
map
)。我申请了这个项目的Bugzilla来跟踪这个

使用自定义包装器对象 一种解决方法是不使用
贴图
作为值,而是使用自定义对象:

@参数
专用地图转换器;
与Mojo位于同一包装中的class
转换器
,为:

公共类转换器{
@参数
私人地图财产;
@凌驾
公共字符串toString(){return properties.toString();}//用于测试
}
然后,您可以使用以下配置您的Mojo:

<converters>
  <json>
    <properties>
      <indent>true</indent>
      <strict>true</strict>
    </properties>
  </json>
  <yaml>
    <properties>
      <stripComments>false</stripComments>
    </properties>
  </yaml>
</converters>
输出是预期的
{json={indent=true,strict=true},yaml={stripComments=false}

使用自定义配置程序 我还找到了一种使用自定义
组件配置器来保存
映射的方法

因此,我们想通过安装它来修复
MapConverter
,问题是如何注册这个新的
FixedMapConverter
。默认情况下,Maven使用
BasicComponentConfigurator
来配置Mojo,并依赖
DefaultConverterLookup
来查找用于特定类的转换器。在本例中,我们希望为
Map
提供一个自定义转换,它将返回我们的固定版本。因此,我们需要扩展这个基本配置程序并注册我们的新转换器

import org.codehaus.plexus.classworlds.realm.ClassRealm;
导入org.codehaus.plexus.component.configurator.BasicComponentConfigurator;
导入org.codehaus.plexus.component.configurator.ComponentConfigurationException;
导入org.codehaus.plexus.component.configurator.ConfigurationListener;
导入org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
导入org.codehaus.plexus.configuration.PlexusConfiguration;
公共类CustomBasicComponentConfigurator扩展了BasicComponentConfigurator{
@凌驾
公共无效配置组件(最终对象组件、最终丛配置配置、,
final ExpressionEvaluator计算器、final ClassRealm领域、final ConfigurationListener侦听器)
抛出ComponentConfigurationException{
registerConverter(新的FixedMapConverter());
super.configureComponent(组件、配置、计算器、领域、侦听器);
}
}
然后我们需要告诉Maven使用这个新的配置程序而不是基本的配置程序。这是一个两步过程:

  • 在Maven插件中,创建一个文件
    src/main/resources/META-INF/plexus/components.xml
    注册新组件:

    <?xml version="1.0" encoding="UTF-8"?>
    <component-set>
      <components>
        <component>
          <role>org.codehaus.plexus.component.configurator.ComponentConfigurator</role>
          <role-hint>custom-basic</role-hint>
          <implementation>package.to.CustomBasicComponentConfigurator</implementation>
        </component>
      </components>
    </component-set>
    
    此处传递的配置程序对应于上面
    components.xml
    中指定的角色提示

  • 通过这样的设置,您最终可以声明

    @参数
    专用地图转换器;
    
    一切都将被正确地注入:Maven将使用我们的定制配置程序,它将注册我们的地图转换器的固定版本,并将正确地转换内部地图


    FixedMapConverter
    的完整代码(它几乎复制粘贴了
    MapConverter
    ,因为我们无法覆盖错误的方法):

    公共类FixedMapConverter扩展MapConverter{
    来自配置的公共对象(最终转换器lookup查找、最终丛配置配置、,
    最终类类型,最终类型[]类型参数,最终类封闭类型,最终类加载器,
    最终表达式评估器(最终配置侦听器)
    抛出ComponentConfigurationException{
    最终对象值=fromExpression(配置、计算器、类型);
    if(null!=值){
    返回值;
    }
    试一试{
    最终映射=实例化映射(配置、类型、加载程序);
    最终类elementType=findElementType(类型参数);
    if(Object.class==elementType | | String.class==elementType){
    对于(int i=0,size=configuration.getChildCount();i