将特殊字符传递给Maven Mojo Map键

问题描述

我的Maven MOJO插件中具有以下属性

@Mojo(name = "generate",defaultPhase = LifecyclePhase.GENERATE_SOURCES,threadSafe = true)
public class GraphQLCodegenMojo extends AbstractMojo {
    @Parameter
    private Map<String,String> customTypesMapping;
}

我通常以以下方式设置(基于Maven guide):

<customTypesMapping>
     <DateTime>java.util.Date</DateTime>
</customTypesMapping>

现在,我想允许插件用户提供特殊字符作为地图

我尝试了不同的方法,但是它们都不起作用:

<customTypesMapping>
    <DateTime!>java.util.Date</DateTime!>
    <DateTime&#33;>java.util.Date</DateTime&#33;>
    <customTypeMapping>
        <name>DateTime</key>
        <value>java.util.Date</value>
    </customTypeMapping>
</customTypeMapping>

是否存在向后兼容的方式来更改我的Maven插件不破坏现有客户端)?

解决方法

解决方案是使用java.util.Properties

@Mojo(name = "generate",defaultPhase = LifecyclePhase.GENERATE_SOURCES,threadSafe = true)
public class GraphQLCodegenMojo extends AbstractMojo {
    @Parameter
    private Properties customTypesMapping = new Properties();

通过这种方式,可以通过两种方式在xml中进行指定:

<customTypesMapping>
    <property>
        <!--note the special character below-->
        <name>Date!</name>
        <value>java.util.Date</value>
    </property>
</customTypesMapping>

并以向后兼容的方式,使该插件的现有用户无需更改其配置:

<customTypesMapping>
    <DateTime>java.util.Date</DateTime>
</customTypesMapping>